Coverage Report - org.apache.commons.cli2.builder.DefaultOptionBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultOptionBuilder
100%
48/48
94%
17/18
2.091
 
 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.builder;
 18  
 
 19  
 import java.util.HashSet;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.apache.commons.cli2.Argument;
 23  
 import org.apache.commons.cli2.Group;
 24  
 import org.apache.commons.cli2.option.DefaultOption;
 25  
 import org.apache.commons.cli2.resource.ResourceConstants;
 26  
 import org.apache.commons.cli2.resource.ResourceHelper;
 27  
 
 28  
 /**
 29  
  * Builds DefaultOption instances.
 30  
  */
 31  
 public class DefaultOptionBuilder {
 32  
     private final String shortPrefix;
 33  
     private final String longPrefix;
 34  
     private final boolean burstEnabled;
 35  
     private String preferredName;
 36  
     private Set aliases;
 37  
     private Set burstAliases;
 38  
     private boolean required;
 39  
     private String description;
 40  
     private Argument argument;
 41  
     private Group children;
 42  
     private int id;
 43  
 
 44  
     /**
 45  
      * Creates a new DefaultOptionBuilder using defaults
 46  
      * @see DefaultOption#DEFAULT_SHORT_PREFIX
 47  
      * @see DefaultOption#DEFAULT_LONG_PREFIX
 48  
      * @see DefaultOption#DEFAULT_BURST_ENABLED
 49  
      */
 50  
     public DefaultOptionBuilder() {
 51  843
         this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX,
 52  
              DefaultOption.DEFAULT_BURST_ENABLED);
 53  843
     }
 54  
 
 55  
     /**
 56  
      * Creates a new DefaultOptionBuilder
 57  
      * @param shortPrefix the prefix to use for short options
 58  
      * @param longPrefix the prefix to use for long options
 59  
      * @param burstEnabled whether to allow gnu style bursting
 60  
      * @throws IllegalArgumentException if either prefix is less than on
 61  
      *                                  character long
 62  
      */
 63  
     public DefaultOptionBuilder(final String shortPrefix,
 64  
                                 final String longPrefix,
 65  
                                 final boolean burstEnabled)
 66  848
         throws IllegalArgumentException {
 67  848
         if ((shortPrefix == null) || (shortPrefix.length() == 0)) {
 68  2
             throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX));
 69  
         }
 70  
 
 71  846
         if ((longPrefix == null) || (longPrefix.length() == 0)) {
 72  2
             throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX));
 73  
         }
 74  
 
 75  844
         this.shortPrefix = shortPrefix;
 76  844
         this.longPrefix = longPrefix;
 77  844
         this.burstEnabled = burstEnabled;
 78  844
         reset();
 79  844
     }
 80  
 
 81  
     /**
 82  
      * Creates a DefaultOption instance
 83  
      * @return the new instance
 84  
      * @throws IllegalStateException if no names have been supplied
 85  
      */
 86  
     public DefaultOption create()
 87  
         throws IllegalStateException {
 88  1061
         if (preferredName == null) {
 89  1
             throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
 90  
         }
 91  
 
 92  1060
         final DefaultOption option =
 93  
             new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description,
 94  
                               aliases, burstAliases, required, argument, children, id);
 95  
 
 96  1060
         reset();
 97  
 
 98  1060
         return option;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Resets the builder.
 103  
      * @return this builder
 104  
      */
 105  
     public DefaultOptionBuilder reset() {
 106  1906
         preferredName = null;
 107  1906
         description = null;
 108  1906
         aliases = new HashSet();
 109  1906
         burstAliases = new HashSet();
 110  1906
         required = false;
 111  1906
         argument = null;
 112  1906
         children = null;
 113  1906
         id = 0;
 114  
 
 115  1906
         return this;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Use this short option name. The first name is used as the preferred
 120  
      * display name for the Command and then later names are used as aliases.
 121  
      *
 122  
      * @param shortName the name to use
 123  
      * @return this builder
 124  
      */
 125  
     public DefaultOptionBuilder withShortName(final String shortName) {
 126  288
         final String name = shortPrefix + shortName;
 127  
 
 128  288
         if (preferredName == null) {
 129  213
             preferredName = name;
 130  
         } else {
 131  75
             aliases.add(name);
 132  
         }
 133  
 
 134  288
         if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) {
 135  245
             burstAliases.add(name);
 136  
         }
 137  
 
 138  288
         return this;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Use this long option name.  The first name is used as the preferred
 143  
      * display name for the Command and then later names are used as aliases.
 144  
      *
 145  
      * @param longName the name to use
 146  
      * @return this builder
 147  
      */
 148  
     public DefaultOptionBuilder withLongName(final String longName) {
 149  1103
         final String name = longPrefix + longName;
 150  
 
 151  1103
         if (preferredName == null) {
 152  849
             preferredName = name;
 153  
         } else {
 154  254
             aliases.add(name);
 155  
         }
 156  
 
 157  1103
         return this;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Use this option description
 162  
      * @param newDescription the description to use
 163  
      * @return this builder
 164  
      */
 165  
     public DefaultOptionBuilder withDescription(final String newDescription) {
 166  247
         this.description = newDescription;
 167  
 
 168  247
         return this;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Use this optionality
 173  
      * @param newRequired true iff the Option is required
 174  
      * @return this builder
 175  
      */
 176  
     public DefaultOptionBuilder withRequired(final boolean newRequired) {
 177  12
         this.required = newRequired;
 178  
 
 179  12
         return this;
 180  
     }
 181  
 
 182  
     /**
 183  
      * Use this child Group
 184  
      * @param newChildren the child Group to use
 185  
      * @return this builder
 186  
      */
 187  
     public DefaultOptionBuilder withChildren(final Group newChildren) {
 188  6
         this.children = newChildren;
 189  
 
 190  6
         return this;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Use this Argument
 195  
      * @param newArgument the argument to use
 196  
      * @return this builder
 197  
      */
 198  
     public DefaultOptionBuilder withArgument(final Argument newArgument) {
 199  80
         this.argument = newArgument;
 200  
 
 201  80
         return this;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Sets the id
 206  
      *
 207  
      * @param newId
 208  
      *            the id of the DefaultOption
 209  
      * @return this DefaultOptionBuilder
 210  
      */
 211  
     public final DefaultOptionBuilder withId(final int newId) {
 212  22
         this.id = newId;
 213  
 
 214  22
         return this;
 215  
     }
 216  
 }