1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/Byte.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,452 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +/**
1.32 + *
1.33 + * The {@code Byte} class wraps a value of primitive type {@code byte}
1.34 + * in an object. An object of type {@code Byte} contains a single
1.35 + * field whose type is {@code byte}.
1.36 + *
1.37 + * <p>In addition, this class provides several methods for converting
1.38 + * a {@code byte} to a {@code String} and a {@code String} to a {@code
1.39 + * byte}, as well as other constants and methods useful when dealing
1.40 + * with a {@code byte}.
1.41 + *
1.42 + * @author Nakul Saraiya
1.43 + * @author Joseph D. Darcy
1.44 + * @see java.lang.Number
1.45 + * @since JDK1.1
1.46 + */
1.47 +public final class Byte extends Number implements Comparable<Byte> {
1.48 +
1.49 + /**
1.50 + * A constant holding the minimum value a {@code byte} can
1.51 + * have, -2<sup>7</sup>.
1.52 + */
1.53 + public static final byte MIN_VALUE = -128;
1.54 +
1.55 + /**
1.56 + * A constant holding the maximum value a {@code byte} can
1.57 + * have, 2<sup>7</sup>-1.
1.58 + */
1.59 + public static final byte MAX_VALUE = 127;
1.60 +
1.61 + /**
1.62 + * The {@code Class} instance representing the primitive type
1.63 + * {@code byte}.
1.64 + */
1.65 + public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
1.66 +
1.67 + /**
1.68 + * Returns a new {@code String} object representing the
1.69 + * specified {@code byte}. The radix is assumed to be 10.
1.70 + *
1.71 + * @param b the {@code byte} to be converted
1.72 + * @return the string representation of the specified {@code byte}
1.73 + * @see java.lang.Integer#toString(int)
1.74 + */
1.75 + public static String toString(byte b) {
1.76 + return Integer.toString((int)b, 10);
1.77 + }
1.78 +
1.79 + private static class ByteCache {
1.80 + private ByteCache(){}
1.81 +
1.82 + static final Byte cache[] = new Byte[-(-128) + 127 + 1];
1.83 +
1.84 + static {
1.85 + for(int i = 0; i < cache.length; i++)
1.86 + cache[i] = new Byte((byte)(i - 128));
1.87 + }
1.88 + }
1.89 +
1.90 + /**
1.91 + * Returns a {@code Byte} instance representing the specified
1.92 + * {@code byte} value.
1.93 + * If a new {@code Byte} instance is not required, this method
1.94 + * should generally be used in preference to the constructor
1.95 + * {@link #Byte(byte)}, as this method is likely to yield
1.96 + * significantly better space and time performance since
1.97 + * all byte values are cached.
1.98 + *
1.99 + * @param b a byte value.
1.100 + * @return a {@code Byte} instance representing {@code b}.
1.101 + * @since 1.5
1.102 + */
1.103 + public static Byte valueOf(byte b) {
1.104 + final int offset = 128;
1.105 + return ByteCache.cache[(int)b + offset];
1.106 + }
1.107 +
1.108 + /**
1.109 + * Parses the string argument as a signed {@code byte} in the
1.110 + * radix specified by the second argument. The characters in the
1.111 + * string must all be digits, of the specified radix (as
1.112 + * determined by whether {@link java.lang.Character#digit(char,
1.113 + * int)} returns a nonnegative value) except that the first
1.114 + * character may be an ASCII minus sign {@code '-'}
1.115 + * (<code>'\u002D'</code>) to indicate a negative value or an
1.116 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
1.117 + * indicate a positive value. The resulting {@code byte} value is
1.118 + * returned.
1.119 + *
1.120 + * <p>An exception of type {@code NumberFormatException} is
1.121 + * thrown if any of the following situations occurs:
1.122 + * <ul>
1.123 + * <li> The first argument is {@code null} or is a string of
1.124 + * length zero.
1.125 + *
1.126 + * <li> The radix is either smaller than {@link
1.127 + * java.lang.Character#MIN_RADIX} or larger than {@link
1.128 + * java.lang.Character#MAX_RADIX}.
1.129 + *
1.130 + * <li> Any character of the string is not a digit of the
1.131 + * specified radix, except that the first character may be a minus
1.132 + * sign {@code '-'} (<code>'\u002D'</code>) or plus sign
1.133 + * {@code '+'} (<code>'\u002B'</code>) provided that the
1.134 + * string is longer than length 1.
1.135 + *
1.136 + * <li> The value represented by the string is not a value of type
1.137 + * {@code byte}.
1.138 + * </ul>
1.139 + *
1.140 + * @param s the {@code String} containing the
1.141 + * {@code byte}
1.142 + * representation to be parsed
1.143 + * @param radix the radix to be used while parsing {@code s}
1.144 + * @return the {@code byte} value represented by the string
1.145 + * argument in the specified radix
1.146 + * @throws NumberFormatException If the string does
1.147 + * not contain a parsable {@code byte}.
1.148 + */
1.149 + public static byte parseByte(String s, int radix)
1.150 + throws NumberFormatException {
1.151 + int i = Integer.parseInt(s, radix);
1.152 + if (i < MIN_VALUE || i > MAX_VALUE)
1.153 + throw new NumberFormatException(
1.154 + "Value out of range. Value:\"" + s + "\" Radix:" + radix);
1.155 + return (byte)i;
1.156 + }
1.157 +
1.158 + /**
1.159 + * Parses the string argument as a signed decimal {@code
1.160 + * byte}. The characters in the string must all be decimal digits,
1.161 + * except that the first character may be an ASCII minus sign
1.162 + * {@code '-'} (<code>'\u002D'</code>) to indicate a negative
1.163 + * value or an ASCII plus sign {@code '+'}
1.164 + * (<code>'\u002B'</code>) to indicate a positive value. The
1.165 + * resulting {@code byte} value is returned, exactly as if the
1.166 + * argument and the radix 10 were given as arguments to the {@link
1.167 + * #parseByte(java.lang.String, int)} method.
1.168 + *
1.169 + * @param s a {@code String} containing the
1.170 + * {@code byte} representation to be parsed
1.171 + * @return the {@code byte} value represented by the
1.172 + * argument in decimal
1.173 + * @throws NumberFormatException if the string does not
1.174 + * contain a parsable {@code byte}.
1.175 + */
1.176 + public static byte parseByte(String s) throws NumberFormatException {
1.177 + return parseByte(s, 10);
1.178 + }
1.179 +
1.180 + /**
1.181 + * Returns a {@code Byte} object holding the value
1.182 + * extracted from the specified {@code String} when parsed
1.183 + * with the radix given by the second argument. The first argument
1.184 + * is interpreted as representing a signed {@code byte} in
1.185 + * the radix specified by the second argument, exactly as if the
1.186 + * argument were given to the {@link #parseByte(java.lang.String,
1.187 + * int)} method. The result is a {@code Byte} object that
1.188 + * represents the {@code byte} value specified by the string.
1.189 + *
1.190 + * <p> In other words, this method returns a {@code Byte} object
1.191 + * equal to the value of:
1.192 + *
1.193 + * <blockquote>
1.194 + * {@code new Byte(Byte.parseByte(s, radix))}
1.195 + * </blockquote>
1.196 + *
1.197 + * @param s the string to be parsed
1.198 + * @param radix the radix to be used in interpreting {@code s}
1.199 + * @return a {@code Byte} object holding the value
1.200 + * represented by the string argument in the
1.201 + * specified radix.
1.202 + * @throws NumberFormatException If the {@code String} does
1.203 + * not contain a parsable {@code byte}.
1.204 + */
1.205 + public static Byte valueOf(String s, int radix)
1.206 + throws NumberFormatException {
1.207 + return valueOf(parseByte(s, radix));
1.208 + }
1.209 +
1.210 + /**
1.211 + * Returns a {@code Byte} object holding the value
1.212 + * given by the specified {@code String}. The argument is
1.213 + * interpreted as representing a signed decimal {@code byte},
1.214 + * exactly as if the argument were given to the {@link
1.215 + * #parseByte(java.lang.String)} method. The result is a
1.216 + * {@code Byte} object that represents the {@code byte}
1.217 + * value specified by the string.
1.218 + *
1.219 + * <p> In other words, this method returns a {@code Byte} object
1.220 + * equal to the value of:
1.221 + *
1.222 + * <blockquote>
1.223 + * {@code new Byte(Byte.parseByte(s))}
1.224 + * </blockquote>
1.225 + *
1.226 + * @param s the string to be parsed
1.227 + * @return a {@code Byte} object holding the value
1.228 + * represented by the string argument
1.229 + * @throws NumberFormatException If the {@code String} does
1.230 + * not contain a parsable {@code byte}.
1.231 + */
1.232 + public static Byte valueOf(String s) throws NumberFormatException {
1.233 + return valueOf(s, 10);
1.234 + }
1.235 +
1.236 + /**
1.237 + * Decodes a {@code String} into a {@code Byte}.
1.238 + * Accepts decimal, hexadecimal, and octal numbers given by
1.239 + * the following grammar:
1.240 + *
1.241 + * <blockquote>
1.242 + * <dl>
1.243 + * <dt><i>DecodableString:</i>
1.244 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1.245 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1.246 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1.247 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1.248 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1.249 + * <p>
1.250 + * <dt><i>Sign:</i>
1.251 + * <dd>{@code -}
1.252 + * <dd>{@code +}
1.253 + * </dl>
1.254 + * </blockquote>
1.255 + *
1.256 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1.257 + * are as defined in section 3.10.1 of
1.258 + * <cite>The Java™ Language Specification</cite>,
1.259 + * except that underscores are not accepted between digits.
1.260 + *
1.261 + * <p>The sequence of characters following an optional
1.262 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1.263 + * "{@code #}", or leading zero) is parsed as by the {@code
1.264 + * Byte.parseByte} method with the indicated radix (10, 16, or 8).
1.265 + * This sequence of characters must represent a positive value or
1.266 + * a {@link NumberFormatException} will be thrown. The result is
1.267 + * negated if first character of the specified {@code String} is
1.268 + * the minus sign. No whitespace characters are permitted in the
1.269 + * {@code String}.
1.270 + *
1.271 + * @param nm the {@code String} to decode.
1.272 + * @return a {@code Byte} object holding the {@code byte}
1.273 + * value represented by {@code nm}
1.274 + * @throws NumberFormatException if the {@code String} does not
1.275 + * contain a parsable {@code byte}.
1.276 + * @see java.lang.Byte#parseByte(java.lang.String, int)
1.277 + */
1.278 + public static Byte decode(String nm) throws NumberFormatException {
1.279 + int i = Integer.decode(nm);
1.280 + if (i < MIN_VALUE || i > MAX_VALUE)
1.281 + throw new NumberFormatException(
1.282 + "Value " + i + " out of range from input " + nm);
1.283 + return valueOf((byte)i);
1.284 + }
1.285 +
1.286 + /**
1.287 + * The value of the {@code Byte}.
1.288 + *
1.289 + * @serial
1.290 + */
1.291 + private final byte value;
1.292 +
1.293 + /**
1.294 + * Constructs a newly allocated {@code Byte} object that
1.295 + * represents the specified {@code byte} value.
1.296 + *
1.297 + * @param value the value to be represented by the
1.298 + * {@code Byte}.
1.299 + */
1.300 + public Byte(byte value) {
1.301 + this.value = value;
1.302 + }
1.303 +
1.304 + /**
1.305 + * Constructs a newly allocated {@code Byte} object that
1.306 + * represents the {@code byte} value indicated by the
1.307 + * {@code String} parameter. The string is converted to a
1.308 + * {@code byte} value in exactly the manner used by the
1.309 + * {@code parseByte} method for radix 10.
1.310 + *
1.311 + * @param s the {@code String} to be converted to a
1.312 + * {@code Byte}
1.313 + * @throws NumberFormatException If the {@code String}
1.314 + * does not contain a parsable {@code byte}.
1.315 + * @see java.lang.Byte#parseByte(java.lang.String, int)
1.316 + */
1.317 + public Byte(String s) throws NumberFormatException {
1.318 + this.value = parseByte(s, 10);
1.319 + }
1.320 +
1.321 + /**
1.322 + * Returns the value of this {@code Byte} as a
1.323 + * {@code byte}.
1.324 + */
1.325 + public byte byteValue() {
1.326 + return value;
1.327 + }
1.328 +
1.329 + /**
1.330 + * Returns the value of this {@code Byte} as a
1.331 + * {@code short}.
1.332 + */
1.333 + public short shortValue() {
1.334 + return (short)value;
1.335 + }
1.336 +
1.337 + /**
1.338 + * Returns the value of this {@code Byte} as an
1.339 + * {@code int}.
1.340 + */
1.341 + public int intValue() {
1.342 + return (int)value;
1.343 + }
1.344 +
1.345 + /**
1.346 + * Returns the value of this {@code Byte} as a
1.347 + * {@code long}.
1.348 + */
1.349 + public long longValue() {
1.350 + return (long)value;
1.351 + }
1.352 +
1.353 + /**
1.354 + * Returns the value of this {@code Byte} as a
1.355 + * {@code float}.
1.356 + */
1.357 + public float floatValue() {
1.358 + return (float)value;
1.359 + }
1.360 +
1.361 + /**
1.362 + * Returns the value of this {@code Byte} as a
1.363 + * {@code double}.
1.364 + */
1.365 + public double doubleValue() {
1.366 + return (double)value;
1.367 + }
1.368 +
1.369 + /**
1.370 + * Returns a {@code String} object representing this
1.371 + * {@code Byte}'s value. The value is converted to signed
1.372 + * decimal representation and returned as a string, exactly as if
1.373 + * the {@code byte} value were given as an argument to the
1.374 + * {@link java.lang.Byte#toString(byte)} method.
1.375 + *
1.376 + * @return a string representation of the value of this object in
1.377 + * base 10.
1.378 + */
1.379 + public String toString() {
1.380 + return Integer.toString((int)value);
1.381 + }
1.382 +
1.383 + /**
1.384 + * Returns a hash code for this {@code Byte}; equal to the result
1.385 + * of invoking {@code intValue()}.
1.386 + *
1.387 + * @return a hash code value for this {@code Byte}
1.388 + */
1.389 + public int hashCode() {
1.390 + return (int)value;
1.391 + }
1.392 +
1.393 + /**
1.394 + * Compares this object to the specified object. The result is
1.395 + * {@code true} if and only if the argument is not
1.396 + * {@code null} and is a {@code Byte} object that
1.397 + * contains the same {@code byte} value as this object.
1.398 + *
1.399 + * @param obj the object to compare with
1.400 + * @return {@code true} if the objects are the same;
1.401 + * {@code false} otherwise.
1.402 + */
1.403 + public boolean equals(Object obj) {
1.404 + if (obj instanceof Byte) {
1.405 + return value == ((Byte)obj).byteValue();
1.406 + }
1.407 + return false;
1.408 + }
1.409 +
1.410 + /**
1.411 + * Compares two {@code Byte} objects numerically.
1.412 + *
1.413 + * @param anotherByte the {@code Byte} to be compared.
1.414 + * @return the value {@code 0} if this {@code Byte} is
1.415 + * equal to the argument {@code Byte}; a value less than
1.416 + * {@code 0} if this {@code Byte} is numerically less
1.417 + * than the argument {@code Byte}; and a value greater than
1.418 + * {@code 0} if this {@code Byte} is numerically
1.419 + * greater than the argument {@code Byte} (signed
1.420 + * comparison).
1.421 + * @since 1.2
1.422 + */
1.423 + public int compareTo(Byte anotherByte) {
1.424 + return compare(this.value, anotherByte.value);
1.425 + }
1.426 +
1.427 + /**
1.428 + * Compares two {@code byte} values numerically.
1.429 + * The value returned is identical to what would be returned by:
1.430 + * <pre>
1.431 + * Byte.valueOf(x).compareTo(Byte.valueOf(y))
1.432 + * </pre>
1.433 + *
1.434 + * @param x the first {@code byte} to compare
1.435 + * @param y the second {@code byte} to compare
1.436 + * @return the value {@code 0} if {@code x == y};
1.437 + * a value less than {@code 0} if {@code x < y}; and
1.438 + * a value greater than {@code 0} if {@code x > y}
1.439 + * @since 1.7
1.440 + */
1.441 + public static int compare(byte x, byte y) {
1.442 + return x - y;
1.443 + }
1.444 +
1.445 + /**
1.446 + * The number of bits used to represent a {@code byte} value in two's
1.447 + * complement binary form.
1.448 + *
1.449 + * @since 1.5
1.450 + */
1.451 + public static final int SIZE = 8;
1.452 +
1.453 + /** use serialVersionUID from JDK 1.1. for interoperability */
1.454 + private static final long serialVersionUID = -7183698231559129828L;
1.455 +}