001: /*
002: * Copyright (C) 2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.util.*;
020:
021: /**
022: * Result when a command line option is found.
023: * Contains the original option and all of its arguments.
024: *
025: * More information about this class and code samples for suggested use are
026: * available from <a target="_top" href=
027: * "http://ostermiller.org/utils/CmdLn.html">ostermiller.org</a>.
028: *
029: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
030: * @since ostermillerutils 1.07.00
031: */
032: public class CmdLnResult {
033: /**
034: * The option that caused this result
035: *
036: * @since ostermillerutils 1.07.00
037: */
038: private CmdLnOption option;
039:
040: /**
041: * The arguments that have been found
042: * for the option.
043: *
044: * @since ostermillerutils 1.07.00
045: */
046: private ArrayList<String> arguments;
047:
048: /**
049: * New command line result
050: *
051: * @param option Option that caused this result
052: *
053: * @since ostermillerutils 1.07.00
054: */
055: CmdLnResult(CmdLnOption option) {
056: this .option = option;
057: }
058:
059: /**
060: * @return the option that caused this result
061: *
062: * @since ostermillerutils 1.07.00
063: */
064: public CmdLnOption getOption() {
065: return option;
066: }
067:
068: /**
069: * @param argument add an argument to this result
070: * @throws IllegalStateException if too many arguments have been added
071: *
072: * @since ostermillerutils 1.07.00
073: */
074: void addArgument(String argument) {
075: if (hasAllArguments()) {
076: throw new IllegalStateException(
077: "Too many arguments to option");
078: }
079: if (arguments == null) {
080: arguments = new ArrayList<String>(Math.min(option
081: .getMaxArguments(), 16));
082: }
083: arguments.add(argument);
084: }
085:
086: /**
087: * @return true iff enough arguments have been added (max not exceeded)
088: *
089: * @since ostermillerutils 1.07.00
090: */
091: boolean hasAllArguments() {
092: return (getArgumentCount() >= option.getMaxArguments());
093: }
094:
095: /**
096: * @return true iff more arguments need to be added (min not satisfied)
097: *
098: * @since ostermillerutils 1.07.00
099: */
100: boolean requiresMoreArguments() {
101: return (getArgumentCount() < option.getMinArguments());
102: }
103:
104: /**
105: * Get the argument count for this option
106: * @return number of arguments
107: *
108: * @since ostermillerutils 1.07.00
109: */
110: public int getArgumentCount() {
111: if (arguments == null)
112: return 0;
113: return arguments.size();
114: }
115:
116: /**
117: * Get all the arguments, in the order that
118: * they were specified.
119: * @return unmodifiable list of arguments or null if none
120: *
121: * @since ostermillerutils 1.07.00
122: */
123: public List<String> getArguments() {
124: if (getArgumentCount() == 0)
125: return null;
126: return Collections.unmodifiableList(arguments);
127: }
128:
129: /**
130: * get the first argument, or null if no arguments
131: * @return first argument
132: *
133: * @since ostermillerutils 1.07.00
134: */
135: public String getArgument() {
136: if (getArgumentCount() == 0)
137: return null;
138: return arguments.get(0);
139: }
140: }
|