001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.cli2.commandline;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.HashSet;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Set;
025    
026    import org.apache.commons.cli2.CommandLine;
027    import org.apache.commons.cli2.Option;
028    import org.apache.commons.cli2.option.PropertyOption;
029    
030    /**
031     * Manages a queue of default CommandLines. This CommandLine implementation is
032     * backed by a queue of CommandLine instances which are queried in turn until a
033     * suitable result is found.
034     *
035     * CommandLine instances can either be added to the back of the queue or can be
036     * pushed in at a specific position.
037     *
038     * @see #appendCommandLine(CommandLine)
039     * @see #insertCommandLine(int, CommandLine)
040     */
041    public class DefaultingCommandLine extends CommandLineImpl {
042    
043        /**
044         * The list of default CommandLine instances
045         */
046        private final List commandLines = new ArrayList();
047    
048        /**
049         * Adds a CommandLine instance to the back of the queue. The supplied
050         * CommandLine will be used as defaults when all other CommandLines produce
051         * no result
052         *
053         * @param commandLine
054         *            the default values to use if all CommandLines
055         */
056        public void appendCommandLine(final CommandLine commandLine) {
057            commandLines.add(commandLine);
058        }
059    
060        /**
061         * Adds a CommandLine instance to a specified position in the queue.
062         *
063         * @param index ths position at which to insert
064         * @param commandLine the CommandLine to insert
065         */
066        public void insertCommandLine(
067            final int index,
068            final CommandLine commandLine) {
069            commandLines.add(index, commandLine);
070        }
071    
072        /**
073         * Builds an iterator over the build in CommandLines.
074         *
075         * @return an unmodifiable iterator
076         */
077        public Iterator commandLines(){
078            return Collections.unmodifiableList(commandLines).iterator();
079        }
080    
081        public Option getOption(String trigger) {
082            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
083                final CommandLine commandLine = (CommandLine)i.next();
084                final Option actual = commandLine.getOption(trigger);
085                if (actual != null) {
086                    return actual;
087                }
088            }
089            return null;
090        }
091    
092        public List getOptions() {
093            final List options = new ArrayList();
094    
095            final List temp = new ArrayList();
096            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
097                final CommandLine commandLine = (CommandLine)i.next();
098                temp.clear();
099                temp.addAll(commandLine.getOptions());
100                temp.removeAll(options);
101                options.addAll(temp);
102            }
103    
104            return Collections.unmodifiableList(options);
105        }
106    
107        public Set getOptionTriggers() {
108            final Set all = new HashSet();
109            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
110                final CommandLine commandLine = (CommandLine)i.next();
111                all.addAll(commandLine.getOptionTriggers());
112            }
113    
114            return Collections.unmodifiableSet(all);
115        }
116    
117        public boolean hasOption(Option option) {
118            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
119                final CommandLine commandLine = (CommandLine)i.next();
120                if (commandLine.hasOption(option)) {
121                    return true;
122                }
123            }
124            return false;
125        }
126    
127        public List getValues(Option option, List defaultValues) {
128            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
129                final CommandLine commandLine = (CommandLine)i.next();
130                final List actual = commandLine.getValues(option);
131                if (actual != null && !actual.isEmpty()) {
132                    return actual;
133                }
134            }
135            if(defaultValues==null){
136                return Collections.EMPTY_LIST;
137            }
138            else{
139                return defaultValues;
140            }
141        }
142    
143        public Boolean getSwitch(Option option, Boolean defaultValue) {
144            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
145                final CommandLine commandLine = (CommandLine)i.next();
146                final Boolean actual = commandLine.getSwitch(option);
147                if (actual != null) {
148                    return actual;
149                }
150            }
151            return defaultValue;
152        }
153    
154        public String getProperty(final String property) {
155            return getProperty(new PropertyOption(), property);
156        }
157    
158        public String getProperty(final Option option, String property, String defaultValue) {
159            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
160                final CommandLine commandLine = (CommandLine)i.next();
161                final String actual = commandLine.getProperty(option, property);
162                if (actual != null) {
163                    return actual;
164                }
165            }
166            return defaultValue;
167        }
168    
169        public Set getProperties(final Option option) {
170            final Set all = new HashSet();
171            for (final Iterator i = commandLines.iterator(); i.hasNext();) {
172                final CommandLine commandLine = (CommandLine)i.next();
173                all.addAll(commandLine.getProperties(option));
174            }
175            return Collections.unmodifiableSet(all);
176        }
177    
178        public Set getProperties() {
179            return getProperties(new PropertyOption());
180        }
181    }