001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import js.Js;
023 
024 /**
025  * <p><b>Internally</b> represents primitive short values, emulating a standard <tt>java.lang</tt> 
026  * interface or class with the same simple name as this one.</p>
027  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
028  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
029  * 
030  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
031  * 
032  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
033  */
034 public final class Short extends Number implements Comparable<Short>
035 {
036     /**
037      * <p>Internally stores a constant holding the minimum value a short can have,-2<sup>15</sup>.</p>
038      * @since 1.0
039      * @javascript Re-compilers must report error on end-users directly using this field.
040      */
041     public static final short MIN_VALUE = (short) 0x8000;
042     /**
043      * <p>Internally stores a constant holding the maximum value a short can have, 2<sup>15</sup>-1.</p>
044      * @since 1.0
045      * @javascript Re-compilers must report error on end-users directly using this field.
046      */
047     public static final short MAX_VALUE = (short) 0x7FFF;
048     /**
049      * <p>Internally stores the number of bits used to represent a short value in two's complement binary form.</p>
050      * @since 1.0
051      * @javascript Re-compilers must report error on end-users directly using this field.
052      */
053     public static final int SIZE = 16;
054 
055     /**
056      * <p>Decodes a string into a {@link Short}.</p>
057      * @param s A string to decode.
058      * @return A {@link Short} represented by <tt>s</tt>.
059      * @throws java.lang.NumberFormatException if the string does not contain a parsable short.
060      * @since 1.0
061      * @javascript Re-compilers must report error on end-users directly using this method. 
062      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
063      */
064     public static final synchronized java.lang.Short decode(java.lang.String s)
065             throws java.lang.NumberFormatException {
066         return Integer.decode(s).shortValue();
067     }
068 
069     /**
070      * <p>Parses the string argument as a signed decimal short.</p>
071      * <p>The characters in the string must all be decimal digits, except that the
072      * first character may be an ASCII minus sign '-' ('-') to indicate a
073      * negative value. The resulting short value is returned, exactly as if the
074      * argument and the radix 10 were given as arguments to the
075      * {@link #parseShort(String, int)} method.</p>
076      * @param s A string containing the short representation to be parsed.
077      * @return The {@link Short} represented by the string argument in decimal.
078      * @throws java.lang.NumberFormatException if the string does not contain a parsable short.
079      * @since 1.0
080      * @javascript Re-compilers must report error on end-users directly using this method. 
081      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
082      */
083     public static final synchronized short parseShort(java.lang.String s)
084             throws java.lang.NumberFormatException {
085         return parseShort(s, 10);
086     }
087 
088     /**
089      * <p>Parses the string argument as a signed short in the radix specified by the second argument.</p>
090      * <p>The characters in the string must all be digits, of the specified radix
091      * (as determined by whether {@link Character#digit(char, int)} returns a nonnegative value) 
092      * except that the first character may be an ASCII minus sign '-' ('-') to indicate a 
093      * negative value. The resulting short value is returned.</p>
094      * @param s A string containing the short representation to be parsed.
095      * @param radix The radix to be used while parsing <tt>s</tt>.
096      * @return The {@link Short} represented by the string argument in the specified radix.
097      * @throws java.lang.NumberFormatException if the string does not contain a parsable short.
098      * @since 1.0
099      * @javascript Re-compilers must report error on end-users directly using this method. 
100      * As a "synchronized" static emulating method, its invocation should be re-compiled into 
101      * in-lined code for efficiency.
102      */
103     public static final synchronized short parseShort(java.lang.String s,
104             int radix) throws java.lang.NumberFormatException {
105         return (short) Integer.parseInt(s, radix);
106     }
107 
108     /**
109      * <p>Returns the value obtained by reversing the order of the bytes in the
110      * two's complement representation of the specified short value.</p>
111      * @param s A short to be converted.
112      * @return The value obtained by reversing (or, equivalently, swapping) the bytes in the specified value.
113      * @since 1.0
114      * @javascript Re-compilers must report error on end-users directly using this method.
115      */
116     public static final short reverseBytes(short s) {
117         return (short) Js.bor(Js.shl(Js.band(s, 255 // 0xff
118                 ), 8), Js.shr(Js.band(s, 65280 // 0xff00
119                 ), 8));
120     }
121 
122     /**
123      * <p>Returns a string representing the specified short. The radix is assumed to be 10.</p>
124      * @param s A short to be converted.
125      * @return The string representation of the specified short.
126      * @since 1.0
127      * @javascript Re-compilers must report error on end-users directly using this method. 
128      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
129      */
130     public static final synchronized java.lang.String toString(short s) {
131         return String.valueOf(s);
132     }
133 
134     /**
135      * <p>Returns a {@link Short} representing the specified short value.</p>
136      * @param s A short value.
137      * @return A {@link Short} representing <tt>s</tt>.
138      * @since 1.0
139      * @javascript Re-compilers must report error on end-users directly using this method. 
140      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
141      */
142     public static final synchronized Short valueOf(short s) {
143         return new Short(s);
144     }
145 
146     /**
147      * <p>Returns a {@link Short} represented by the specified string value.</p>
148      * @param s A string value.
149      * @return A {@link Short} represented by <tt>s</tt>.
150      * @throws java.lang.NumberFormatException if the string does not contain a parsable short.
151      * @since 1.0
152      * @javascript Re-compilers must report error on end-users directly using this method. 
153      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
154      */
155     public static final synchronized Short valueOf(java.lang.String s)
156             throws java.lang.NumberFormatException {
157         return new Short(parseShort(s));
158     }
159 
160     /**
161      * <p>Returns a {@link Short} represented by the specified string value when
162      * parsed with the radix given by the second argument.</p>
163      * @param s A string value.
164      * @param radix The radix to be used in interpreting <tt>s</tt>.
165      * @return A {@link Short} represented by <tt>s</tt> in the specified radix.
166      * @throws java.lang.NumberFormatException if the string does not contain a parsable short.
167      * @since 1.0
168      * @javascript Re-compilers must report error on end-users directly using this method. 
169      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
170      */
171     public static final synchronized Short valueOf(java.lang.String s, int radix)
172             throws java.lang.NumberFormatException {
173         return new Short(parseShort(s, radix));
174     }
175 
176     /**
177      * <p>Allocates a {@link Short} representing the short argument.</p>
178      * @param value A short value.
179      * @since 1.0
180      * @javascript Re-compilers must report error on end-users directly using this constructor. 
181      * A re-compiler simply replaces an invocation of this constructor with its argument.
182      */
183     public Short(short value) {}
184 
185     /**
186      * <p>Allocates a {@link Short} representing the value indicated by the string parameter.</p>
187      * <p>The string is converted to a short value in exactly the manner used by
188      * the {@link #parseShort(String)} method for radix 10.</p>
189      * @param s A string to be converted to a {@link Short}.
190      * @throws NumberFormatException if the string does not contain a parsable short.
191      * @since 1.0
192      * @javascript Re-compilers must report error on end-users directly using this constructor. 
193      * A re-compiler simply replaces an invocation of this constructor with an invocation of {@link #valueOf(String)} passing the string argument.
194      */
195     public Short(java.lang.String s) {}
196 
197     /**
198      * <p>Returns the value of this {@link Short} as a byte primitive.</p>
199      * @return The primitive byte value.
200      * @since 1.0
201      * @javascript Re-compilers must report error on end-users directly using this method. 
202      * A re-compiler simply replaces an invocation of this native method with the current {@link Short} involving rounding or truncation necessarily.
203      */
204     @Override
205     public final native byte byteValue();
206 
207     /**
208      * <p>Returns the value of this {@link Short} as a short primitive.</p>
209      * @return The primitive short value.
210      * @since 1.0
211      * @javascript Re-compilers must report error on end-users directly using this method. 
212      * A re-compiler simply replaces an invocation of this native method with the current {@link Short}.
213      */
214     @Override
215     public final native short shortValue();
216 
217     /**
218      * <p>Returns the value of this {@link Short} as an int primitive.</p>
219      * @return The primitive int value.
220      * @since 1.0
221      * @javascript Re-compilers must report error on end-users directly using this method. 
222      * A re-compiler simply replaces an invocation of this native method with the current {@link Short}.
223      */
224     @Override
225     public final native int intValue();
226 
227     /**
228      * <p>Returns the value of this {@link Short} as a long primitive.</p>
229      * @return The primitive long value.
230      * @since 1.0
231      * @javascript Re-compilers must report error on end-users directly using this method. 
232      * A re-compiler simply replaces an invocation of this native method with the current {@link Short}.
233      */
234     @Override
235     public final native long longValue();
236 
237     /**
238      * <p>Returns the value of this {@link Short} as a float primitive.</p>
239      * @return The primitive float value.
240      * @since 1.0
241      * @javascript Re-compilers must report error on end-users directly using this method. 
242      * A re-compiler simply replaces an invocation of this native method with the current {@link Short}.
243      */
244     @Override
245     public final native float floatValue();
246 
247     /**
248      * <p>Returns the value of this {@link Short} as a double primitive.</p>
249      * @return The primitive double value.
250      * @since 1.0
251      * @javascript Re-compilers must report error on end-users directly using this method. 
252      * A re-compiler simply replaces an invocation of this native method with the current {@link Short}.
253      */
254     @Override
255     public final native double doubleValue();
256 
257     /**
258      * <p>Compares two {@link Short}s numerically.</p>
259      * @param s The {@link Short} to be compared.
260      * @return 0 if this {@link Short} is equal to the argument {@link Short}; a value less than 0 
261      * if this {@link Short} is numerically less than the argument {@link Short}; and a value 
262      * greater than 0 if this {@link Short} is numerically greater than the argument
263      * {@link Short} (signed comparison).
264      * @since 1.0
265      * @javascript Re-compilers must report error on end-users directly using this method. 
266      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
267      */
268     public final int compareTo(Short s) {
269         return ((java.lang.Integer) Js.cond(Js.lt(this, s), -1,
270                 Js.cond(Js.gt(this, s), 1, 0))).intValue();
271     }
272 
273     /**
274      * <p>Compares this {@link Short} to the specified object.</p>
275      * <p>Returns <tt>true</tt> if and only if the argument is not <tt>null</tt> and is a 
276      * {@link Short} that represents the same value as this {@link Short}.</p>
277      * @param o The object to compare with.
278      * @return <tt>true</tt> if the {@link Short}s represent the same value; <tt>false</tt> otherwise.
279      * @since 1.0
280      * @javascript Re-compilers must report error on end-users directly using this method. 
281      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
282      */
283     @Override
284     public final boolean equals(java.lang.Object o) {
285         return Js.eq(this, o);
286     }
287 
288     /**
289      * <p>Returns a hash code for this {@link Short}.</p>
290      * @return A hash code value for this {@link Short}.
291      * @since 1.0
292      * @javascript Re-compilers must report error on end-users directly using this method. 
293      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
294      */
295     @Override
296     public final int hashCode() {
297         return shortValue();
298     }
299 
300     /**
301      * <p>Returns a string representing this {@link Short}'s value.</p>
302      * <p>The value is converted to signed decimal representation and returned as a string, 
303      * exactly as if the short value were given as an argument to the {@link #toString(short)} method.</p>
304      * @return A string representation of this {@link Short} in base 10.
305      * @since 1.0
306      * @javascript Re-compilers must report error on end-users directly using this method.
307      */
308     @Override
309     public final java.lang.String toString() {
310         return toString(shortValue());
311     }
312 }