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