Coverage Report - org.apache.commons.cli2.builder.SwitchBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SwitchBuilder
65%
27/41
50%
6/12
1.818
 
 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.Switch;
 25  
 import org.apache.commons.cli2.resource.ResourceConstants;
 26  
 import org.apache.commons.cli2.resource.ResourceHelper;
 27  
 
 28  
 /**
 29  
  * Builds Switch instance.
 30  
  */
 31  
 public class SwitchBuilder {
 32  
     private final String enabledPrefix;
 33  
     private final String disabledPrefix;
 34  
     private String description;
 35  
     private String preferredName;
 36  
     private Set aliases;
 37  
     private boolean required;
 38  
     private Argument argument;
 39  
     private Group children;
 40  
     private int id;
 41  
     private Boolean switchDefault;
 42  
 
 43  
     /**
 44  
      * Creates a new SwitchBuilder using defaults.
 45  
      * @see Switch#DEFAULT_ENABLED_PREFIX
 46  
      * @see Switch#DEFAULT_DISABLED_PREFIX
 47  
      */
 48  
     public SwitchBuilder() {
 49  7
         this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX);
 50  7
     }
 51  
 
 52  
     /**
 53  
      * Creates a new SwitchBuilder
 54  
      * @param enabledPrefix the prefix to use for enabling the option
 55  
      * @param disabledPrefix the prefix to use for disabling the option
 56  
      * @throws IllegalArgumentException if either prefix is less than 1
 57  
      *                                  character long or the prefixes match
 58  
      */
 59  
     public SwitchBuilder(final String enabledPrefix,
 60  
                          final String disabledPrefix)
 61  7
         throws IllegalArgumentException {
 62  7
         if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) {
 63  0
             throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX));
 64  
         }
 65  
 
 66  7
         if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) {
 67  0
             throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX));
 68  
         }
 69  
 
 70  7
         if (enabledPrefix.equals(disabledPrefix)) {
 71  0
             throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES));
 72  
         }
 73  
 
 74  7
         this.enabledPrefix = enabledPrefix;
 75  7
         this.disabledPrefix = disabledPrefix;
 76  7
         reset();
 77  7
     }
 78  
 
 79  
     /**
 80  
      * Creates a new Switch instance
 81  
      * @return a new Switch instance
 82  
      */
 83  
     public Switch create() {
 84  7
         final Switch option =
 85  
             new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description,
 86  
                        required, argument, children, id, switchDefault);
 87  
 
 88  7
         reset();
 89  
 
 90  7
         return option;
 91  
     }
 92  
 
 93  
     /**
 94  
      * Resets the builder.
 95  
      * @return this builder
 96  
      */
 97  
     public SwitchBuilder reset() {
 98  14
         description = null;
 99  14
         preferredName = null;
 100  14
         required = false;
 101  14
         aliases = new HashSet();
 102  14
         argument = null;
 103  14
         children = null;
 104  14
         id = 0;
 105  14
         switchDefault = null;
 106  
 
 107  14
         return this;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Use this option description
 112  
      * @param newDescription the description to use
 113  
      * @return this builder
 114  
      */
 115  
     public SwitchBuilder withDescription(final String newDescription) {
 116  0
         this.description = newDescription;
 117  
 
 118  0
         return this;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Use this option name. The first name is used as the preferred
 123  
      * display name for the Command and then later names are used as aliases.
 124  
      *
 125  
      * @param name the name to use
 126  
      * @return this builder
 127  
      */
 128  
     public SwitchBuilder withName(final String name) {
 129  7
         if (preferredName == null) {
 130  7
             preferredName = name;
 131  
         } else {
 132  0
             aliases.add(name);
 133  
         }
 134  
 
 135  7
         return this;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Use this optionality
 140  
      * @param newRequired true iff the Option is required
 141  
      * @return this builder
 142  
      */
 143  
     public SwitchBuilder withRequired(final boolean newRequired) {
 144  0
         this.required = newRequired;
 145  
 
 146  0
         return this;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Use this Argument
 151  
      * @param newArgument the argument to use
 152  
      * @return this builder
 153  
      */
 154  
     public SwitchBuilder withArgument(final Argument newArgument) {
 155  0
         this.argument = newArgument;
 156  
 
 157  0
         return this;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Use this child Group
 162  
      * @param newChildren the child Group to use
 163  
      * @return this builder
 164  
      */
 165  
     public SwitchBuilder withChildren(final Group newChildren) {
 166  0
         this.children = newChildren;
 167  
 
 168  0
         return this;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Sets the id
 173  
      *
 174  
      * @param newId
 175  
      *            the id of the Switch
 176  
      * @return this SwitchBuilder
 177  
      */
 178  
     public final SwitchBuilder withId(final int newId) {
 179  0
         this.id = newId;
 180  
 
 181  0
         return this;
 182  
     }
 183  
 
 184  
     /**
 185  
      * Sets the default state for this switch
 186  
      *
 187  
      * @param newSwitchDefault the default state
 188  
      * @return this SwitchBuilder
 189  
      */
 190  
     public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) {
 191  7
         this.switchDefault = newSwitchDefault;
 192  
 
 193  7
         return this;
 194  
     }
 195  
 }