001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple;
007:
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013:
014: /**
015: * <p>Representation of a group of detected command line options, their arguments, and
016: * non-option arguments.</p>
017: *
018: * @since 1.0
019: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
020: * @version $Id: OptionSet.java,v 1.22 2007/04/10 20:06:25 pholser Exp $
021: */
022: public class OptionSet {
023: private final Map detectedOptions = new HashMap();
024: private final List nonOptionArguments = new ArrayList();
025:
026: OptionSet() {
027: // Empty on purpose. Package-private because clients don't create these.
028: }
029:
030: /**
031: * Tells whether the given option was detected.
032: *
033: * @since 1.0
034: * @param option the option to search for
035: * @return <code>true</code> if the option was detected
036: */
037: public boolean wasDetected(String option) {
038: return detectedOptions.containsKey(option);
039: }
040:
041: /**
042: * Gives the detected non-option arguments.
043: *
044: * @since 2.1
045: * @return the detected non-option arguments as a list of {@link String}s.
046: */
047: public List nonOptionArguments() {
048: return Collections.unmodifiableList(nonOptionArguments);
049: }
050:
051: /**
052: * Tells whether there are any arguments associated with the given option.
053: *
054: * @since 1.0
055: * @param option the option to search for
056: * @return <code>true</code> if the option was detected and at least one argument was
057: * detected for the option
058: */
059: public boolean hasArgument(String option) {
060: return !argumentsOf(option).isEmpty();
061: }
062:
063: /**
064: * Gives the argument associated with the given option.
065: *
066: * @since 1.0
067: * @param option the option to search for
068: * @return the argument of the given option as a {@link String}; <code>null</code> if
069: * no argument is present, or that option was not detected
070: * @throws OptionException if more than one argument was detected for the option
071: * @throws ClassCastException if the argument was given a type other than
072: * {@link String}
073: * @see #valueOf(String)
074: */
075: public String argumentOf(String option) {
076: return (String) valueOf(option);
077: }
078:
079: /**
080: * Gives any arguments associated with the given option.
081: *
082: * @since 1.0
083: * @param option the option to search for
084: * @return the arguments associated with the option, as a list of objects of the
085: * type given to the arguments; an empty list if no such arguments are present, or if
086: * the option was not detected
087: * @see #valuesOf(String)
088: */
089: public List argumentsOf(String option) {
090: return valuesOf(option);
091: }
092:
093: /**
094: * Gives the argument associated with the given option. If the argument was given a
095: * type, it will take on that type; otherwise, use {@link #argumentOf(String)
096: * argumentOf} to get the argument as a {@link String}.
097: *
098: * @since 2.0
099: * @param option the option to search for
100: * @return the argument of the given option; <code>null</code> if no argument is
101: * present, or that option was not detected
102: * @throws OptionException if more than one argument was detected for the
103: * option
104: */
105: public Object valueOf(String option) {
106: List values = valuesOf(option);
107:
108: switch (values.size()) {
109: case 0:
110: return null;
111: case 1:
112: return values.get(0);
113: default:
114: throw new MultipleArgumentsForOptionException(option);
115: }
116: }
117:
118: /**
119: * Gives any arguments associated with the given option.
120: *
121: * @since 2.0
122: * @param option the option to search for
123: * @return the arguments associated with the option, as a list of objects of the
124: * type given to the arguments; an empty list if no such arguments are present, or if
125: * the option was not detected
126: */
127: public List valuesOf(String option) {
128: List values = (List) detectedOptions.get(option);
129: return values == null ? Collections.EMPTY_LIST : Collections
130: .unmodifiableList(values);
131: }
132:
133: /**
134: * {@inheritDoc}
135: */
136: public boolean equals(Object that) {
137: if (this == that)
138: return true;
139:
140: if (that == null || !getClass().equals(that.getClass()))
141: return false;
142:
143: OptionSet other = (OptionSet) that;
144: return detectedOptions.equals(other.detectedOptions)
145: && nonOptionArguments
146: .equals(other.nonOptionArguments());
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: public int hashCode() {
153: return detectedOptions.hashCode()
154: ^ nonOptionArguments.hashCode();
155: }
156:
157: void add(String option) {
158: addWithArgument(option, null);
159: }
160:
161: void addWithArgument(String option, Object argument) {
162: List optionArguments = (List) detectedOptions.get(option);
163:
164: if (optionArguments == null) {
165: optionArguments = new ArrayList();
166: detectedOptions.put(option, optionArguments);
167: }
168:
169: if (argument != null)
170: optionArguments.add(argument);
171: }
172:
173: void addNonOptionArgument(String argument) {
174: nonOptionArguments.add(argument);
175: }
176: }
|