001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: /**
021: * Byte is the wrapper for the primitive type <code>byte</code>.
022: *
023: * @since 1.1
024: */
025: public final class Byte extends Number implements Comparable<Byte> {
026:
027: private static final long serialVersionUID = -7183698231559129828L;
028:
029: /**
030: * The value which the receiver represents.
031: */
032: private final byte value;
033:
034: /**
035: * Constant for the maximum <code>byte</code> value, 2<sup>7</sup>-1.
036: */
037: public static final byte MAX_VALUE = (byte) 0x7F;
038:
039: /**
040: * Constant for the minimum <code>byte</code> value, -2<sup>7</sup>.
041: */
042: public static final byte MIN_VALUE = (byte) 0x80;
043:
044: /**
045: * Constant for the number of bits to represent a <code>byte</code> in
046: * two's compliment form.
047: *
048: * @since 1.5
049: */
050: public static final int SIZE = 8;
051:
052: /**
053: * The java.lang.Class that represents this class.
054: */
055: @SuppressWarnings("unchecked")
056: public static final Class<Byte> TYPE = (Class<Byte>) new byte[0]
057: .getClass().getComponentType();
058:
059: // Note: This can't be set to "byte.class", since *that* is
060: // defined to be "java.lang.Byte.TYPE";
061:
062: /**
063: * A cache of instances used by {@link #valueOf(byte)} and auto-boxing.
064: */
065: private static final Byte[] CACHE = new Byte[256];
066:
067: /**
068: * Constructs a new instance of the receiver which represents the byte
069: * valued argument.
070: *
071: * @param value
072: * the byte to store in the new instance.
073: */
074: public Byte(byte value) {
075: this .value = value;
076: }
077:
078: /**
079: * Constructs a new instance of this class given a string.
080: *
081: * @param string
082: * a string representation of a single byte quantity.
083: * @throws NumberFormatException
084: * if the argument could not be parsed as a byte quantity.
085: */
086: public Byte(String string) throws NumberFormatException {
087: this (parseByte(string));
088: }
089:
090: /**
091: * Answers the byte value which the receiver represents
092: *
093: * @return byte the value of the receiver.
094: */
095: @Override
096: public byte byteValue() {
097: return value;
098: }
099:
100: /**
101: * Compares this <code>Byte</code> to the <code>Byte</code> passed. If
102: * this instance's value is equal to the value of the instance passed, then
103: * 0 is returned. If this instance's value is less than the value of the
104: * instance passed, then a negative value is returned. If this instance's
105: * value is greater than the value of the instance passed, then a positive
106: * value is returned.
107: *
108: * @param object
109: * The instance to compare to.
110: * @throws NullPointerException
111: * if <code>object</code> is <code>null</code>.
112: * @since 1.2
113: */
114: public int compareTo(Byte object) {
115: return value > object.value ? 1 : (value < object.value ? -1
116: : 0);
117: }
118:
119: /**
120: * Parses the string argument as if it was a byte value and returns the
121: * result. It is an error if the received string does not contain a
122: * representation of a single byte quantity. The string may be a hexadecimal
123: * ("0x..."), octal ("0..."), or decimal ("...") representation of a byte.
124: *
125: * @param string
126: * a string representation of a single byte quantity.
127: * @return Byte the value represented by the argument
128: * @throws NumberFormatException
129: * if the argument could not be parsed as a byte quantity.
130: */
131: public static Byte decode(String string)
132: throws NumberFormatException {
133: int intValue = Integer.decode(string).intValue();
134: byte result = (byte) intValue;
135: if (result == intValue) {
136: return valueOf(result);
137: }
138: throw new NumberFormatException();
139: }
140:
141: /**
142: * Answers the double value which the receiver represents
143: *
144: * @return double the value of the receiver.
145: */
146: @Override
147: public double doubleValue() {
148: return value;
149: }
150:
151: /**
152: * Compares the argument to the receiver, and answers true if they represent
153: * the <em>same</em> object using a class specific comparison.
154: * <p>
155: * In this case, the argument must also be a Byte, and the receiver and
156: * argument must represent the same byte value.
157: * </p>
158: *
159: * @param object
160: * the object to compare with this object
161: * @return <code>true</code> if the object is the same as this object
162: * <code>false</code> if it is different from this object
163: * @see #hashCode
164: */
165: @Override
166: public boolean equals(Object object) {
167: return (object == this ) || (object instanceof Byte)
168: && (value == ((Byte) object).value);
169: }
170:
171: /**
172: * Answers the float value which the receiver represents
173: *
174: * @return float the value of the receiver.
175: */
176: @Override
177: public float floatValue() {
178: return value;
179: }
180:
181: /**
182: * Answers an integer hash code for the receiver. Any two objects which
183: * answer <code>true</code> when passed to <code>equals</code> must
184: * answer the same value for this method.
185: *
186: * @return the receiver's hash
187: * @see #equals
188: */
189: @Override
190: public int hashCode() {
191: return value;
192: }
193:
194: /**
195: * Answers the int value which the receiver represents
196: *
197: * @return int the value of the receiver.
198: */
199: @Override
200: public int intValue() {
201: return value;
202: }
203:
204: /**
205: * Answers the long value which the receiver represents
206: *
207: * @return long the value of the receiver.
208: */
209: @Override
210: public long longValue() {
211: return value;
212: }
213:
214: /**
215: * Parses the string argument as if it was a byte value and returns the
216: * result. Throws NumberFormatException if the string does not represent a
217: * single byte quantity.
218: *
219: * @param string
220: * a string representation of a single byte quantity.
221: * @return byte the value represented by the argument
222: * @throws NumberFormatException
223: * if the argument could not be parsed as a byte quantity.
224: */
225: public static byte parseByte(String string)
226: throws NumberFormatException {
227: int intValue = Integer.parseInt(string);
228: byte result = (byte) intValue;
229: if (result == intValue) {
230: return result;
231: }
232: throw new NumberFormatException();
233: }
234:
235: /**
236: * Parses the string argument as if it was a byte value and returns the
237: * result. Throws NumberFormatException if the string does not represent a
238: * single byte quantity. The second argument specifies the radix to use when
239: * parsing the value.
240: *
241: * @param string
242: * a string representation of a single byte quantity.
243: * @param radix
244: * the radix to use when parsing.
245: * @return byte the value represented by the argument
246: * @throws NumberFormatException
247: * if the argument could not be parsed as a byte quantity.
248: */
249: public static byte parseByte(String string, int radix)
250: throws NumberFormatException {
251: int intValue = Integer.parseInt(string, radix);
252: byte result = (byte) intValue;
253: if (result == intValue) {
254: return result;
255: }
256: throw new NumberFormatException();
257: }
258:
259: /**
260: * Answers the short value which the receiver represents
261: *
262: * @return short the value of the receiver.
263: */
264: @Override
265: public short shortValue() {
266: return value;
267: }
268:
269: /**
270: * Answers a string containing a concise, human-readable description of the
271: * receiver.
272: *
273: * @return a printable representation for the receiver.
274: */
275: @Override
276: public String toString() {
277: return Integer.toString(value);
278: }
279:
280: /**
281: * Answers a string containing a concise, human-readable description of the
282: * argument.
283: *
284: * @param value
285: * byte the byte to convert.
286: * @return String a printable representation for the byte.
287: */
288: public static String toString(byte value) {
289: return Integer.toString(value);
290: }
291:
292: /**
293: * Parses the string argument as if it was a byte value and returns a Byte
294: * representing the result. Throws NumberFormatException if the string
295: * cannot be parsed as a byte quantity.
296: *
297: * @param string
298: * a string representation of a single byte quantity.
299: * @return Byte the value represented by the argument
300: * @throws NumberFormatException
301: * if the argument could not be parsed as a byte quantity.
302: */
303: public static Byte valueOf(String string)
304: throws NumberFormatException {
305: return valueOf(parseByte(string));
306: }
307:
308: /**
309: * Parses the string argument as if it was a byte value and returns a Byte
310: * representing the result. Throws NumberFormatException if the string
311: * cannot be parsed as a byte quantity. The second argument specifies the
312: * radix to use when parsing the value.
313: *
314: * @param string
315: * a string representation of a single byte quantity.
316: * @param radix
317: * the radix to use when parsing.
318: * @return Byte the value represented by the argument
319: * @throws NumberFormatException
320: * if the argument could not be parsed as a byte quantity.
321: */
322: public static Byte valueOf(String string, int radix)
323: throws NumberFormatException {
324: return valueOf(parseByte(string, radix));
325: }
326:
327: /**
328: * Returns a <code>Byte</code> instance for the <code>byte</code> value
329: * passed. This method is preferred over the constructor, as this method may
330: * maintain a cache of instances.
331: *
332: * @param b
333: * The byte value.
334: * @return A <code>Byte</code> instance.
335: * @since 1.5
336: */
337: public static Byte valueOf(byte b) {
338: synchronized (CACHE) {
339: int idx = b - MIN_VALUE;
340: Byte result = CACHE[idx];
341: return (result == null ? CACHE[idx] = new Byte(b) : result);
342: }
343: }
344: }
|