001: /*
002: * $Id: Int8.java,v 1.20 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>_int8</em>.
015: *
016: * @version $Revision: 1.20 $ $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 class Int8 extends Type {
022:
023: /**
024: * The only instance of this class. This field is never <code>null</code>.
025: */
026: public static final Int8 SINGLETON = new Int8();
027:
028: /**
029: * The minimum value that this Int8 can have.
030: */
031: private final byte _minimum;
032:
033: /**
034: * The maximum value that this Int8 can have.
035: */
036: private final byte _maximum;
037:
038: /**
039: * Constructs a new <code>Int8</code>.
040: * This constructor is private, the field {@link #SINGLETON} should be
041: * used.
042: */
043: private Int8() {
044: this ("_int8", Byte.MIN_VALUE, Byte.MAX_VALUE);
045: }
046:
047: /**
048: * Constructs a new <code>Int8</code> object (constructor for
049: * subclasses).
050: *
051: * @param name
052: * the name of this type, cannot be <code>null</code>.
053: *
054: * @param minimum
055: * the minimum for the value.
056: *
057: * @param maximum
058: * the maximum for the value.
059: */
060: protected Int8(String name, byte minimum, byte maximum) {
061: super (name, Byte.class);
062:
063: _minimum = minimum;
064: _maximum = maximum;
065: }
066:
067: /**
068: * Converts the specified non-<code>null</code> string value to a
069: * <code>byte</code>.
070: *
071: * @param string
072: * the string to convert, cannot be <code>null</code>.
073: *
074: * @return
075: * the <code>byte</code> value.
076: *
077: * @throws IllegalArgumentException
078: * if <code>string == null</code>.
079: *
080: * @throws TypeValueException
081: * if the specified string does not represent a valid value for this
082: * type.
083: */
084: public static byte fromStringForRequired(String string)
085: throws IllegalArgumentException, TypeValueException {
086: if (string == null) {
087: throw new IllegalArgumentException("string == null");
088: } else {
089: try {
090: return Byte.parseByte(string);
091: } catch (NumberFormatException nfe) {
092: throw new TypeValueException(SINGLETON, string);
093: }
094: }
095: }
096:
097: /**
098: * Converts the specified string value to a <code>Byte</code> value.
099: *
100: * @param string
101: * the string to convert, can be <code>null</code>.
102: *
103: * @return
104: * the {@link Byte}, or <code>null</code> if
105: * <code>string == null</code>.
106: *
107: * @throws TypeValueException
108: * if the specified string does not represent a valid value for this
109: * type.
110: */
111: public static Byte fromStringForOptional(String string)
112: throws TypeValueException {
113:
114: if (string == null) {
115: return null;
116: }
117:
118: try {
119: return Byte.valueOf(string);
120: } catch (NumberFormatException nfe) {
121: throw new TypeValueException(SINGLETON, string);
122: }
123: }
124:
125: /**
126: * Converts the specified <code>Byte</code> to a string.
127: *
128: * @param value
129: * the value to convert, can be <code>null</code>.
130: *
131: * @return
132: * the textual representation of the value, or <code>null</code> if and
133: * only if <code>value == null</code>.
134: */
135: public static String toString(Byte value) {
136: if (value == null) {
137: return null;
138: } else {
139: return toString(value.byteValue());
140: }
141: }
142:
143: /**
144: * Converts the specified <code>byte</code> to a string.
145: *
146: * @param value
147: * the value to convert.
148: *
149: * @return
150: * the textual representation of the value, never <code>null</code>.
151: */
152: public static String toString(byte value) {
153:
154: return String.valueOf(value);
155: }
156:
157: protected boolean isValidValueImpl(String value) {
158: try {
159: byte number = Byte.parseByte(value);
160: if (number < _minimum || number > _maximum) {
161: return false;
162: }
163: return true;
164: } catch (NumberFormatException nfe) {
165: return false;
166: }
167: }
168:
169: protected Object fromStringImpl(String string) {
170: return Byte.valueOf(string);
171: }
172:
173: public final String toString(Object value)
174: throws IllegalArgumentException, ClassCastException,
175: TypeValueException {
176: MandatoryArgumentChecker.check("value", value);
177: Byte b = (Byte) value;
178: return b.toString();
179: }
180:
181: public String getDescription() {
182: return "An 8 bits precision integer number.";
183: }
184: }
|