Source for java.lang.Byte

   1: /* Byte.java -- object wrapper for byte
   2:    Copyright (C) 1998, 2001, 2002, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.lang;
  40: 
  41: /**
  42:  * Instances of class <code>Byte</code> represent primitive <code>byte</code>
  43:  * values.
  44:  *
  45:  * Additionally, this class provides various helper functions and variables
  46:  * useful to bytes.
  47:  *
  48:  * @author Paul Fisher
  49:  * @author John Keiser
  50:  * @author Per Bothner
  51:  * @author Eric Blake (ebb9@email.byu.edu)
  52:  * @since 1.1
  53:  * @status updated to 1.5
  54:  */
  55: public final class Byte extends Number implements Comparable
  56: {
  57:   /**
  58:    * Compatible with JDK 1.1+.
  59:    */
  60:   private static final long serialVersionUID = -7183698231559129828L;
  61: 
  62:   /**
  63:    * The minimum value a <code>byte</code> can represent is -128 (or
  64:    * -2<sup>7</sup>).
  65:    */
  66:   public static final byte MIN_VALUE = -128;
  67: 
  68:   /**
  69:    * The maximum value a <code>byte</code> can represent is 127 (or
  70:    * 2<sup>7</sup> - 1).
  71:    */
  72:   public static final byte MAX_VALUE = 127;
  73: 
  74:   /**
  75:    * The primitive type <code>byte</code> is represented by this
  76:    * <code>Class</code> object.
  77:    */
  78:   public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
  79: 
  80:   /**
  81:    * The number of bits needed to represent a <code>byte</code>.
  82:    * @since 1.5
  83:    */
  84:   public static final int SIZE = 8;
  85: 
  86:   // This caches Byte values, and is used by boxing conversions via
  87:   // valueOf().  We're required to cache all possible values here.
  88:   private static Byte[] byteCache = new Byte[MAX_VALUE - MIN_VALUE + 1];
  89: 
  90:   /**
  91:    * The immutable value of this Byte.
  92:    *
  93:    * @serial the wrapped byte
  94:    */
  95:   private final byte value;
  96: 
  97:   /**
  98:    * Create a <code>Byte</code> object representing the value of the
  99:    * <code>byte</code> argument.
 100:    *
 101:    * @param value the value to use
 102:    */
 103:   public Byte(byte value)
 104:   {
 105:     this.value = value;
 106:   }
 107: 
 108:   /**
 109:    * Create a <code>Byte</code> object representing the value specified
 110:    * by the <code>String</code> argument
 111:    *
 112:    * @param s the string to convert
 113:    * @throws NumberFormatException if the String does not contain a byte
 114:    * @see #valueOf(String)
 115:    */
 116:   public Byte(String s)
 117:   {
 118:     value = parseByte(s, 10);
 119:   }
 120: 
 121:   /**
 122:    * Converts the <code>byte</code> to a <code>String</code> and assumes
 123:    * a radix of 10.
 124:    *
 125:    * @param b the <code>byte</code> to convert to <code>String</code>
 126:    * @return the <code>String</code> representation of the argument
 127:    */
 128:   public static String toString(byte b)
 129:   {
 130:     return String.valueOf(b);
 131:   }
 132: 
 133:   /**
 134:    * Converts the specified <code>String</code> into a <code>byte</code>.
 135:    * This function assumes a radix of 10.
 136:    *
 137:    * @param s the <code>String</code> to convert
 138:    * @return the <code>byte</code> value of <code>s</code>
 139:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 140:    *         <code>byte</code>
 141:    * @see #parseByte(String)
 142:    */
 143:   public static byte parseByte(String s)
 144:   {
 145:     return parseByte(s, 10);
 146:   }
 147: 
 148:   /**
 149:    * Converts the specified <code>String</code> into an <code>int</code>
 150:    * using the specified radix (base). The string must not be <code>null</code>
 151:    * or empty. It may begin with an optional '-', which will negate the answer,
 152:    * provided that there are also valid digits. Each digit is parsed as if by
 153:    * <code>Character.digit(d, radix)</code>, and must be in the range
 154:    * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
 155:    * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
 156:    * Unlike Double.parseDouble, you may not have a leading '+'.
 157:    *
 158:    * @param s the <code>String</code> to convert
 159:    * @param radix the radix (base) to use in the conversion
 160:    * @return the <code>String</code> argument converted to <code>byte</code>
 161:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 162:    *         <code>byte</code>
 163:    */
 164:   public static byte parseByte(String s, int radix)
 165:   {
 166:     int i = Integer.parseInt(s, radix, false);
 167:     if ((byte) i != i)
 168:       throw new NumberFormatException();
 169:     return (byte) i;
 170:   }
 171: 
 172:   /**
 173:    * Creates a new <code>Byte</code> object using the <code>String</code>
 174:    * and specified radix (base).
 175:    *
 176:    * @param s the <code>String</code> to convert
 177:    * @param radix the radix (base) to convert with
 178:    * @return the new <code>Byte</code>
 179:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 180:    *         <code>byte</code>
 181:    * @see #parseByte(String, int)
 182:    */
 183:   public static Byte valueOf(String s, int radix)
 184:   {
 185:     return new Byte(parseByte(s, radix));
 186:   }
 187: 
 188:   /**
 189:    * Creates a new <code>Byte</code> object using the <code>String</code>,
 190:    * assuming a radix of 10.
 191:    *
 192:    * @param s the <code>String</code> to convert
 193:    * @return the new <code>Byte</code>
 194:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 195:    *         <code>byte</code>
 196:    * @see #Byte(String)
 197:    * @see #parseByte(String)
 198:    */
 199:   public static Byte valueOf(String s)
 200:   {
 201:     return new Byte(parseByte(s, 10));
 202:   }
 203: 
 204:   /**
 205:    * Returns a <code>Byte</code> object wrapping the value.
 206:    * In contrast to the <code>Byte</code> constructor, this method
 207:    * will cache some values.  It is used by boxing conversion.
 208:    *
 209:    * @param val the value to wrap
 210:    * @return the <code>Byte</code>
 211:    * 
 212:    * @since 1.5
 213:    */
 214:   public static Byte valueOf(byte val)
 215:   {
 216:     synchronized (byteCache)
 217:       {
 218:     if (byteCache[val - MIN_VALUE] == null)
 219:       byteCache[val - MIN_VALUE] = new Byte(val);
 220:     return byteCache[val - MIN_VALUE];
 221:       }
 222:   }
 223: 
 224:  /**
 225:    * Convert the specified <code>String</code> into a <code>Byte</code>.
 226:    * The <code>String</code> may represent decimal, hexadecimal, or
 227:    * octal numbers.
 228:    *
 229:    * <p>The extended BNF grammar is as follows:<br>
 230:    * <pre>
 231:    * <em>DecodableString</em>:
 232:    *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
 233:    *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
 234:    *              | <code>#</code> ) { <em>HexDigit</em> }+ )
 235:    *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
 236:    * <em>DecimalNumber</em>:
 237:    *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
 238:    * <em>DecimalDigit</em>:
 239:    *        <em>Character.digit(d, 10) has value 0 to 9</em>
 240:    * <em>OctalDigit</em>:
 241:    *        <em>Character.digit(d, 8) has value 0 to 7</em>
 242:    * <em>DecimalDigit</em>:
 243:    *        <em>Character.digit(d, 16) has value 0 to 15</em>
 244:    * </pre>
 245:    * Finally, the value must be in the range <code>MIN_VALUE</code> to
 246:    * <code>MAX_VALUE</code>, or an exception is thrown.
 247:    *
 248:    * @param s the <code>String</code> to interpret
 249:    * @return the value of the String as a <code>Byte</code>
 250:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 251:    *         <code>byte</code>
 252:    * @throws NullPointerException if <code>s</code> is null
 253:    * @see Integer#decode(String)
 254:    */
 255:   public static Byte decode(String s)
 256:   {
 257:     int i = Integer.parseInt(s, 10, true);
 258:     if ((byte) i != i)
 259:       throw new NumberFormatException();
 260:     return new Byte((byte) i);
 261:   }
 262: 
 263:   /**
 264:    * Return the value of this <code>Byte</code>.
 265:    *
 266:    * @return the byte value
 267:    */
 268:   public byte byteValue()
 269:   {
 270:     return value;
 271:   }
 272: 
 273:   /**
 274:    * Return the value of this <code>Byte</code> as a <code>short</code>.
 275:    *
 276:    * @return the short value
 277:    */
 278:   public short shortValue()
 279:   {
 280:     return value;
 281:   }
 282: 
 283:   /**
 284:    * Return the value of this <code>Byte</code> as an <code>int</code>.
 285:    *
 286:    * @return the int value
 287:    */
 288:   public int intValue()
 289:   {
 290:     return value;
 291:   }
 292: 
 293:   /**
 294:    * Return the value of this <code>Byte</code> as a <code>long</code>.
 295:    *
 296:    * @return the long value
 297:    */
 298:   public long longValue()
 299:   {
 300:     return value;
 301:   }
 302: 
 303:   /**
 304:    * Return the value of this <code>Byte</code> as a <code>float</code>.
 305:    *
 306:    * @return the float value
 307:    */
 308:   public float floatValue()
 309:   {
 310:     return value;
 311:   }
 312: 
 313:   /**
 314:    * Return the value of this <code>Byte</code> as a <code>double</code>.
 315:    *
 316:    * @return the double value
 317:    */
 318:   public double doubleValue()
 319:   {
 320:     return value;
 321:   }
 322: 
 323:   /**
 324:    * Converts the <code>Byte</code> value to a <code>String</code> and
 325:    * assumes a radix of 10.
 326:    *
 327:    * @return the <code>String</code> representation of this <code>Byte</code>
 328:    * @see Integer#toString()
 329:    */
 330:   public String toString()
 331:   {
 332:     return String.valueOf(value);
 333:   }
 334: 
 335:   /**
 336:    * Return a hashcode representing this Object. <code>Byte</code>'s hash
 337:    * code is simply its value.
 338:    *
 339:    * @return this Object's hash code
 340:    */
 341:   public int hashCode()
 342:   {
 343:     return value;
 344:   }
 345: 
 346:   /**
 347:    * Returns <code>true</code> if <code>obj</code> is an instance of
 348:    * <code>Byte</code> and represents the same byte value.
 349:    *
 350:    * @param obj the object to compare
 351:    * @return whether these Objects are semantically equal
 352:    */
 353:   public boolean equals(Object obj)
 354:   {
 355:     return obj instanceof Byte && value == ((Byte) obj).value;
 356:   }
 357: 
 358:   /**
 359:    * Compare two Bytes numerically by comparing their <code>byte</code> values.
 360:    * The result is positive if the first is greater, negative if the second
 361:    * is greater, and 0 if the two are equal.
 362:    *
 363:    * @param b the Byte to compare
 364:    * @return the comparison
 365:    * @since 1.2
 366:    */
 367:   public int compareTo(Byte b)
 368:   {
 369:     return value - b.value;
 370:   }
 371: 
 372:   /**
 373:    * Behaves like <code>compareTo(Byte)</code> unless the Object
 374:    * is not a <code>Byte</code>.
 375:    *
 376:    * @param o the object to compare
 377:    * @return the comparison
 378:    * @throws ClassCastException if the argument is not a <code>Byte</code>
 379:    * @see #compareTo(Byte)
 380:    * @see Comparable
 381:    * @since 1.2
 382:    */
 383:   public int compareTo(Object o)
 384:   {
 385:     return compareTo((Byte) o);
 386:   }
 387: }