1 module java.lang.Integer; 2 3 import java.lang.util; 4 import java.lang.exceptions; 5 import java.lang.Number; 6 import java.lang.Class; 7 import java.lang.Character; 8 import java.lang.String; 9 10 version(Tango){ 11 static import tango.text.convert.Integer; 12 } else { // Phobos 13 static import std.conv; 14 static import std..string; 15 } 16 17 18 class Integer : Number { 19 20 public static const int MIN_VALUE = 0x80000000; 21 public static const int MAX_VALUE = 0x7fffffff; 22 public static const int SIZE = 32; 23 private int value; 24 25 public this ( void* value ){ 26 super(); 27 this.value = cast(int)value; 28 } 29 public this ( int value ){ 30 super(); 31 this.value = value; 32 } 33 34 public this ( String s ){ 35 super(); 36 this.value = parseInt(s); 37 } 38 39 public static String toString( int i, int radix ){ 40 if(radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 41 radix = 10; 42 version(Tango){ 43 switch( radix ){ 44 case 10: 45 return tango.text.convert.Integer.toString(i); 46 case 16: 47 return tango.text.convert.Integer.toString(i, "x" ); 48 case 2: 49 return tango.text.convert.Integer.toString(i, "b" ); 50 case 8: 51 return tango.text.convert.Integer.toString(i, "o" ); 52 default: 53 implMissingInTango( __FILE__, __LINE__ ); 54 return null; 55 } 56 } else { // Phobos 57 return std.conv.to!(String)(i, radix); 58 } 59 } 60 61 public static String toHexString( int i ){ 62 return toString(i, 16); 63 } 64 65 public static String toOctalString( int i ){ 66 return toString(i, 8); 67 } 68 69 public static String toBinaryString( int i ){ 70 return toString(i, 2); 71 } 72 73 public static String toString( int i ){ 74 return String_valueOf(i); 75 } 76 77 public static int parseInt( String s, int radix ){ 78 if(radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 79 throw new NumberFormatException("The radix is out of range"); 80 version(Tango){ 81 try{ 82 return tango.text.convert.Integer.toInt( s, radix ); 83 } 84 catch( IllegalArgumentException e ){ 85 throw new NumberFormatException( e ); 86 } 87 } else { // Phobos 88 try{ 89 immutable res = std.conv.parse!(int)( s, radix ); 90 if(s.length) 91 throw new NumberFormatException("String has invalid characters: " ~ s); 92 return res; 93 } 94 catch( std.conv.ConvException e ){ 95 throw new NumberFormatException( e ); 96 } 97 } 98 } 99 100 public static int parseInt( String s ){ 101 return parseInt( s, 10 ); 102 } 103 104 public static Integer valueOf( String s, int radix ){ 105 return new Integer( parseInt( s, radix )); 106 } 107 108 public static Integer valueOf( String s ){ 109 return valueOf(parseInt(s)); 110 } 111 112 public static Integer valueOf( int i ){ 113 return new Integer(i); 114 } 115 116 override 117 public byte byteValue(){ 118 return cast(byte)value; 119 } 120 121 override 122 public short shortValue(){ 123 return cast(short)value; 124 } 125 126 override 127 public int intValue(){ 128 return value; 129 } 130 131 override 132 public long longValue(){ 133 return cast(long)value; 134 } 135 136 override 137 public float floatValue(){ 138 return cast(float)value; 139 } 140 141 override 142 public double doubleValue(){ 143 return cast(double)value; 144 } 145 146 public override hash_t toHash(){ 147 return value; 148 } 149 150 public override String toString(){ 151 return toString(value); 152 } 153 154 private static Class TYPE_; 155 public static Class TYPE(){ 156 if( TYPE_ is null ){ 157 TYPE_ = Class.fromType!(int); 158 } 159 return TYPE_; 160 } 161 162 } 163 alias Integer ValueWrapperInt; 164