| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArgumentList |
|
| 3.0;3 |
| 1 | /* | |
| 2 | ||
| 3 | dsh-commandline Command line parser based on typed arguments. | |
| 4 | Copyright (c) 2004-2014 held jointly by the individual authors. | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or modify it | |
| 7 | under the terms of the GNU Lesser General Public License as published | |
| 8 | by the Free Software Foundation; either version 3 of the License, or (at | |
| 9 | your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or | |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 14 | License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public License | |
| 17 | along with this library; if not, write to the Free Software Foundation, | |
| 18 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 19 | ||
| 20 | > http://www.fsf.org/licensing/licenses/lgpl.html | |
| 21 | > http://www.opensource.org/licenses/lgpl-license.php | |
| 22 | ||
| 23 | */ | |
| 24 | package org.dishevelled.commandline; | |
| 25 | ||
| 26 | import java.util.Set; | |
| 27 | import java.util.List; | |
| 28 | import java.util.Arrays; | |
| 29 | import java.util.Iterator; | |
| 30 | import java.util.HashSet; | |
| 31 | import java.util.ArrayList; | |
| 32 | import java.util.Collection; | |
| 33 | import java.util.Collections; | |
| 34 | ||
| 35 | /** | |
| 36 | * An argument list. | |
| 37 | * | |
| 38 | * @author Michael Heuer | |
| 39 | * @version $Revision$ $Date$ | |
| 40 | */ | |
| 41 | public final class ArgumentList | |
| 42 | implements Iterable<Argument<?>> | |
| 43 | { | |
| 44 | /** Set of argument short names. */ | |
| 45 | private final Set<String> shortNames; | |
| 46 | ||
| 47 | /** Set of argument long names. */ | |
| 48 | private final Set<String> longNames; | |
| 49 | ||
| 50 | /** List of arguments. */ | |
| 51 | private final List<Argument<?>> arguments; | |
| 52 | ||
| 53 | ||
| 54 | /** | |
| 55 | * Create a new empty argument list. | |
| 56 | */ | |
| 57 | public ArgumentList() | |
| 58 | 199 | { |
| 59 | 199 | shortNames = new HashSet<String>(); |
| 60 | 199 | longNames = new HashSet<String>(); |
| 61 | 199 | arguments = new ArrayList<Argument<?>>(); |
| 62 | 199 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Create a new argument list with the specified arguments. The arguments must | |
| 66 | * not contain any duplicate short or long names or an <code>IllegalArgumentException</code> | |
| 67 | * will be thrown. | |
| 68 | * | |
| 69 | * @param arguments variable number of arguments, must not be null | |
| 70 | */ | |
| 71 | public ArgumentList(final Argument<?>... arguments) | |
| 72 | { | |
| 73 | 4 | this(); |
| 74 | 4 | if (arguments == null) |
| 75 | { | |
| 76 | 1 | throw new IllegalArgumentException("arguments must not be null"); |
| 77 | } | |
| 78 | 3 | addAll(Arrays.asList(arguments)); |
| 79 | 2 | } |
| 80 | ||
| 81 | /** | |
| 82 | * Create a new argument list with the arguments in the specified | |
| 83 | * collection of arguments. The arguments will be copied defensively | |
| 84 | * into this class. The arguments in <code>arguments</code> must | |
| 85 | * not contain any duplicate short or long names or an <code>IllegalArgumentException</code> | |
| 86 | * will be thrown. | |
| 87 | * | |
| 88 | * @param arguments collection of arguments to add, must not be null | |
| 89 | */ | |
| 90 | public ArgumentList(final Collection<Argument<?>> arguments) | |
| 91 | { | |
| 92 | 189 | this(); |
| 93 | 189 | addAll(arguments); |
| 94 | 188 | } |
| 95 | ||
| 96 | ||
| 97 | /** | |
| 98 | * Add the specified argument to this argument list. An argument with | |
| 99 | * the same short or long name as the specified argument must not | |
| 100 | * be contained in this argument list already or an <code>IllegalArgumentException</code> | |
| 101 | * will be thrown. | |
| 102 | * | |
| 103 | * @param argument argument to add, must not be null | |
| 104 | * @return true if this argument list changed as a result of the call | |
| 105 | */ | |
| 106 | public boolean add(final Argument<?> argument) | |
| 107 | { | |
| 108 | 201 | if (argument == null) |
| 109 | { | |
| 110 | 2 | throw new IllegalArgumentException("argument must not be null"); |
| 111 | } | |
| 112 | 199 | if (shortNames.contains(argument.getShortName())) |
| 113 | { | |
| 114 | 3 | throw new IllegalArgumentException("this argument list already contains an argument with short name " |
| 115 | + argument.getShortName()); | |
| 116 | } | |
| 117 | 196 | if (longNames.contains(argument.getLongName())) |
| 118 | { | |
| 119 | 2 | throw new IllegalArgumentException("this argument list already contains an argument with long name " |
| 120 | + argument.getLongName()); | |
| 121 | } | |
| 122 | 194 | if (arguments.add(argument)) |
| 123 | { | |
| 124 | 194 | shortNames.add(argument.getShortName()); |
| 125 | 194 | longNames.add(argument.getLongName()); |
| 126 | 194 | return true; |
| 127 | } | |
| 128 | 0 | return false; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Add all of the arguments in the specified collection of arguments to this | |
| 133 | * argument list. An argument with the same short or long name as any | |
| 134 | * of the arguments in <code>arguments</code> must not be contained | |
| 135 | * in this argument list already or an <code>IllegalArgumentException</code> | |
| 136 | * will be thrown. | |
| 137 | * | |
| 138 | * @param arguments collection of arguments to add, must not be null | |
| 139 | * @return true if this argument list changed as a result of the call | |
| 140 | */ | |
| 141 | public boolean addAll(final Collection<Argument<?>> arguments) | |
| 142 | { | |
| 143 | 197 | if (arguments == null) |
| 144 | { | |
| 145 | 2 | throw new IllegalArgumentException("arguments must not be null"); |
| 146 | } | |
| 147 | ||
| 148 | 195 | boolean result = false; |
| 149 | 195 | for (Argument<?> a : arguments) |
| 150 | { | |
| 151 | 197 | result = add(a) || result; |
| 152 | 193 | } |
| 153 | 191 | return result; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Return the number of arguments in this argument list. | |
| 158 | * | |
| 159 | * @return the number of arguments in this argument list | |
| 160 | */ | |
| 161 | public int size() | |
| 162 | { | |
| 163 | 2 | return arguments.size(); |
| 164 | } | |
| 165 | ||
| 166 | /** {@inheritDoc} */ | |
| 167 | public Iterator<Argument<?>> iterator() | |
| 168 | { | |
| 169 | 1299 | return Collections.unmodifiableList(arguments).iterator(); |
| 170 | } | |
| 171 | } |