001: /*
002: * $Id: Boolean.java,v 1.19 2007/08/27 11:18:20 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.types.standard;
008:
009: import org.xins.common.types.Type;
010: import org.xins.common.types.TypeValueException;
011: import org.xins.common.MandatoryArgumentChecker;
012:
013: /**
014: * Standard type <em>_boolean</em>.
015: *
016: * @version $Revision: 1.19 $ $Date: 2007/08/27 11:18:20 $
017: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
018: *
019: * @since XINS 1.0.0
020: */
021: public final class Boolean extends Type {
022:
023: /**
024: * The only instance of this class. This field is never <code>null</code>.
025: */
026: public static final Boolean SINGLETON = new org.xins.common.types.standard.Boolean();
027:
028: /**
029: * Constructs a new <code>Boolean</code>.
030: * This constructor is private, the field {@link #SINGLETON} should be
031: * used.
032: */
033: private Boolean() {
034: super ("_boolean", java.lang.Boolean.class);
035: }
036:
037: /**
038: * Converts the specified non-<code>null</code> string value to a
039: * <code>boolean</code>.
040: *
041: * @param string
042: * the string to convert, cannot be <code>null</code>.
043: *
044: * @return
045: * the <code>boolean</code> value.
046: *
047: * @throws IllegalArgumentException
048: * if <code>string == null</code>.
049: *
050: * @throws TypeValueException
051: * if the specified string does not represent a valid value for this
052: * type.
053: */
054: public static boolean fromStringForRequired(String string)
055: throws IllegalArgumentException, TypeValueException {
056: if ("true".equals(string)) {
057: return true;
058: } else if ("false".equals(string)) {
059: return false;
060: } else if (string == null) {
061: throw new IllegalArgumentException("string == null");
062: } else {
063: throw new TypeValueException(SINGLETON, string);
064: }
065: }
066:
067: /**
068: * Converts the specified string value to a <code>java.lang.Boolean</code>
069: * value.
070: *
071: * @param string
072: * the string to convert, can be <code>null</code>.
073: *
074: * @return
075: * the {@link java.lang.Boolean}, or <code>null</code> if
076: * <code>string == null</code>.
077: *
078: * @throws TypeValueException
079: * if the specified string does not represent a valid value for this
080: * type.
081: */
082: public static java.lang.Boolean fromStringForOptional(String string)
083: throws TypeValueException {
084: if ("true".equals(string)) {
085: return java.lang.Boolean.TRUE;
086: } else if ("false".equals(string)) {
087: return java.lang.Boolean.FALSE;
088: } else if (string == null) {
089: return null;
090: } else {
091: throw new TypeValueException(SINGLETON, string);
092: }
093: }
094:
095: /**
096: * Converts the specified <code>Boolean</code> to a string.
097: *
098: * @param value
099: * the value to convert, can be <code>null</code>.
100: *
101: * @return
102: * the textual representation of the value, or <code>null</code> if and
103: * only if <code>value == null</code>.
104: */
105: public static String toString(java.lang.Boolean value) {
106: if (value == null) {
107: return null;
108: } else {
109: return toString(value.booleanValue());
110: }
111: }
112:
113: /**
114: * Converts the specified <code>boolean</code> to a string.
115: *
116: * @param value
117: * the value to convert.
118: *
119: * @return
120: * the textual representation of the value, never <code>null</code>.
121: */
122: public static String toString(boolean value) {
123: return value ? "true" : "false";
124: }
125:
126: /**
127: * Determines if the specified <code>String</code> value is considered
128: * valid for this type (implementation method).
129: *
130: * <p>This method is called from {@link #isValidValue(String)}. When
131: * called from that method, it is guaranteed that the argument is not
132: * <code>null</code>.
133: *
134: * @param string
135: * the <code>String</code> value that should be checked for validity,
136: * never <code>null</code>.
137: *
138: * @return
139: * <code>true</code> if and only if the specified <code>String</code>
140: * value is valid, <code>false</code> otherwise.
141: */
142: protected boolean isValidValueImpl(String string) {
143: return "true".equals(string) || "false".equals(string);
144: }
145:
146: /**
147: * Converts from a <code>String</code> to an instance of the value class
148: * for this type (implementation method).
149: *
150: * <p>This method is not required to check the validity of the specified
151: * value (since {@link #isValidValueImpl(String)} should have been called
152: * before) but if it does, then it may throw a {@link TypeValueException}.
153: *
154: * @param string
155: * the string to convert to an instance of the value class, guaranteed
156: * to be not <code>null</code> and guaranteed to have been passed to
157: * {@link #isValidValueImpl(String)} without getting an exception.
158: *
159: * @return
160: * an instance of the value class, cannot be <code>null</code>.
161: */
162: protected Object fromStringImpl(String string) {
163: return "true".equals(string) ? java.lang.Boolean.TRUE
164: : java.lang.Boolean.FALSE;
165: }
166:
167: /**
168: * Generates a string representation of the specified value for this type.
169: * The specified value must be an instance of the value class for this type
170: * (see {@link #getValueClass()}). Also, it may have to fall within a
171: * certain range of valid values, depending on the type.
172: *
173: * @param value
174: * the value, cannot be <code>null</code>.
175: *
176: * @return
177: * the string representation of the specified value for this type,
178: * cannot be <code>null</code>.
179: *
180: * @throws IllegalArgumentException
181: * if <code>value == null</code>.
182: *
183: * @throws ClassCastException
184: * if <code>getValueClass().isInstance(value) == false</code>.
185: *
186: * @throws TypeValueException
187: * if the specified value is not in the allowed range.
188: */
189: public String toString(Object value)
190: throws IllegalArgumentException, ClassCastException,
191: TypeValueException {
192: MandatoryArgumentChecker.check("value", value);
193: java.lang.Boolean b = (java.lang.Boolean) value;
194: return b.toString();
195: }
196:
197: public String getDescription() {
198: return "A boolean, either 'true' or 'false'.";
199: }
200: }
|