001/* 002 * Copyright 2010-2014 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2010-2014 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 com.unboundid.util.Mutable; 026import com.unboundid.util.StaticUtils; 027import com.unboundid.util.ThreadSafety; 028import com.unboundid.util.ThreadSafetyLevel; 029 030import static com.unboundid.util.args.ArgsMessages.*; 031 032 033 034/** 035 * Creates a new argument that is intended to represent Boolean states based on 036 * the value provided for this argument. This is similar to the 037 * {@link BooleanArgument} argument type, except that the Boolean value for this 038 * argument must be explicitly specified, whereas the Boolean value for the 039 * {@code BooleanArgument} class is inferred based on whether the argument 040 * was present. 041 * <BR><BR> 042 * Arguments of this type must always have exactly one value. Values of "true", 043 * "t", "yes", "y", "on", and "1" will be interpreted as representing a Boolean 044 * value of {@code true}, and values of "false", "f", "no", "n", "off", and "0" 045 * will be interpreted as representing a Boolean value of {@code false}. No 046 * other values will be allowed. 047 */ 048@Mutable() 049@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 050public final class BooleanValueArgument 051 extends Argument 052{ 053 /** 054 * The serial version UID for this serializable class. 055 */ 056 private static final long serialVersionUID = -3903872574065550222L; 057 058 059 060 // The default value for this argument. 061 private final Boolean defaultValue; 062 063 // The provided value for this argument. 064 private Boolean value; 065 066 067 068 /** 069 * Creates a new Boolean value argument with no default value. 070 * 071 * @param shortIdentifier The short identifier for this argument. It may 072 * not be {@code null} if the long identifier is 073 * {@code null}. 074 * @param longIdentifier The long identifier for this argument. It may 075 * not be {@code null} if the short identifier is 076 * {@code null}. 077 * @param isRequired Indicates whether this argument is required to 078 * be provided. 079 * @param valuePlaceholder A placeholder to display in usage information to 080 * indicate that a value must be provided. It must 081 * not be {@code null}. 082 * @param description A human-readable description for this argument. 083 * It must not be {@code null}. 084 * 085 * @throws ArgumentException If there is a problem with the definition of 086 * this argument. 087 */ 088 public BooleanValueArgument(final Character shortIdentifier, 089 final String longIdentifier, 090 final boolean isRequired, 091 final String valuePlaceholder, 092 final String description) 093 throws ArgumentException 094 { 095 this(shortIdentifier, longIdentifier, isRequired, valuePlaceholder, 096 description, null); 097 } 098 099 100 101 /** 102 * Creates a new Boolean value argument with the specified default value. 103 * 104 * @param shortIdentifier The short identifier for this argument. It may 105 * not be {@code null} if the long identifier is 106 * {@code null}. 107 * @param longIdentifier The long identifier for this argument. It may 108 * not be {@code null} if the short identifier is 109 * {@code null}. 110 * @param isRequired Indicates whether this argument is required to 111 * be provided. 112 * @param valuePlaceholder A placeholder to display in usage information to 113 * indicate that a value must be provided. It must 114 * not be {@code null}. 115 * @param description A human-readable description for this argument. 116 * It must not be {@code null}. 117 * @param defaultValue The default value that will be used for this 118 * argument if no values are provided. It may be 119 * {@code null} if there should not be a default 120 * value. 121 * 122 * @throws ArgumentException If there is a problem with the definition of 123 * this argument. 124 */ 125 public BooleanValueArgument(final Character shortIdentifier, 126 final String longIdentifier, 127 final boolean isRequired, 128 final String valuePlaceholder, 129 final String description, 130 final Boolean defaultValue) 131 throws ArgumentException 132 { 133 super(shortIdentifier, longIdentifier, isRequired, 1, valuePlaceholder, 134 description); 135 136 if (valuePlaceholder == null) 137 { 138 throw new ArgumentException( 139 ERR_ARG_MUST_TAKE_VALUE.get(getIdentifierString())); 140 } 141 142 this.defaultValue = defaultValue; 143 144 value = null; 145 } 146 147 148 149 /** 150 * Creates a new Boolean value argument that is a "clean" copy of the provided 151 * source argument. 152 * 153 * @param source The source argument to use for this argument. 154 */ 155 private BooleanValueArgument(final BooleanValueArgument source) 156 { 157 super(source); 158 159 defaultValue = source.defaultValue; 160 value = null; 161 } 162 163 164 165 /** 166 * {@inheritDoc} 167 */ 168 @Override() 169 protected boolean hasDefaultValue() 170 { 171 return (defaultValue != null); 172 } 173 174 175 176 /** 177 * Retrieves the default value for this argument, if defined. 178 * 179 * @return The default value for this argument, or {@code null} if none is 180 * defined. 181 */ 182 public Boolean getDefaultValue() 183 { 184 return defaultValue; 185 } 186 187 188 189 /** 190 * Retrieves the value for this argument, if one was provided. 191 * 192 * @return The value for this argument. If no value was provided but a 193 * default value was defined, then the default value will be 194 * returned. If no value was provided and no default value was 195 * defined, then {@code null} will be returned. 196 */ 197 public Boolean getValue() 198 { 199 if (value == null) 200 { 201 return defaultValue; 202 } 203 else 204 { 205 return value; 206 } 207 } 208 209 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override() 215 protected void addValue(final String valueString) 216 throws ArgumentException 217 { 218 if (value != null) 219 { 220 throw new ArgumentException( 221 ERR_ARG_MAX_OCCURRENCES_EXCEEDED.get(getIdentifierString())); 222 } 223 224 final String lowerStr = StaticUtils.toLowerCase(valueString); 225 if (lowerStr.equals("true") || lowerStr.equals("t") || 226 lowerStr.equals("yes") || lowerStr.equals("y") || 227 lowerStr.equals("on") || lowerStr.equals("1")) 228 { 229 value = Boolean.TRUE; 230 } 231 else if (lowerStr.equals("false") || lowerStr.equals("f") || 232 lowerStr.equals("no") || lowerStr.equals("n") || 233 lowerStr.equals("off") || lowerStr.equals("0")) 234 { 235 value = Boolean.FALSE; 236 } 237 else 238 { 239 throw new ArgumentException(ERR_ARG_VALUE_NOT_ALLOWED.get( 240 valueString, getIdentifierString())); 241 } 242 } 243 244 245 246 /** 247 * {@inheritDoc} 248 */ 249 @Override() 250 public String getDataTypeName() 251 { 252 return INFO_BOOLEAN_VALUE_TYPE_NAME.get(); 253 } 254 255 256 257 /** 258 * {@inheritDoc} 259 */ 260 @Override() 261 public String getValueConstraints() 262 { 263 return INFO_BOOLEAN_VALUE_CONSTRAINTS.get(); 264 } 265 266 267 268 /** 269 * {@inheritDoc} 270 */ 271 @Override() 272 public BooleanValueArgument getCleanCopy() 273 { 274 return new BooleanValueArgument(this); 275 } 276 277 278 279 /** 280 * {@inheritDoc} 281 */ 282 @Override() 283 public void toString(final StringBuilder buffer) 284 { 285 buffer.append("BooleanValueArgument("); 286 appendBasicToStringInfo(buffer); 287 288 if (defaultValue != null) 289 { 290 buffer.append(", defaultValue="); 291 buffer.append(defaultValue); 292 } 293 294 buffer.append(')'); 295 } 296}