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 org.jscripter.emu.java.io.Serializable;
023 
024 import js.Js;
025 
026 /**
027  * <p><b>Internally</b> represents primitive boolean values, emulating a standard <tt>java.lang</tt> interface or 
028  * class with the same simple name as this one.</p>
029  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
030  * <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>
031  * 
032  * @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>
033  * 
034  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
035  */
036 public final class Boolean implements Comparable<Boolean>, Serializable
037 {
038     /**
039      * <p>Internally stores the {@link Boolean} corresponding to the primitive value <tt>false</tt>.</p>
040      * @since 1.0
041      * @javascript Re-compilers must report error on end-users directly using this field.
042      */
043     public final static Boolean FALSE = new Boolean(false);
044     /**
045      * <p>Internally stores the {@link Boolean} corresponding to the primitive value <tt>true</tt>.</p>
046      * @since 1.0
047      * @javascript Re-compilers must report error on end-users directly using this field.
048      */
049     public final static Boolean TRUE  = new Boolean(true );
050 
051     /**
052      * <p>Parses the string argument as a boolean.</p>
053      * <p>The boolean returned represents the value <tt>true</tt> if the string argument is 
054      * not <tt>null</tt> and is equal, ignoring case, to the string "true".</p>
055      * @param s A string containing the boolean representation to be parsed.
056      * @return The {@link Boolean} represented by the string argument.
057      * @since 1.0
058      * @javascript Re-compilers must report error on end-users directly using this method.
059      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
060      */
061     public static final synchronized Boolean parseString(java.lang.String s) {
062         return valueOf(s);
063     }
064 
065     /**
066      * <p>Returns a string representing the specified boolean.</p>
067      * <p>If the specified boolean is true, then the string "true" will be returned, otherwise the 
068      * string "false" will be returned.</p>
069      * @param b A boolean to be converted.
070      * @return The string representation of the specified boolean.
071      * @since 1.0
072      * @javascript Re-compilers must report error on end-users directly using this method.
073      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
074      */
075     public static final synchronized java.lang.String toString(boolean b) {
076         return Js.cond(b, "true", "false");
077     }
078 
079     /**
080      * <p>Returns a {@link Boolean} representing the specified boolean value.</p>
081      * @param b A boolean value.
082      * @return A {@link Boolean} representing <tt>b</tt>.
083      * @since 1.0
084      * @javascript Re-compilers must report error on end-users directly using this method.
085      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
086      */
087     public static final synchronized Boolean valueOf(boolean b) {
088         return new Boolean(b);
089     }
090 
091     /**
092      * <p>Returns a {@link Boolean} represented by the specified string value.</p>
093      * @param s A string value.
094      * @return A {@link Boolean} represented by <tt>s</tt>.
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 Boolean valueOf(java.lang.String s) {
100         return new Boolean(s.equalsIgnoreCase("true"));
101     }
102 
103     /**
104      * <p>Allocates a {@link Boolean} representing the boolean argument.</p>
105      * @param value A boolean value.
106      * @since 1.0
107      * @javascript Re-compilers must report error on end-users directly using this constructor.
108      * A re-compiler simply replaces an invocation of this empty constructor with its argument.
109      */
110     public Boolean(boolean value) {
111     }
112 
113     /**
114      * <p>Allocates a {@link Boolean} representing the value <tt>true</tt> if the string argument is 
115      * not <tt>null</tt> and is equal, ignoring case, to the string "true". Otherwise, allocate a 
116      * {@link Boolean} object representing the value <tt>false</tt>.</p>
117      * @param s A string to be converted to a {@link Boolean}.
118      * @since 1.0
119      * @javascript Re-compilers must report error on end-users directly using this constructor.
120      * A re-compiler simply replaces an invocation of this empty constructor with its argument.
121      */
122     public Boolean(java.lang.String s) {
123     }
124 
125     /**
126      * <p>Returns the value of this {@link Boolean} as a boolean primitive.</p>
127      * @return The primitive boolean value.
128      * @since 1.0
129      * @javascript Re-compilers must report error on end-users directly using this method.
130      * A re-compiler simply replaces an invocation of this native method with the current {@link Boolean}.
131      */
132     public final native boolean booleanValue();
133 
134     /**
135      * <p>Compares this {@link Boolean} with another.</p>
136      * @param b The {@link Boolean} to be compared.
137      * @return zero if this {@link Boolean} represents the same boolean value as the argument; a positive 
138      * value if this {@link Boolean} represents <tt>true</tt> and the argument represents <tt>false</tt>; 
139      * and a negative value if this {@link Boolean} represents <tt>false</tt> and the argument 
140      * represents <tt>true</tt>.
141      * @since 1.0
142      * @javascript Re-compilers must report error on end-users directly using this method.
143      */
144     public int compareTo(Boolean b) {
145         return Js.be(this) ? (Js.be(b) ? 0 : 1) : (Js.be(b) ? -1 : 0);
146     }
147 
148     /**
149      * <p>Compares this {@link Boolean} to the specified object.</p>
150      * <p>Returns <tt>true</tt> if and only if the argument is not <tt>null</tt> and is a {@link Boolean} 
151      * that represents the same boolean value as this {@link Boolean}.</p>
152      * @param o The object to compare with.
153      * @return <tt>true</tt> if the {@link Boolean}s represent the same value; <tt>false</tt> otherwise.
154      * @since 1.0
155      * @javascript Re-compilers must report error on end-users directly using this method.
156      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
157      */
158     @Override
159     public boolean equals(java.lang.Object o) {
160         return Js.eq(this, o);
161     }
162 
163     /**
164      * <p>Returns a hash code for this {@link Boolean}.</p>
165      * @return The integer 1231 if this {@link Boolean} represents <tt>true</tt>; it returns the integer 1237 
166      * if this {@link Boolean} represents false.
167      * @since 1.0
168      * @javascript Re-compilers must report error on end-users directly using this method.
169      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
170      */
171     @Override
172     public int hashCode() {
173         return Js.be(this) ? 1231 : 1237;
174     }
175 
176     /**
177      * <p>Returns a string representing this {@link Boolean}'s value.</p>
178      * <p>If this {@link Boolean} represents the value <tt>true</tt>, a string equal to "true" is 
179      * returned. Otherwise, a string equal to "false" is returned.</p>
180      * @return A string representation of this {@link Boolean}.
181      * @since 1.0
182      * @javascript Re-compilers must report error on end-users directly using this method.
183      */
184     @Override
185     public java.lang.String toString() {
186         return toString(booleanValue());
187     }
188 }