components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/control/SimpleNavigable.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.common.control;
       
    27 
       
    28 import java.util.*;
       
    29 
       
    30 /**
       
    31  * The {@code SimpleNavigable} class provides a default implementation of
       
    32  * the {@link Navigable} interface.
       
    33  */
       
    34 public class SimpleNavigable implements Navigable {
       
    35     //
       
    36     // Instance data
       
    37     //
       
    38 
       
    39     private String id;
       
    40     private String name;
       
    41     private Map<String, String> parameters = Collections.emptyMap();
       
    42 
       
    43     //
       
    44     // Constructors
       
    45     //
       
    46 
       
    47     /**
       
    48      * Constructs a {@code SimpleNavigable} from the given identifier and
       
    49      * initialization parameters.
       
    50      *
       
    51      * @param	    id
       
    52      *		    a {@link Control#getId Control identifier}
       
    53      *
       
    54      * @param	    name
       
    55      *		    the name of this {@link Navigable}, or {@code null} for no
       
    56      *		    name
       
    57      *
       
    58      * @param	    parameters
       
    59      *		    initialization parameters for the {@link Control} with the
       
    60      *		    given id, or {@code null} if no parameters apply
       
    61      */
       
    62     public SimpleNavigable(String id, String name,
       
    63 	Map<String, String> parameters) {
       
    64 
       
    65 	this.id = id;
       
    66 	this.name = name;
       
    67 	this.parameters = parameters;
       
    68     }
       
    69 
       
    70     /**
       
    71      * Constructs a {@code SimpleNavigable} from the given identifier and
       
    72      * initialization parameters.
       
    73      *
       
    74      * @param	    id
       
    75      *		    a {@link Control#getId Control identifier}
       
    76      *
       
    77      * @param	    name
       
    78      *		    the name of this {@link Navigable}, or {@code null} for no
       
    79      *		    name
       
    80      *
       
    81      * @param	    parameters
       
    82      *		    initialization parameters (in {@link #toMap array form}) for
       
    83      *		    the {@link Control} with the given id
       
    84      *
       
    85      * @exception   ArrayIndexOutOfBoundsException
       
    86      *		    see {@link #toMap}
       
    87      */
       
    88     public SimpleNavigable(String id, String name, String... parameters) {
       
    89 	this(id, name, toMap(parameters));
       
    90     }
       
    91 
       
    92     //
       
    93     // HasId methods
       
    94     //
       
    95 
       
    96     @Override
       
    97     public String getId() {
       
    98 	return id;
       
    99     }
       
   100 
       
   101     //
       
   102     // Navigable methods
       
   103     //
       
   104 
       
   105     @Override
       
   106     public String getName() {
       
   107 	return name;
       
   108     }
       
   109 
       
   110     @Override
       
   111     public Map<String, String> getParameters() {
       
   112 	return parameters;
       
   113     }
       
   114 
       
   115     //
       
   116     // Object methods
       
   117     //
       
   118 
       
   119     @Override
       
   120     public String toString() {
       
   121 	return Control.encode(getId(), getParameters());
       
   122     }
       
   123 
       
   124     //
       
   125     // Static methods
       
   126     //
       
   127 
       
   128     /**
       
   129      * Converts the given parameter array to a map.
       
   130      *
       
   131      * @param	    parameters
       
   132      *		    an array of the form {@code [name1, value1, name2, value2,
       
   133      *		    ...]}
       
   134      *
       
   135      * @exception   ArrayIndexOutOfBoundsException
       
   136      *		    if the given array does not take the above form
       
   137      */
       
   138     public static Map<String, String> toMap(String... parameters) {
       
   139 	if (parameters == null) {
       
   140 	    return null;
       
   141 	}
       
   142 
       
   143 	Map<String, String> map = new HashMap<String, String>();
       
   144 	for (int i = 0; i < parameters.length; i += 2) {
       
   145 	    map.put(parameters[i], parameters[i + 1]);
       
   146 	}
       
   147 
       
   148 	return map;
       
   149     }
       
   150 }