1 4 package gnu.kawa.xml; 5 import gnu.math.*; 6 import gnu.bytecode.*; 7 import java.math.*; 8 import gnu.kawa.functions.Arithmetic; 9 10 13 14 public class XIntegerType extends XDataType 15 { 16 18 public final IntNum minValue; 19 21 public final IntNum maxValue; 22 23 static ClassType typeIntNum = ClassType.make("gnu.math.IntNum"); 24 25 public static final XIntegerType integerType = 26 new XIntegerType ("integer", decimalType, INTEGER_TYPE_CODE, 27 null, null); 28 public static final XIntegerType longType = 29 new XIntegerType("long", integerType, LONG_TYPE_CODE, 30 IntNum.make(Long.MIN_VALUE), IntNum.make(Long.MAX_VALUE)); 31 public static final XIntegerType intType = 32 new XIntegerType("int", longType, INT_TYPE_CODE, 33 IntNum.make(Integer.MIN_VALUE), 34 IntNum.make(Integer.MAX_VALUE)); 35 public static final XIntegerType shortType = 36 new XIntegerType("short", intType, SHORT_TYPE_CODE, 37 IntNum.make(Short.MIN_VALUE), 38 IntNum.make(Short.MAX_VALUE)); 39 public static final XIntegerType byteType = 40 new XIntegerType("byte", shortType, BYTE_TYPE_CODE, 41 IntNum.make(Byte.MIN_VALUE), 42 IntNum.make(Byte.MAX_VALUE)); 43 public static final XIntegerType nonPositiveIntegerType = 44 new XIntegerType("nonPositiveInteger", integerType, 45 NON_POSITIVE_INTEGER_TYPE_CODE, 46 null, IntNum.zero()); 47 public static final XIntegerType negativeIntegerType = 48 new XIntegerType("negativeInteger", nonPositiveIntegerType, 49 NEGATIVE_INTEGER_TYPE_CODE, 50 null, IntNum.minusOne()); 51 public static final XIntegerType nonNegativeIntegerType = 52 new XIntegerType("nonNegativeInteger", integerType, 53 NONNEGATIVE_INTEGER_TYPE_CODE, 54 IntNum.zero(), null); 55 public static final XIntegerType unsignedLongType = 56 new XIntegerType("unsignedLong", nonNegativeIntegerType, 57 UNSIGNED_LONG_TYPE_CODE, 58 IntNum.zero(), IntNum.valueOf("18446744073709551615")); 59 public static final XIntegerType unsignedIntType = 60 new XIntegerType("unsignedInt", unsignedLongType, 61 UNSIGNED_INT_TYPE_CODE, 62 IntNum.zero(), IntNum.make(4294967295L)); 63 public static final XIntegerType unsignedShortType = 64 new XIntegerType("unsignedShort", unsignedIntType, 65 UNSIGNED_SHORT_TYPE_CODE, 66 IntNum.zero(), IntNum.make(65535)); 67 public static final XIntegerType unsignedByteType = 68 new XIntegerType("unsignedByte", unsignedShortType, 69 UNSIGNED_BYTE_TYPE_CODE, 70 IntNum.zero(), IntNum.make(255)); 71 public static final XIntegerType positiveIntegerType = 72 new XIntegerType("positiveInteger", nonNegativeIntegerType, 73 POSITIVE_INTEGER_TYPE_CODE, 74 IntNum.one(), null); 75 76 public XIntegerType (String name, XDataType base, int typeCode, 77 IntNum min, IntNum max) 78 { 79 this((Object ) name, base, typeCode, min, max); 81 } 82 83 public XIntegerType (Object name, XDataType base, int typeCode, 84 IntNum min, IntNum max) 85 { 86 super(name, typeIntNum, typeCode); 87 minValue = min; 88 maxValue = max; 89 baseType = base; 90 } 91 92 public boolean isInstance (Object obj) 93 { 94 if (! (obj instanceof IntNum)) 95 return false; 96 if (this == integerType) 97 return true; 98 XDataType objType 99 = (obj instanceof XInteger ? ((XInteger) obj).getIntegerType() 100 : integerType); 101 while (objType != null) 102 { 103 if (objType == this) 104 return true; 105 objType = objType.baseType; 106 } 107 return false; 108 } 109 110 public Object coerceFromObject (Object obj) 111 { 112 return valueOf((IntNum) obj); 113 } 114 115 public IntNum valueOf (IntNum value) 116 { 117 if (this != integerType) 118 { 119 if ((minValue != null && IntNum.compare(value, minValue) < 0) 120 || (maxValue != null && IntNum.compare(value, maxValue) > 0)) 121 throw new ClassCastException ("cannot cast "+value+" to "+name); 122 return new XInteger(value, this); 123 } 124 return value; 125 } 126 127 public Object cast (Object value) 128 { 129 if (value instanceof Boolean ) 130 return valueOf(((Boolean )value).booleanValue() ? IntNum.one() 131 : IntNum.zero()); 132 if (value instanceof IntNum) 133 return valueOf((IntNum) value); 134 if (value instanceof BigDecimal) 135 return valueOf(Arithmetic.asIntNum((BigDecimal) value)); 136 if (value instanceof RealNum) 137 return valueOf(((RealNum) value).toExactInt(RealNum.TRUNCATE)); 138 if (value instanceof Number ) 139 return valueOf(RealNum.toExactInt(((Number ) value).doubleValue(), RealNum.TRUNCATE)); 140 return super.cast(value); 141 } 142 143 public Object valueOf (String value) 144 { 145 return valueOf(IntNum.valueOf(value.trim(), 10)); 146 } 147 148 public IntNum valueOf (String value, int radix) 149 { 150 return valueOf(IntNum.valueOf(value.trim(), radix)); 151 } 152 } 153 | Popular Tags |