001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.util.args;
028    import org.opends.messages.Message;
029    
030    
031    
032    import static org.opends.messages.UtilityMessages.*;
033    import org.opends.messages.MessageBuilder;
034    
035    
036    /**
037     * This class defines an argument type that will be used to represent Boolean
038     * values.  These arguments will never take values from the command line but
039     * and will never be required.  If the argument is provided, then it will be
040     * considered true, and if not then it will be considered false.  As such,
041     * the default value will always be "false".
042     */
043    public class BooleanArgument
044           extends Argument
045    {
046      /**
047       * Creates a new Boolean argument with the provided information.
048       *
049       * @param  name              The generic name that should be used to refer to
050       *                           this argument.
051       * @param  shortIdentifier   The single-character identifier for this
052       *                           argument, or <CODE>null</CODE> if there is none.
053       * @param  longIdentifier    The long identifier for this argument, or
054       *                           <CODE>null</CODE> if there is none.
055       * @param  description       Message for the description of this
056       *                           argument.
057       *
058       * @throws  ArgumentException  If there is a problem with any of the
059       *                             parameters used to create this argument.
060       */
061      public BooleanArgument(String name, Character shortIdentifier,
062                             String longIdentifier,
063                             Message description)
064             throws ArgumentException
065      {
066        super(name, shortIdentifier, longIdentifier, false, false, false, null,
067              String.valueOf(false), null, description);
068      }
069    
070    
071    
072      /**
073       * Indicates whether the provided value is acceptable for use in this
074       * argument.
075       *
076       * @param  valueString    The value for which to make the determination.
077       * @param  invalidReason  A buffer into which the invalid reason may be
078       *                        written if the value is not acceptable.
079       *
080       * @return  <CODE>true</CODE> if the value is acceptable, or
081       *          <CODE>false</CODE> if it is not.
082       */
083      public boolean valueIsAcceptable(String valueString,
084                                       MessageBuilder invalidReason)
085      {
086        // This argument type should never have a value, so any value provided will
087        // be unacceptable.
088    
089        invalidReason.append(ERR_BOOLEANARG_NO_VALUE_ALLOWED.get(getName()));
090    
091        return false;
092      }
093    
094    
095      /**
096       * {@inheritDoc}
097       */
098      final public void addValue(String valueString) {
099        if (valueString != null) {
100          clearValues();
101          super.addValue(valueString);
102          super.setPresent(Boolean.valueOf(valueString));
103        }
104      }
105    
106    
107      /**
108       * {@inheritDoc}
109       */
110      final public void setPresent(boolean isPresent) {
111        addValue(String.valueOf(isPresent));
112      }
113    
114    }
115