001/*
002 * Copyright 2008-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2008-2017 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.util.args;
022
023
024
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.List;
028
029import com.unboundid.util.Mutable;
030import com.unboundid.util.ThreadSafety;
031import com.unboundid.util.ThreadSafetyLevel;
032
033import static com.unboundid.util.args.ArgsMessages.*;
034
035
036
037/**
038 * Creates a new argument that is intended to represent Boolean states based on
039 * whether it was present in the provided set of command-line arguments.
040 * Boolean arguments never have values, since the argument identifier itself is
041 * sufficient to indicate presence.  If the argument is present in the set of
042 * provided command-line arguments, then it will be assumed to have a value of
043 * {@code true}.  If the argument is not present, then it will be assumed to
044 * have a value of {@code false}.
045 * <BR><BR>
046 * Note that it may be beneficial in some cases to allow multiple occurrences of
047 * the same Boolean argument if that has special meaning (e.g., if "-v" is used
048 * to enable verbose output, then perhaps "-v -v" would be even more verbose).
049 */
050@Mutable()
051@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
052public final class BooleanArgument
053       extends Argument
054{
055  /**
056   * The serial version UID for this serializable class.
057   */
058  private static final long serialVersionUID = -3366354214909534696L;
059
060
061
062  /**
063   * Creates a new Boolean argument with the provided information.  The
064   * argument will be allowed at most one time in a set of command line
065   * arguments.
066   *
067   * @param  shortIdentifier  The short identifier for this argument.  It may
068   *                          not be {@code null} if the long identifier is
069   *                          {@code null}.
070   * @param  longIdentifier   The long identifier for this argument.  It may
071   *                          not be {@code null} if the short identifier is
072   *                          {@code null}.
073   * @param  description      A human-readable description for this argument.
074   *                          It must not be {@code null}.
075   *
076   * @throws  ArgumentException  If there is a problem with the definition of
077   *                             this argument.
078   */
079  public BooleanArgument(final Character shortIdentifier,
080                         final String longIdentifier, final String description)
081         throws ArgumentException
082  {
083    super(shortIdentifier, longIdentifier, false, 1, null, description);
084  }
085
086
087
088  /**
089   * Creates a new Boolean argument with the provided information.
090   *
091   * @param  shortIdentifier  The short identifier for this argument.  It may
092   *                          not be {@code null} if the long identifier is
093   *                          {@code null}.
094   * @param  longIdentifier   The long identifier for this argument.  It may
095   *                          not be {@code null} if the short identifier is
096   *                          {@code null}.
097   * @param  maxOccurrences   The maximum number of times this argument may be
098   *                          provided on the command line.  A value less than
099   *                          or equal to zero indicates that it may be present
100   *                          any number of times.
101   * @param  description      A human-readable description for this argument.
102   *                          It must not be {@code null}.
103   *
104   * @throws  ArgumentException  If there is a problem with the definition of
105   *                             this argument.
106   */
107  public BooleanArgument(final Character shortIdentifier,
108                         final String longIdentifier, final int maxOccurrences,
109                         final String description)
110         throws ArgumentException
111  {
112    super(shortIdentifier, longIdentifier, false, maxOccurrences, null,
113          description);
114  }
115
116
117
118  /**
119   * Creates a new Boolean argument that is a "clean" copy of the provided
120   * source argument.
121   *
122   * @param  source  The source argument to use for this argument.
123   */
124  private BooleanArgument(final BooleanArgument source)
125  {
126    super(source);
127  }
128
129
130
131  /**
132   * {@inheritDoc}
133   */
134  @Override()
135  protected void addValue(final String valueString)
136            throws ArgumentException
137  {
138    throw new ArgumentException(ERR_BOOLEAN_VALUES_NOT_ALLOWED.get(
139                                     getIdentifierString()));
140  }
141
142
143
144  /**
145   * {@inheritDoc}
146   */
147  @Override()
148  public List<String> getValueStringRepresentations(final boolean useDefault)
149  {
150    return Collections.unmodifiableList(
151         Arrays.asList(String.valueOf(isPresent())));
152  }
153
154
155
156  /**
157   * {@inheritDoc}
158   */
159  @Override()
160  protected boolean hasDefaultValue()
161  {
162    return false;
163  }
164
165
166
167  /**
168   * {@inheritDoc}
169   */
170  @Override()
171  public String getDataTypeName()
172  {
173    return INFO_BOOLEAN_TYPE_NAME.get();
174  }
175
176
177
178  /**
179   * {@inheritDoc}
180   */
181  @Override()
182  public String getValueConstraints()
183  {
184    return INFO_BOOLEAN_CONSTRAINTS.get();
185  }
186
187
188
189  /**
190   * {@inheritDoc}
191   */
192  @Override()
193  public BooleanArgument getCleanCopy()
194  {
195    return new BooleanArgument(this);
196  }
197
198
199
200  /**
201   * {@inheritDoc}
202   */
203  @Override()
204  protected void addToCommandLine(final List<String> argStrings)
205  {
206    for (int i=0; i < getNumOccurrences(); i++)
207    {
208      argStrings.add(getIdentifierString());
209    }
210  }
211
212
213
214  /**
215   * {@inheritDoc}
216   */
217  @Override()
218  public void toString(final StringBuilder buffer)
219  {
220    buffer.append("BooleanArgument(");
221    appendBasicToStringInfo(buffer);
222    buffer.append(')');
223  }
224}