java 实现 c#中的Uint32类型

public class Test4 {
    
    public static void main(String[] args) {
        Uint32 min = Uint32.MIN_VALUE;
        Uint32 max = Uint32.MAX_VALUE;
        Uint32 a = new Uint32(4294967295L);
        Uint32 b = new Uint32(0);
        Uint32 c = new Uint32(1);
        
        System.out.println(min.getValue());
        System.out.println(max.getValue());
        System.out.println(a.getValue());
        System.out.println(b.getValue());
        System.out.println(c.getValue());

        test(max);
        
        System.out.println(a.equals(max));
    }
    
    public static void test(Uint32 num) {        
        System.out.println(num == null ? "null" : num.getValue());
    }
}


public class Uint32 {
    
    public final static int SIZE = 32;
    
    private final static long MIN = 0L;
    private final static long MAX = (1L << SIZE) - 1;

    public final static Uint32 MIN_VALUE = new Uint32(MIN);
    public final static Uint32 MAX_VALUE = new Uint32(MAX);
    
    private Long value;
    
    public Uint32() {
        this(0L);
    }
    
    public Uint32(long value) {
        setValue(value);
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        check(value);
        this.value = value & MAX;
    }
    
    private void check(long value) {
        if(value < MIN || value > MAX) {
            throw new IllegalArgumentException(value + " 值必须在 " + MIN + " 到 " + MAX + " 之间");
        }
    }

    @Override
    public int hashCode() {
        return ((Long)value).hashCode();
    }
    
    public String toString() {
        return String.valueOf(value);
    }
    
    public int compareTo(Uint32 obj) {
        if(obj == null) {
            return -1;
        }
        if(this.value == obj.value) {
            return 0;
        }
        return this.value > obj.value ? 1 : 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Uint32 other = (Uint32) obj;
        if (value != other.value)
            return false;
        return true;
    }
}

转自 http://topic.csdn.net/u/20090417/16/b8d051c4-7b78-4eeb-b3b1-295b5ffb7f32.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值