001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.commandline;
034:
035: import java.util.*;
036:
037: /**
038: * The switch must be followed by a value, but it can occur multiple times
039: * on the command-line. The values are accumulated in the same order as on
040: * the command-line and you retrieve them as a single <code>java.util.List</code>.
041: */
042: public class MultipleValuesSwitch extends CommandLineSwitchBase {
043: public MultipleValuesSwitch(String name) {
044: this (name, new LinkedList<String>(), false);
045: }
046:
047: public MultipleValuesSwitch(String name, String defaultValue) {
048: this (name, Collections.singletonList(defaultValue), false);
049: }
050:
051: public MultipleValuesSwitch(String name, String[] defaultValue) {
052: this (name, Arrays.asList(defaultValue), false);
053: }
054:
055: public MultipleValuesSwitch(String name, List<String> defaultValue) {
056: this (name, defaultValue, false);
057: }
058:
059: public MultipleValuesSwitch(String name, boolean mandatory) {
060: this (name, new LinkedList<String>(), mandatory);
061: }
062:
063: public MultipleValuesSwitch(String name, String defaultValue,
064: boolean mandatory) {
065: this (name, Collections.singletonList(defaultValue), mandatory);
066: }
067:
068: public MultipleValuesSwitch(String name, String[] defaultValue,
069: boolean mandatory) {
070: this (name, Arrays.asList(defaultValue), mandatory);
071: }
072:
073: public MultipleValuesSwitch(String name, List<String> defaultValue,
074: boolean mandatory) {
075: super (name, new LinkedList<String>(defaultValue), mandatory);
076:
077: this .value = new LinkedList();
078: }
079:
080: public Object getValue() {
081: Object result = getDefaultValue();
082:
083: if (!((List<String>) value).isEmpty()) {
084: result = value;
085: }
086:
087: return result;
088: }
089:
090: public void setValue(Object value) {
091: ((List<String>) this .value).add((String) value);
092: super .setValue(this .value);
093: }
094:
095: public int parse(String value) throws CommandLineException {
096: if (value == null) {
097: throw new CommandLineException(
098: "Missing mandatory value for switch \"" + getName()
099: + "\"");
100: }
101:
102: setValue(value);
103:
104: return 2;
105: }
106:
107: public void accept(Visitor visitor) {
108: visitor.visitMultipleValuesSwitch(this);
109: }
110: }
|