Source for net.dpml.cli.builder.SwitchBuilder

   1: /*
   2:  * Copyright 2003-2005 The Apache Software Foundation
   3:  *
   4:  * Licensed under the Apache License, Version 2.0 (the "License");
   5:  * you may not use this file except in compliance with the License.
   6:  * You may obtain a copy of the License at
   7:  *
   8:  *     http://www.apache.org/licenses/LICENSE-2.0
   9:  *
  10:  * Unless required by applicable law or agreed to in writing, software
  11:  * distributed under the License is distributed on an "AS IS" BASIS,
  12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13:  * See the License for the specific language governing permissions and
  14:  * limitations under the License.
  15:  */
  16: package net.dpml.cli.builder;
  17: 
  18: import java.util.HashSet;
  19: import java.util.Set;
  20: 
  21: import net.dpml.cli.Argument;
  22: import net.dpml.cli.Group;
  23: import net.dpml.cli.option.Switch;
  24: import net.dpml.cli.resource.ResourceConstants;
  25: import net.dpml.cli.resource.ResourceHelper;
  26: 
  27: /**
  28:  * Builds Switch instance.
  29:  *
  30:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  31:  * @version @PROJECT-VERSION@
  32:  */
  33: public class SwitchBuilder
  34: {
  35:     private final String m_enabledPrefix;
  36:     private final String m_disabledPrefix;
  37:     private String m_description;
  38:     private String m_preferredName;
  39:     private Set m_aliases;
  40:     private boolean m_required;
  41:     private Argument m_argument;
  42:     private Group m_children;
  43:     private int m_id;
  44:     private Boolean m_switchDefault;
  45: 
  46:     /**
  47:      * Creates a new SwitchBuilder using defaults.
  48:      * @see Switch#DEFAULT_ENABLED_PREFIX
  49:      * @see Switch#DEFAULT_DISABLED_PREFIX
  50:      */
  51:     public SwitchBuilder()
  52:     {
  53:         this( Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX );
  54:     }
  55: 
  56:     /**
  57:      * Creates a new SwitchBuilder
  58:      * @param enabledPrefix the prefix to use for enabling the option
  59:      * @param disabledPrefix the prefix to use for disabling the option
  60:      * @throws IllegalArgumentException if either prefix is less than 1
  61:      *                                  character long or the prefixes match
  62:      */
  63:     public SwitchBuilder( final String enabledPrefix, final String disabledPrefix )
  64:       throws IllegalArgumentException
  65:     {
  66:         if( ( enabledPrefix == null ) || ( enabledPrefix.length() < 1 ) ) 
  67:         {
  68:             throw new IllegalArgumentException(
  69:               ResourceHelper.getResourceHelper().getMessage(
  70:                 ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX ) );
  71:         }
  72: 
  73:         if( ( disabledPrefix == null ) || ( disabledPrefix.length() < 1 ) )
  74:         {
  75:             throw new IllegalArgumentException(
  76:               ResourceHelper.getResourceHelper().getMessage(
  77:                 ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX ) );
  78:         }
  79: 
  80:         if( enabledPrefix.equals( disabledPrefix ) )
  81:         {
  82:             throw new IllegalArgumentException(
  83:               ResourceHelper.getResourceHelper().getMessage(
  84:                 ResourceConstants.SWITCH_IDENTICAL_PREFIXES ) );
  85:         }
  86: 
  87:         m_enabledPrefix = enabledPrefix;
  88:         m_disabledPrefix = disabledPrefix;
  89:         reset();
  90:     }
  91: 
  92:     /**
  93:      * Creates a new Switch instance
  94:      * @return a new Switch instance
  95:      */
  96:     public Switch create()
  97:     {
  98:         final Switch option =
  99:             new Switch(
 100:               m_enabledPrefix, 
 101:               m_disabledPrefix, 
 102:               m_preferredName, 
 103:               m_aliases, 
 104:               m_description,
 105:               m_required, 
 106:               m_argument, 
 107:               m_children, 
 108:               m_id, 
 109:               m_switchDefault );
 110:         reset();
 111:         return option;
 112:     }
 113: 
 114:     /**
 115:      * Resets the builder.
 116:      * @return the builder
 117:      */
 118:     public SwitchBuilder reset() 
 119:     {
 120:         m_description = null;
 121:         m_preferredName = null;
 122:         m_required = false;
 123:         m_aliases = new HashSet();
 124:         m_argument = null;
 125:         m_children = null;
 126:         m_id = 0;
 127:         m_switchDefault = null;
 128:         return this;
 129:     }
 130: 
 131:     /**
 132:      * Use this option description
 133:      * @param newDescription the description to use
 134:      * @return this builder
 135:      */
 136:     public SwitchBuilder withDescription( final String newDescription ) 
 137:     {
 138:         m_description = newDescription;
 139:         return this;
 140:     }
 141: 
 142:     /**
 143:      * Use this option name. The first name is used as the preferred
 144:      * display name for the Command and then later names are used as aliases.
 145:      *
 146:      * @param name the name to use
 147:      * @return this builder
 148:      */
 149:     public SwitchBuilder withName( final String name )
 150:     {
 151:         if( m_preferredName == null )
 152:         {
 153:             m_preferredName = name;
 154:         } 
 155:         else
 156:         {
 157:             m_aliases.add( name );
 158:         }
 159:         return this;
 160:     }
 161: 
 162:     /**
 163:      * Use this optionality
 164:      * @param newRequired true iff the Option is required
 165:      * @return this builder
 166:      */
 167:     public SwitchBuilder withRequired( final boolean newRequired )
 168:     {
 169:         m_required = newRequired;
 170:         return this;
 171:     }
 172: 
 173:     /**
 174:      * Use this Argument
 175:      * @param newArgument the argument to use
 176:      * @return this builder
 177:      */
 178:     public SwitchBuilder withArgument( final Argument newArgument )
 179:     {
 180:         m_argument = newArgument;
 181:         return this;
 182:     }
 183: 
 184:     /**
 185:      * Use this child Group
 186:      * @param newChildren the child Group to use
 187:      * @return this builder
 188:      */
 189:     public SwitchBuilder withChildren( final Group newChildren )
 190:     {
 191:         m_children = newChildren;
 192:         return this;
 193:     }
 194: 
 195:     /**
 196:      * Sets the id
 197:      *
 198:      * @param newId the id of the Switch
 199:      * @return this SwitchBuilder
 200:      */
 201:     public final SwitchBuilder withId( final int newId )
 202:     {
 203:         m_id = newId;
 204:         return this;
 205:     }
 206: 
 207:     /**
 208:      * Sets the default state for this switch
 209:      *
 210:      * @param newSwitchDefault the default state
 211:      * @return this SwitchBuilder
 212:      */
 213:     public final SwitchBuilder withSwitchDefault( final Boolean newSwitchDefault )
 214:     {
 215:         m_switchDefault = newSwitchDefault;
 216:         return this;
 217:     }
 218: }