Groovy web console

subscribe to the feed Subscribe
to this
site

Untitled

Published 2 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import static java.lang.Integer.compare;

def _1 = new ByteUnsigned(1)
def _2 = new ByteUnsigned(2)
_1 == _2

public final class ByteUnsigned extends Number implements Comparable<ByteUnsigned> {
    public static final Short MIN_VALUE = 0;
    public static final Short MAX_VALUE = 255;
    public static final ByteUnsigned ZERO = new ByteUnsigned(0);
    private final short value;
    public ByteUnsigned(Byte value) {
        if (0 <= value) {
            this.value = value;
        } else {
            this.value = (short) (0x80 | (value & 0x7F));
        }
    }
    public static ByteUnsigned fromSignedByte(Byte b) {
        if (b < 0) {
            b = 0;
        }

        return new ByteUnsigned((short) b & 0x7F);
    }
    public static ByteUnsigned from_Un_SignedByte(Byte b) {
        return new ByteUnsigned(b);
    }
    public ByteUnsigned(Integer value) {
        if (inRange(value)) {
            this.value = value.shortValue();
        } else {
            throw new NumberFormatException("Value out of range. Value = " + value);
        }
    }
    public ByteUnsigned(Long value) {
        if (inRange(value)) {
            this.value = value.shortValue();
        } else {
            throw new NumberFormatException("Value out of range. Value = " + value);
        }
    }
    public static final ByteUnsigned byteUnsignedZero = new ByteUnsigned(0);
    public ByteUnsigned(String s) throws NumberFormatException {
        this(s, 10);
    }
    public ByteUnsigned(String s, Integer radix) throws NumberFormatException {
        Short prospect = Short.parseShort(s, radix);
        if (inRange(prospect)) {
            this.value = prospect;
        } else {
            throw new NumberFormatException("Value out of range. Value = " + s);
        }
    }
    public static Boolean inRange(Short valueToTest) {
        Boolean result = true;
        result &= (MIN_VALUE <= valueToTest);
        result &= (valueToTest <= MAX_VALUE);
        return result;
    }
    public static Boolean inRange(Integer valueToTest) {
        Boolean result = true;
        result &= (MIN_VALUE <= valueToTest);
        result &= (valueToTest <= MAX_VALUE);
        return result;
    }
    public static Boolean inRange(Long valueToTest) {
        Boolean result = true;
        result &= (MIN_VALUE <= valueToTest);
        result &= (valueToTest <= MAX_VALUE);
        return result;
    }
    public static Boolean inRange(Byte valueToTest) {
        Boolean result = true;
        result &= (MIN_VALUE <= valueToTest);
        result &= (valueToTest <= MAX_VALUE);
        return result;
    }
    @Override
    public int intValue() {
        return value;
    }
    @Override
    public long longValue() {
        return value;
    }
    @Override
    public float floatValue() {
        return value;
    }
    @Override
    public double doubleValue() {
        return value;
    }
    @Override
    public byte byteValue() {
        return (byte) (value & 0xFF);
    }
    public ByteUnsigned shiftRight() {
        byte myByte = byteValue();
        return new ByteUnsigned((myByte & 0xFF) >>> 1);

    }
    public ByteUnsigned shiftLeft() {
        byte myByte = byteValue();
        int temp = (myByte & 0xFF) << 1;
        temp = (temp & 0xFF);
        return new ByteUnsigned(temp);
    }
    public Boolean isSignBitSet() {
        Boolean result;
        result = 0 < (value & 0x80);
        return result;
    }
    @Override
    public int compareTo(ByteUnsigned anotherByte) {
        return compare(this.value, anotherByte.value);
    }
    @Override
    public String toString() {
        return Integer.toString(value) + "(0x" + Integer.toHexString(value).toUpperCase() + ")";
    }
    public String toString(int radix) {
        return Integer.toString(value, radix);
    }
    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ByteUnsigned that = (ByteUnsigned) o;

        return (value & 0xFF) == (that.value & 0xFF);
    }
}