Coverage Report - org.apache.commons.cli2.commandline.DefaultingCommandLine
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultingCommandLine
100%
67/67
96%
29/30
2.615
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.cli2.commandline;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.cli2.CommandLine;
 27  
 import org.apache.commons.cli2.Option;
 28  
 import org.apache.commons.cli2.option.PropertyOption;
 29  
 
 30  
 /**
 31  
  * Manages a queue of default CommandLines. This CommandLine implementation is
 32  
  * backed by a queue of CommandLine instances which are queried in turn until a
 33  
  * suitable result is found.
 34  
  *
 35  
  * CommandLine instances can either be added to the back of the queue or can be
 36  
  * pushed in at a specific position.
 37  
  *
 38  
  * @see #appendCommandLine(CommandLine)
 39  
  * @see #insertCommandLine(int, CommandLine)
 40  
  */
 41  40
 public class DefaultingCommandLine extends CommandLineImpl {
 42  
 
 43  
     /**
 44  
      * The list of default CommandLine instances
 45  
      */
 46  40
     private final List commandLines = new ArrayList();
 47  
 
 48  
     /**
 49  
      * Adds a CommandLine instance to the back of the queue. The supplied
 50  
      * CommandLine will be used as defaults when all other CommandLines produce
 51  
      * no result
 52  
      *
 53  
      * @param commandLine
 54  
      *            the default values to use if all CommandLines
 55  
      */
 56  
     public void appendCommandLine(final CommandLine commandLine) {
 57  40
         commandLines.add(commandLine);
 58  40
     }
 59  
 
 60  
     /**
 61  
      * Adds a CommandLine instance to a specified position in the queue.
 62  
      *
 63  
      * @param index ths position at which to insert
 64  
      * @param commandLine the CommandLine to insert
 65  
      */
 66  
     public void insertCommandLine(
 67  
         final int index,
 68  
         final CommandLine commandLine) {
 69  2
         commandLines.add(index, commandLine);
 70  2
     }
 71  
 
 72  
     /**
 73  
      * Builds an iterator over the build in CommandLines.
 74  
      *
 75  
      * @return an unmodifiable iterator
 76  
      */
 77  
     public Iterator commandLines(){
 78  6
         return Collections.unmodifiableList(commandLines).iterator();
 79  
     }
 80  
 
 81  
     public Option getOption(String trigger) {
 82  29
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 83  27
             final CommandLine commandLine = (CommandLine)i.next();
 84  27
             final Option actual = commandLine.getOption(trigger);
 85  27
             if (actual != null) {
 86  18
                 return actual;
 87  
             }
 88  9
         }
 89  11
         return null;
 90  
     }
 91  
 
 92  
     public List getOptions() {
 93  5
         final List options = new ArrayList();
 94  
 
 95  5
         final List temp = new ArrayList();
 96  5
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 97  5
             final CommandLine commandLine = (CommandLine)i.next();
 98  5
             temp.clear();
 99  5
             temp.addAll(commandLine.getOptions());
 100  5
             temp.removeAll(options);
 101  5
             options.addAll(temp);
 102  5
         }
 103  
 
 104  5
         return Collections.unmodifiableList(options);
 105  
     }
 106  
 
 107  
     public Set getOptionTriggers() {
 108  1
         final Set all = new HashSet();
 109  1
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 110  2
             final CommandLine commandLine = (CommandLine)i.next();
 111  2
             all.addAll(commandLine.getOptionTriggers());
 112  2
         }
 113  
 
 114  1
         return Collections.unmodifiableSet(all);
 115  
     }
 116  
 
 117  
     public boolean hasOption(Option option) {
 118  5
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 119  5
             final CommandLine commandLine = (CommandLine)i.next();
 120  5
             if (commandLine.hasOption(option)) {
 121  3
                 return true;
 122  
             }
 123  2
         }
 124  2
         return false;
 125  
     }
 126  
 
 127  
     public List getValues(Option option, List defaultValues) {
 128  28
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 129  26
             final CommandLine commandLine = (CommandLine)i.next();
 130  26
             final List actual = commandLine.getValues(option);
 131  26
             if (actual != null && !actual.isEmpty()) {
 132  16
                 return actual;
 133  
             }
 134  10
         }
 135  12
         if(defaultValues==null){
 136  1
             return Collections.EMPTY_LIST;
 137  
         }
 138  
         else{
 139  11
             return defaultValues;
 140  
         }
 141  
     }
 142  
 
 143  
     public Boolean getSwitch(Option option, Boolean defaultValue) {
 144  8
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 145  8
             final CommandLine commandLine = (CommandLine)i.next();
 146  8
             final Boolean actual = commandLine.getSwitch(option);
 147  8
             if (actual != null) {
 148  4
                 return actual;
 149  
             }
 150  4
         }
 151  4
         return defaultValue;
 152  
     }
 153  
 
 154  
     public String getProperty(final String property) {
 155  2
         return getProperty(new PropertyOption(), property);
 156  
     }
 157  
 
 158  
     public String getProperty(final Option option, String property, String defaultValue) {
 159  4
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 160  4
             final CommandLine commandLine = (CommandLine)i.next();
 161  4
             final String actual = commandLine.getProperty(option, property);
 162  4
             if (actual != null) {
 163  2
                 return actual;
 164  
             }
 165  2
         }
 166  2
         return defaultValue;
 167  
     }
 168  
 
 169  
     public Set getProperties(final Option option) {
 170  1
         final Set all = new HashSet();
 171  1
         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
 172  1
             final CommandLine commandLine = (CommandLine)i.next();
 173  1
             all.addAll(commandLine.getProperties(option));
 174  1
         }
 175  1
         return Collections.unmodifiableSet(all);
 176  
     }
 177  
 
 178  
     public Set getProperties() {
 179  1
         return getProperties(new PropertyOption());
 180  
     }
 181  
 }