View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.commons.betwixt;
19  
20  import java.util.HashMap;
21  import java.util.Set;
22  
23  /**
24   * Collective for <code>Betwixt</code> optional behaviour hints.
25   * An option links a name with a value (both strings). 
26   * 
27   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
28   * @since 0.5
29   */
30  public class Options {
31      /** Empty string array for use with toArray  */
32      private static final String[] EMPTY_STRING_ARRAY = {};
33      /** Option values indexed by name */
34      private HashMap valuesByName = new HashMap();
35      
36      /**
37       * Gets the value (if any) associated with the given name.
38       * @param name <code>String</code>, not null 
39       * @return the associated value, or null if no value is assocated
40       */
41      public String getValue(String name) {
42         return (String) valuesByName.get(name); 
43      }
44      
45      /**
46       * Gets the names of each option.
47       * @return <code>String</code> array containing the name of each option
48       */
49      public String[] getNames() {
50          Set names = valuesByName.keySet();
51          return (String[]) names.toArray(EMPTY_STRING_ARRAY);
52      }
53      
54      /**
55       * Adds the option.
56       * The rule with options is that the last call to set the
57       * value with a given name wins.
58       * @param name <code>String</code> name, not null
59       * @param value <code>Strong</code> name, not null
60       */
61      public void addOption(String name, String value) {
62          valuesByName.put(name, value);
63      }
64      
65      /**
66       * Adds multiple options from an existing <code>Options</code> collection.
67       * The rule with options is that the most recently set value for an option
68       * wins, so options are potentially overwritten by this call.
69       * 
70       * @param options -
71       *            an existing <code>Options</code> collection
72       * @since 0.8
73       */
74      public void addOptions(Options options) {
75          valuesByName.putAll(options.valuesByName);
76      }
77  }