jdk/src/share/classes/java/lang/Short.java
author martin
Thu, 13 May 2010 21:56:13 -0700
changeset 5603 682e3deac7ce
parent 3964 cf913644be58
child 5506 202f599c92aa
permissions -rw-r--r--
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity Summary: make sure to grow with size => size * 2 + 2 Reviewed-by: dholmes, chegar, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3288
db82a42da273 6862919: Update copyright year
xdono
parents: 3224
diff changeset
     2
 * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The {@code Short} class wraps a value of primitive type {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * short} in an object.  An object of type {@code Short} contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * single field whose type is {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>In addition, this class provides several methods for converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a {@code short} to a {@code String} and a {@code String} to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * {@code short}, as well as other constants and methods useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * dealing with a {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Nakul Saraiya
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     java.lang.Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class Short extends Number implements Comparable<Short> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * A constant holding the minimum value a {@code short} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * have, -2<sup>15</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final short   MIN_VALUE = -32768;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * A constant holding the maximum value a {@code short} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * have, 2<sup>15</sup>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final short   MAX_VALUE = 32767;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The {@code Class} instance representing the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Returns a new {@code String} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * specified {@code short}. The radix is assumed to be 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param s the {@code short} to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @return the string representation of the specified {@code short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see java.lang.Integer#toString(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static String toString(short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return Integer.toString((int)s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Parses the string argument as a signed {@code short} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * radix specified by the second argument. The characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * string must all be digits, of the specified radix (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * determined by whether {@link java.lang.Character#digit(char,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * int)} returns a nonnegative value) except that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * character may be an ASCII minus sign {@code '-'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * (<code>'&#92;u002D'</code>) to indicate a negative value or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * indicate a positive value.  The resulting {@code short} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>An exception of type {@code NumberFormatException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * thrown if any of the following situations occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <li> The first argument is {@code null} or is a string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * length zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <li> The radix is either smaller than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * java.lang.Character#MIN_RADIX} or larger than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * java.lang.Character#MAX_RADIX}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <li> Any character of the string is not a digit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * specified radix, except that the first character may be a minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * sign {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * string is longer than length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <li> The value represented by the string is not a value of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param s         the {@code String} containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *                  {@code short} representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param radix     the radix to be used while parsing {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return          the {@code short} represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *                  argument in the specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws          NumberFormatException If the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                  does not contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static short parseShort(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int i = Integer.parseInt(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return (short)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Parses the string argument as a signed decimal {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * short}. The characters in the string must all be decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * digits, except that the first character may be an ASCII minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * sign {@code '-'} (<code>'&#92;u002D'</code>) to indicate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * negative value or an ASCII plus sign {@code '+'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * (<code>'&#92;u002B'</code>) to indicate a positive value.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * resulting {@code short} value is returned, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * argument and the radix 10 were given as arguments to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * #parseShort(java.lang.String, int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param s a {@code String} containing the {@code short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return  the {@code short} value represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *          argument in decimal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws  NumberFormatException If the string does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *          contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static short parseShort(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return parseShort(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns a {@code Short} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * extracted from the specified {@code String} when parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * with the radix given by the second argument. The first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * is interpreted as representing a signed {@code short} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * the radix specified by the second argument, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * argument were given to the {@link #parseShort(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * int)} method. The result is a {@code Short} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * represents the {@code short} value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>In other words, this method returns a {@code Short} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *  {@code new Short(Short.parseShort(s, radix))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param s         the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param radix     the radix to be used in interpreting {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return          a {@code Short} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *                  represented by the string argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                  specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws          NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *                  not contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static Short valueOf(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throws NumberFormatException {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   173
        return valueOf(parseShort(s, radix));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns a {@code Short} object holding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * value given by the specified {@code String}. The argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * is interpreted as representing a signed decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * {@code short}, exactly as if the argument were given to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * the {@link #parseShort(java.lang.String)} method. The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * a {@code Short} object that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * {@code short} value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>In other words, this method returns a {@code Short} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *  {@code new Short(Short.parseShort(s))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param s the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return  a {@code Short} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *          represented by the string argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws  NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          not contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static Short valueOf(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return valueOf(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static class ShortCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private ShortCache(){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        static final Short cache[] = new Short[-(-128) + 127 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for(int i = 0; i < cache.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                cache[i] = new Short((short)(i - 128));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns a {@code Short} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * {@code short} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * If a new {@code Short} instance is not required, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * should generally be used in preference to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * {@link #Short(short)}, as this method is likely to yield
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * significantly better space and time performance by caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * frequently requested values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
3224
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2
diff changeset
   222
     * This method will always cache values in the range -128 to 127,
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2
diff changeset
   223
     * inclusive, and may cache other values outside of this range.
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2
diff changeset
   224
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param  s a short value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return a {@code Short} instance representing {@code s}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static Short valueOf(short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        final int offset = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int sAsInt = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return ShortCache.cache[sAsInt + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return new Short(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Decodes a {@code String} into a {@code Short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Accepts decimal, hexadecimal, and octal numbers given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <dt><i>DecodableString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">&sect;3.10.1</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Language Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p>The sequence of characters following an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * "{@code #}", or leading zero) is parsed as by the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Short.parseShort} method with the indicated radix (10, 16, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * 8).  This sequence of characters must represent a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * value or a {@link NumberFormatException} will be thrown.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * result is negated if first character of the specified {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * String} is the minus sign.  No whitespace characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * permitted in the {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param     nm the {@code String} to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return    a {@code Short} object holding the {@code short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *            value represented by {@code nm}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws    NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *            contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see java.lang.Short#parseShort(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static Short decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        int i = Integer.decode(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    "Value " + i + " out of range from input " + nm);
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   285
        return valueOf((short)i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * The value of the {@code Short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private final short value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Constructs a newly allocated {@code Short} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * represents the specified {@code short} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param value     the value to be represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *                  {@code Short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public Short(short value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Constructs a newly allocated {@code Short} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * represents the {@code short} value indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * {@code String} parameter. The string is converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * {@code short} value in exactly the manner used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * {@code parseShort} method for radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param s the {@code String} to be converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *          {@code Short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws  NumberFormatException If the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          does not contain a parsable {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see     java.lang.Short#parseShort(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public Short(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this.value = parseShort(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns the value of this {@code Short} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the value of this {@code Short} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns the value of this {@code Short} as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the value of this {@code Short} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the value of this {@code Short} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns the value of this {@code Short} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns a {@code String} object representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * {@code Short}'s value.  The value is converted to signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * decimal representation and returned as a string, exactly as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * the {@code short} value were given as an argument to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * {@link java.lang.Short#toString(short)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return  a string representation of the value of this object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *          base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public String toString() {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   382
        return Integer.toString((int)value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   386
     * Returns a hash code for this {@code Short}; equal to the result
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   387
     * of invoking {@code intValue()}.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   388
     *
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   389
     * @return a hash code value for this {@code Short}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Compares this object to the specified object.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * {@code null} and is a {@code Short} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * contains the same {@code short} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param obj       the object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return          {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *                  {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (obj instanceof Short) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return value == ((Short)obj).shortValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Compares two {@code Short} objects numerically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param   anotherShort   the {@code Short} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @return  the value {@code 0} if this {@code Short} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *          equal to the argument {@code Short}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *          {@code 0} if this {@code Short} is numerically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *          than the argument {@code Short}; and a value greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *           {@code 0} if this {@code Short} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *           greater than the argument {@code Short} (signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *           comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public int compareTo(Short anotherShort) {
3943
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   426
        return compare(this.value, anotherShort.value);
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   427
    }
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   428
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   429
    /**
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   430
     * Compares two {@code short} values numerically.
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   431
     * The value returned is identical to what would be returned by:
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   432
     * <pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   433
     *    Short.valueOf(x).compareTo(Short.valueOf(y))
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   434
     * </pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   435
     *
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   436
     * @param  x the first {@code short} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   437
     * @param  y the second {@code short} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   438
     * @return the value {@code 0} if {@code x == y};
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   439
     *         a value less than {@code 0} if {@code x < y}; and
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   440
     *         a value greater than {@code 0} if {@code x > y}
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   441
     * @since 1.7
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   442
     */
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   443
    public static int compare(short x, short y) {
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   444
        return x - y;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * The number of bits used to represent a {@code short} value in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * complement binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public static final int SIZE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the value obtained by reversing the order of the bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * two's complement representation of the specified {@code short} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return the value obtained by reversing (or, equivalently, swapping)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *     the bytes in the specified {@code short} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public static short reverseBytes(short i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /** use serialVersionUID from JDK 1.1. for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private static final long serialVersionUID = 7515723908773894738L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
}