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