view base/src/java/lang/Integer.d @ 114:46539f5c5993

Added implementation of ResourceBundle.
author kntroh
date Fri, 08 Apr 2011 20:12:20 +0900
parents 9f4c18c268b2
children 536e43f63c81
line wrap: on
line source

module java.lang.Integer;

import java.lang.util;
import java.lang.exceptions;
import java.lang.Number;
import java.lang.Class;
import java.lang.String;

version(Tango){
} else { // Phobos
    static import std.conv;
    static import std.string;
}

version(Tango){
} else { // Phobos
}


class Integer : Number {

    public static const int MIN_VALUE = 0x80000000;
    public static const int MAX_VALUE = 0x7fffffff;
    public static const int SIZE = 32;
    private int value;

    public this ( void* value ){
        super();
        this.value = cast(int)value;
    }
    public this ( int value ){
        super();
        this.value = value;
    }

    public this ( String s ){
        super();
        this.value = parseInt(s);
    }

    public static String toString( int i, int radix ){
        switch( radix ){
            case 2:
                return toBinaryString(i);
            case 8:
                return toOctalString(i);
            case 10:
                return toString(i);
            case 16:
                return toHexString(i);
            default:
                implMissing( __FILE__, __LINE__ );
                return null;
        }
    }

    public static String toHexString( int i ){
        version(Tango){
            return tango.text.convert.Integer.toString(i, "x" );
        } else { // Phobos
            return std.string.format("%x", i);
        }
    }

    public static String toOctalString( int i ){
        version(Tango){
            return tango.text.convert.Integer.toString(i, "o" );
        } else { // Phobos
            return std.string.format("%o", i);
        }
    }

    public static String toBinaryString( int i ){
        version(Tango){
            return tango.text.convert.Integer.toString(i, "b" );
        } else { // Phobos
            return std.string.format("%b", i);
        }
    }

    public static String toString( int i ){
        version(Tango){
            return tango.text.convert.Integer.toString(i);
        } else { // Phobos
            return std.conv.to!(string)( i );
        }
    }

    public static int parseInt( String s, int radix ){
        version(Tango){
            try{
                return tango.text.convert.Integer.toLong( s, radix );
            }
            catch( IllegalArgumentException e ){
                throw new NumberFormatException( e );
            }
        } else { // Phobos
            try{
            	return std.conv.parse!(int)( s, radix );
            }
            catch( std.conv.ConvException e ){
                throw new NumberFormatException( e );
            }
        }
    }

    public static int parseInt( String s ){
        return parseInt( s, 10 );
    }

    public static Integer valueOf( String s, int radix ){
        return new Integer( parseInt( s, radix ));
    }

    public static Integer valueOf( String s ){
        return valueOf( parseInt(s));
    }

    public static Integer valueOf( int i ){
        return new Integer(i);
    }

    public byte byteValue(){
        return cast(byte)value;
    }

    public short shortValue(){
        return cast(short)value;
    }

    public int intValue(){
        return value;
    }

    public long longValue(){
        return cast(long)value;
    }

    public float floatValue(){
        return cast(float)value;
    }

    public double doubleValue(){
        return cast(double)value;
    }

    public override  hash_t toHash(){
        return intValue();
    }

    public override String toString(){
        version(Tango){
            return tango.text.convert.Integer.toString( value );
        } else { // Phobos
            return std.conv.to!(string)(value);
        }
    }

    private static Class TYPE_;
    public static Class TYPE(){
        if( TYPE_ is null ){
            TYPE_ = Class.fromType!(int);
        }
        return TYPE_;
    }

}
alias Integer ValueWrapperInt;