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