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