001: /*
002: * $Id: Int32.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>_int32</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 Int32 extends Type {
022:
023: /**
024: * The only instance of this class. This field is never <code>null</code>.
025: */
026: public static final Int32 SINGLETON = new Int32();
027:
028: /**
029: * The minimum value that this Int32 can have.
030: */
031: private final int _minimum;
032:
033: /**
034: * The maximum value that this Int32 can have.
035: */
036: private final int _maximum;
037:
038: /**
039: * Constructs a new <code>Int32</code>.
040: * This constructor is private, the field {@link #SINGLETON} should be
041: * used.
042: */
043: private Int32() {
044: this ("_int32", Integer.MIN_VALUE, Integer.MAX_VALUE);
045: }
046:
047: /**
048: * Constructs a new <code>Int32</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 Int32(String name, int minimum, int maximum) {
061: super (name, Integer.class);
062:
063: _minimum = minimum;
064: _maximum = maximum;
065: }
066:
067: /**
068: * Converts the specified non-<code>null</code> string value to an
069: * <code>int</code>.
070: *
071: * @param string
072: * the string to convert, cannot be <code>null</code>.
073: *
074: * @return
075: * the <code>int</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 int fromStringForRequired(String string)
085: throws IllegalArgumentException, TypeValueException {
086: if (string == null) {
087: throw new IllegalArgumentException("string == null");
088: } else {
089: try {
090: return Integer.parseInt(string);
091: } catch (NumberFormatException nfe) {
092: throw new TypeValueException(SINGLETON, string);
093: }
094: }
095: }
096:
097: /**
098: * Converts the specified string value to an <code>Integer</code> value.
099: *
100: * @param string
101: * the string to convert, can be <code>null</code>.
102: *
103: * @return
104: * the {@link Integer}, 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 Integer fromStringForOptional(String string)
112: throws TypeValueException {
113:
114: if (string == null) {
115: return null;
116: }
117:
118: try {
119: return Integer.valueOf(string);
120: } catch (NumberFormatException nfe) {
121: throw new TypeValueException(SINGLETON, string);
122: }
123: }
124:
125: /**
126: * Converts the specified <code>Integer</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(Integer value) {
136: if (value == null) {
137: return null;
138: } else {
139: return toString(value.intValue());
140: }
141: }
142:
143: /**
144: * Converts the specified <code>int</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(int value) {
153: return String.valueOf(value);
154: }
155:
156: protected boolean isValidValueImpl(String value) {
157: try {
158: int number = Integer.parseInt(value);
159: if (number < _minimum || number > _maximum) {
160: return false;
161: }
162: return true;
163: } catch (NumberFormatException nfe) {
164: return false;
165: }
166: }
167:
168: protected Object fromStringImpl(String string) {
169: return Integer.valueOf(string);
170: }
171:
172: public final String toString(Object value)
173: throws IllegalArgumentException, ClassCastException,
174: TypeValueException {
175: MandatoryArgumentChecker.check("value", value);
176: Integer i = (Integer) value;
177: return i.toString();
178: }
179:
180: public String getDescription() {
181: return "A 32 bits precision integer number.";
182: }
183: }
|