1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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://jakarta.apache.org/'>Jakarta 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 }