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