001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: /**
019: * Wraps native <code>byte</code> as an object.
020: */
021: public final class Byte extends Number implements Comparable<Byte> {
022:
023: public static final byte MIN_VALUE = (byte) 0x80;
024: public static final byte MAX_VALUE = (byte) 0x7F;
025: public static final int SIZE = 8;
026:
027: // Box all values according to JLS
028: private static Byte[] boxedValues = new Byte[256];
029:
030: public static Byte decode(String s) throws NumberFormatException {
031: return new Byte((byte) __decodeAndValidateLong(s, MIN_VALUE,
032: MAX_VALUE));
033: }
034:
035: /**
036: * @skip
037: *
038: * Here for shared implementation with Arrays.hashCode
039: */
040: public static int hashCode(byte b) {
041: return b;
042: }
043:
044: public static byte parseByte(String s) throws NumberFormatException {
045: final int baseTen = 10;
046: return parseByte(s, baseTen);
047: }
048:
049: public static byte parseByte(String s, int radix)
050: throws NumberFormatException {
051: return (byte) __parseAndValidateLong(s, radix, MIN_VALUE,
052: MAX_VALUE);
053: }
054:
055: public static String toString(byte b) {
056: return String.valueOf(b);
057: }
058:
059: public static Byte valueOf(byte b) {
060: int rebase = b + 128;
061: if (boxedValues[rebase] == null) {
062: boxedValues[rebase] = new Byte(b);
063: }
064: return boxedValues[rebase];
065: }
066:
067: public static Byte valueOf(String s) throws NumberFormatException {
068: return new Byte(Byte.parseByte(s));
069: }
070:
071: public static Byte valueOf(String s, int radix)
072: throws NumberFormatException {
073: return new Byte(Byte.parseByte(s, radix));
074: }
075:
076: private final transient byte value;
077:
078: public Byte(byte value) {
079: this .value = value;
080: }
081:
082: public Byte(String s) {
083: value = parseByte(s);
084: }
085:
086: @Override
087: public byte byteValue() {
088: return value;
089: }
090:
091: public int compareTo(Byte b) {
092: if (value < b.value) {
093: return -1;
094: } else if (value > b.value) {
095: return 1;
096: } else {
097: return 0;
098: }
099: }
100:
101: @Override
102: public double doubleValue() {
103: return value;
104: }
105:
106: @Override
107: public boolean equals(Object o) {
108: return (o instanceof Byte) && (((Byte) o).value == value);
109: }
110:
111: @Override
112: public float floatValue() {
113: return value;
114: }
115:
116: @Override
117: public int hashCode() {
118: return hashCode(value);
119: }
120:
121: @Override
122: public int intValue() {
123: return value;
124: }
125:
126: @Override
127: public long longValue() {
128: return value;
129: }
130:
131: @Override
132: public short shortValue() {
133: return value;
134: }
135:
136: @Override
137: public String toString() {
138: return toString(value);
139: }
140: }
|