Subversion Repositories HomeAutomation

Rev

Rev 641 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * File created by Jimmy
3
 * at 2003-sep-06 18:24:50
4
 */
5
package Macbeth.Utilities;
6
 
7
/**
8
 * An unsigned byte representation. It uses a signed byte
9
 * for the actual storage, but when converted to short and
10
 * string, the data will be shown as unsigned, resulting
11
 * in the output range [0,255] instead of [-128,127]
12
 * @author Jimmy
13
 */
14
public class UByte {
15
    //The actual byte value (signed)
16
    private byte byteVal;
17
 
18
    /**
19
     * Creates a new instance of UByte using the specified
20
     * (signed) initial value.
21
     * @param b The initial byte value (signed).
22
     */
23
    public UByte(byte b) {
24
        byteVal = b;
25
    }
26
 
27
    /**
28
     * Creates a new instance of UByte.
29
     */
30
    public UByte() {
31
        this((byte)0);
32
    }
33
 
34
    /**
35
     * Sets the signed byte value.
36
     * @param b The signed byte value.
37
     */
38
    public void setByteValue(byte b) {
39
        byteVal = b;
40
    }
41
 
42
    /**
43
     * Creates an unsigned byte from a short value.
44
     * @param s The short value. Must be within the 0-255 range, or else a NumberFormatException will be thrown.
45
     * @return The unsigned byte.
46
     */
47
    public static UByte fromShort(short s) {
48
        //If s>255 then return the short - 256 (cast to a byte).  Else
49
        //just return s (cast to a byte).
50
        return ((s>255) ? (new UByte(((byte)(s-256)))) : (new UByte((byte)s)));
51
    }
52
 
53
    /**
54
     * Parses an unsigned byte from a string.
55
     * @param str The string to parse from. If this isn't in range 0-255, a NumberFormatException will be thrown.
56
     * @return The UByte.
57
     * @throws NumberFormatException When string couldn't be parsed as a UByte within the range 0-255.
58
     */
59
    public static UByte parseUByte(String str) throws NumberFormatException {
60
        short s = Short.parseShort(str);
61
        return fromShort(s);
62
    }
63
 
64
    /**
65
     * Gets the unsigned short representation of this byte.
66
     * @return The unsigned short value. It will be within the 0-255 range.
67
     */
68
    public short shortValue() {
69
        //If b<0 (ie negative) then return b+256 (cast to a short).  Else
70
        //b is positive, so just return b (cast to a short).
71
        return ((byteVal<0) ? (short)(byteVal+256) : (short)byteVal);
72
    }
73
 
74
    /**
75
     * Gets the signed byte value.
76
     * @return The signed byte value.
77
     */
78
    public byte byteValue() {
79
        return byteVal;
80
    }
81
 
82
    /**
83
     * Gets a string showing the unsigned short representation of this byte.
84
     * @return The unsigned short representation as a String.
85
     */
86
    public String toString() {
87
        short s = shortValue();
88
        return Short.toString(s);
89
    }
90
 
91
    /**
92
     * Gets a string showing the unsigned short representation of a byte.
93
     * @param b The byte.
94
     * @return The unsigned short representation as a String.
95
     */
96
    public static String toString(byte b) {
97
        return Short.toString((new UByte(b)).shortValue());
98
    }
99
 
100
    /**
101
     * Checks whether an object is equal to this UByte.
102
     * @param o The Object to compare with.
103
     * @return True if equal, otherwise false.
104
     */
105
    public boolean equals(Object o)
106
    {
107
        //must be instance of this class, and must not be null
108
        if (o==null || !(o instanceof UByte)) {
109
          return false;
110
        }
111
        // Now compare data fields...
112
        return ((UByte)o).byteValue()==this.byteValue();
113
    }
114
 
115
    /**
116
     * Returns this object's hash code.
117
     * @return The hash code.
118
     */
119
    public int hashCode() {
120
        return (int)byteValue();
121
    }
122
}