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 byte values, emulating a standard <tt>java.lang</tt> interface or 
026  * 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 Byte extends Number implements java.lang.Comparable<Byte>
035 {
036     /**
037      * <p>Internally stores a constant holding the minimum value a byte can have, -2<sup>7</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 byte MIN_VALUE = (byte)0x80;
042     /**
043      * <p>Internally stores a constant holding the maximum value a byte can have, 2<sup>7</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 byte MAX_VALUE = (byte)0x7F;
048     /**
049      * <p>Internally stores the number of bits used to represent a byte 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 = 8;
054 
055     /**
056      * <p>Decodes a string into a {@link Byte}.</p>
057      * @param s A string to decode.
058      * @return A {@link Byte} represented by <tt>s</tt>.
059      * @throws java.lang.NumberFormatException if the string does not contain a parsable byte.
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.Byte decode(java.lang.String s) throws java.lang.NumberFormatException {
065         return Integer.decode(s).byteValue();
066     }
067 
068     /**
069      * <p>Parses the string argument as a signed decimal byte.</p>
070      * <p>The characters in the string must all be decimal digits, except that the first character 
071      * may be an ASCII minus sign '-' ('-') to indicate a negative value. The resulting byte 
072      * value is returned, exactly as if the argument and the radix 10 were given as arguments to 
073      * the {@link #parseByte(String, int)} method.</p>
074      * @param s A string containing the byte representation to be parsed.
075      * @return The {@link Byte} represented by the string argument in decimal.
076      * @throws java.lang.NumberFormatException if the string does not contain a parsable byte.
077      * @since 1.0
078      * @javascript Re-compilers must report error on end-users directly using this method.
079      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
080      */
081     public static final synchronized byte parseByte(java.lang.String s) throws java.lang.NumberFormatException {
082         return parseByte(s, 10);
083     }
084 
085     /**
086      * <p>Parses the string argument as a signed byte in the radix specified by the second argument.</p>
087      * <p>The characters in the string must all be digits, of the specified radix (as determined by 
088      * whether {@link Character#digit(char, int)} returns a nonnegative value) except that the first 
089      * character may be an ASCII minus sign '-' ('-') to indicate a negative value. The resulting
090      * byte value is returned.</p>
091      * @param s A string containing the byte representation to be parsed.
092      * @param radix The radix to be used while parsing <tt>s</tt>.
093      * @return The {@link Byte} represented by the string argument in the specified radix.
094      * @throws java.lang.NumberFormatException if the string does not contain a parsable byte.
095      * @since 1.0
096      * @javascript Re-compilers must report error on end-users directly using this method.
097      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
098      */
099     public static final synchronized byte parseByte(java.lang.String s, int radix)
100             throws java.lang.NumberFormatException {
101         return (byte)Integer.parseInt(s, radix);
102     }
103 
104     /**
105      * <p>Returns a string representing the specified byte. The radix is assumed to be 10.</p>
106      * @param b A byte to be converted.
107      * @return The string representation of the specified byte.
108      * @since 1.0
109      * @javascript Re-compilers must report error on end-users directly using this method.
110      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
111      */
112     public static final synchronized java.lang.String toString(byte b) {
113         return String.valueOf(b);
114     }
115 
116     /**
117      * <p>Returns a {@link Byte} representing the specified byte value.</p>
118      * @param b A byte value.
119      * @return A {@link Byte} representing <tt>b</tt>.
120      * @since 1.0
121      * @javascript Re-compilers must report error on end-users directly using this method.
122      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
123      */
124     public static final synchronized Byte valueOf(byte b) {
125         return new Byte(b);
126     }
127 
128     /**
129      * <p>Returns a {@link Byte} represented by the specified string value.</p>
130      * @param s A string value.
131      * @return A {@link Byte} represented by <tt>s</tt>.
132      * @throws java.lang.NumberFormatException if the string does not contain a parsable byte.
133      * @since 1.0
134      * @javascript Re-compilers must report error on end-users directly using this method.
135      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
136      */
137     public static final synchronized Byte valueOf(java.lang.String s) throws java.lang.NumberFormatException {
138         return new Byte(parseByte(s));
139     }
140 
141     /**
142      * <p>Returns a {@link Byte} represented by the specified string value when parsed with the radix 
143      * given by the second argument.</p>
144      * @param s A string value.
145      * @param radix The radix to be used in interpreting <tt>s</tt>.
146      * @return A {@link Byte} represented by <tt>s</tt> in the specified radix.
147      * @throws java.lang.NumberFormatException if the string does not contain a parsable byte.
148      * @since 1.0
149      * @javascript Re-compilers must report error on end-users directly using this method.
150      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
151      */
152     public static final synchronized Byte valueOf(java.lang.String s, int radix) throws java.lang.NumberFormatException {
153         return new Byte(parseByte(s, radix));
154     }
155 
156     /**
157      * <p>Allocates a {@link Byte} representing the byte argument.</p>
158      * @param value A byte value.
159      * @since 1.0
160      * @javascript Re-compilers must report error on end-users directly using this constructor.
161      * A re-compiler simply replaces an invocation of this empty constructor with its argument.
162      */
163     public Byte(byte value) {
164     }
165 
166     /**
167      * <p>Allocates a {@link Byte} representing the value indicated by the string parameter.</p>
168      * <p>The string is converted to a byte value in exactly the manner used by the {@link #parseByte(String)}
169      * method for radix 10.</p>
170      * @param s A string to be converted to a {@link Byte}.
171      * @throws NumberFormatException if the string does not contain a parsable byte.
172      * @since 1.0
173      * @javascript Re-compilers must report error on end-users directly using this constructor.
174      * A re-compiler simply replaces an invocation of this constructor with an invocation of 
175      * {@link #valueOf(String)} passing the string argument.
176      */
177     public Byte(java.lang.String s) {
178     }
179 
180     /**
181      * <p>Returns the value of this {@link Byte} as a byte primitive.</p>
182      * @return The primitive byte value.
183      * @since 1.0
184      * @javascript Re-compilers must report error on end-users directly using this method.
185      * A re-compiler simply replaces an invocation of this native method with the current {@link Byte}.
186      */
187     @Override
188     public final native byte byteValue();
189     /**
190      * <p>Returns the value of this {@link Byte} as a short primitive.</p>
191      * @return The primitive short value.
192      * @since 1.0
193      * @javascript Re-compilers must report error on end-users directly using this method.
194      * A re-compiler simply replaces an invocation of this native method with the current {@link Byte}.
195      */
196     @Override
197     public final native short shortValue();
198     /**
199      * <p>Returns the value of this {@link Byte} as an int primitive.</p>
200      * @return The primitive int value.
201      * @since 1.0
202      * @javascript Re-compilers must report error on end-users directly using this method.
203      * A re-compiler simply replaces an invocation of this native method with the current {@link Byte}.
204      */
205     @Override
206     public final native int intValue();
207     /**
208      * <p>Returns the value of this {@link Byte} as a long primitive.</p>
209      * @return The primitive long 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 Byte}.
213      */
214     @Override
215     public final native long longValue();
216     /**
217      * <p>Returns the value of this {@link Byte} as a float primitive.</p>
218      * @return The primitive float value.
219      * @since 1.0
220      * @javascript Re-compilers must report error on end-users directly using this method.
221      * A re-compiler simply replaces an invocation of this native method with the current {@link Byte}.
222      */
223     @Override
224     public final native float floatValue();
225     /**
226      * <p>Returns the value of this {@link Byte} as a double primitive.</p>
227      * @return The primitive byte value.
228      * @since 1.0
229      * @javascript Re-compilers must report error on end-users directly using this method.
230      * A re-compiler simply replaces an invocation of this native method with the current {@link Byte}.
231      */
232     @Override
233     public final native double doubleValue();
234 
235     /**
236      * <p>Compares two {@link Byte}s numerically.</p>
237      * @param b The {@link Byte} to be compared.
238      * @return 0 if this {@link Byte} is equal to the argument {@link Byte}; a value less than 0 
239      * if this {@link Byte} is numerically less than the argument {@link Byte}; and a value greater 
240      * than 0 if this {@link Byte} is numerically greater than the argument {@link Byte} (signed comparison).
241      * @since 1.0
242      * @javascript Re-compilers must report error on end-users directly using this method.
243      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
244      */
245     public int compareTo(Byte b) {
246         return ((java.lang.Integer)Js.cond(
247                 Js.lt(this, b),
248                 -1, 
249                 Js.cond(
250                         Js.gt(this, b),
251                         1, 
252                         0
253                 )
254         )).intValue();
255     }
256 
257     /**
258      * <p>Compares this {@link Byte} to the specified object.</p>
259      * <p>Returns <tt>true</tt> if and only if the argument is not <tt>null</tt> and is a {@link Byte} 
260      * that represents the same value as this {@link Byte}.</p>
261      * @param o The object to compare with.
262      * @return <tt>true</tt> if the {@link Byte}s represent the same value; <tt>false</tt> otherwise.
263      * @since 1.0
264      * @javascript Re-compilers must report error on end-users directly using this method.
265      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
266      */
267     @Override
268     public boolean equals(java.lang.Object o) {
269         return Js.eq(this, o);
270     }
271 
272     /**
273      * <p>Returns a hash code for this {@link Byte}.</p>
274      * @return A hash code value for this {@link Byte}.
275      * @since 1.0
276      * @javascript Re-compilers must report error on end-users directly using this method.
277      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
278      */
279     @Override
280     public int hashCode() {
281         return byteValue();
282     }
283 
284     /**
285      * <p>Returns a string representing this {@link Byte}'s value.</p>
286      * <p>The value is converted to signed decimal representation and returned as a string, exactly 
287      * as if the byte value were given as an argument to the {@link #toString(byte)} method.</p>
288      * @return A string representation of this {@link Byte} in base 10.
289      * @since 1.0
290      * @javascript Re-compilers must report error on end-users directly using this method.
291      */
292     @Override
293     public java.lang.String toString() {
294         return toString(byteValue());
295     }
296 }