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: * Passes its values to the switches it is an alias for. Use an alias switch
039: * to group many switches together and set them all at once with just one name.
040: */
041: public class AliasSwitch implements CommandLineSwitch {
042: private Collection<CommandLineSwitch> switches = new LinkedList<CommandLineSwitch>();
043:
044: private String name;
045:
046: public AliasSwitch(String name, CommandLineSwitch... switches) {
047: this .name = name;
048: for (CommandLineSwitch commandLineSwitch : switches) {
049: this .switches.add(commandLineSwitch);
050: }
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public Collection<CommandLineSwitch> getSwitches() {
058: return switches;
059: }
060:
061: public Object getDefaultValue() {
062: return "";
063: }
064:
065: public Object getValue() {
066: return null;
067: }
068:
069: public void setValue(Object value) {
070: for (CommandLineSwitch commandLineSwitch : getSwitches()) {
071: commandLineSwitch.setValue(value);
072: }
073: }
074:
075: public boolean isPresent() {
076: boolean result = !getSwitches().isEmpty();
077:
078: for (CommandLineSwitch commandLineSwitch : getSwitches()) {
079: result = result && commandLineSwitch.isPresent();
080: }
081:
082: return result;
083: }
084:
085: public boolean isMandatory() {
086: return false;
087: }
088:
089: public void validate() throws CommandLineException {
090: // Do nothing
091: }
092:
093: public int parse(String value) throws CommandLineException {
094: int result = 1;
095:
096: for (CommandLineSwitch commandLineSwitch : getSwitches()) {
097: result = Math.max(result, commandLineSwitch.parse(value));
098: }
099:
100: return result;
101: }
102:
103: public void accept(Visitor visitor) {
104: visitor.visitAliasSwitch(this);
105: }
106: }
|