001 
002 /*
003  *  JScripter Standard 1.0 - To Script In 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 js;
021 
022 /**
023  * <p>An <b>opaque</b> class to construct an ID which can be used as a field 
024  * identifier that is globally or locally unique.</p>
025  *
026  * @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>
027  * 
028  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be 
029  * generated into the target codes. Re-compilers must exit with error on the operations of 
030  * accessing that kind of class objects.
031  */
032 
033 public class Id<T> extends Disposable
034 {
035     private static int nextId;
036 
037     private final Var.Mid mid;
038 
039     /**
040      * <p>The typical constructor to create an ID that is locally unique with a 
041      * specified string value.</p>
042      * @param idx The argument string value that uniquely identifies the ID in the 
043      * local context.
044      * @since 1.0
045      * @javascript Re-compilers must replace this constructor with the specified string 
046      * argument as field identifier.
047      */
048     protected Id(String idx) {
049         mid = Var.Members.id(idx);
050     }
051 
052     /**
053      * <p>Re-wraps the argument ID to the desired generic type.</p>
054      * @param idx The argument ID of the original type.
055      * @since 1.0
056      * @javascript Re-compilers must replace this constructor with the the argument ID.
057      */
058     public Id(Id<?> idx) {
059         mid = idx.mid;
060     }
061 
062     /**
063      * <p>Creates an ID that is globally unique.</p>
064      * @since 1.0
065      * @javascript Re-compilers must replace this constructor with a globally unique 
066      * integer as a field identifier.
067      */
068     public Id() {
069         this(Integer.toString(nextId++));
070     }
071 
072     /**
073      * <p>Gets the member ID of the current ID  instance.</p>
074      * @return The member ID of the current ID  instance.
075      * @since 1.0
076      * @javascript Re-compilers must ignore the virtual invocation of this method, that is, 
077      * replacing it with its current instance.
078      */
079     public final Var.Mid mid() {
080         return mid;
081     }
082 
083     /**
084      * <p>Gets the hash code of the current ID  instance.</p>
085      * @return The hash code of the current ID  instance.
086      * @since 1.0
087      * @javascript Re-compilers must replace the virtual invocation of this method with the 
088      * JavaScript expression converted from the Java static invocation:
089      * <pre>System.identityHashCode(this)</pre>.
090      */
091     @Override
092     public final int hashCode() {
093         return mid.hashCode();
094     }
095 
096     /**
097      * <p>Checks if the current ID  instance equals another object.</p>
098      * @param o Any value
099      * @return <tt>true</tt> if the current ID  instance equals <tt>o</tt>; 
100      * false, otherwise.
101      * @since 1.0
102      * @javascript Re-compilers must replace the virtual invocation of this method with the 
103      * JavaScript expression converted from the Java special invocation:
104      * <pre>super.equals(o)</pre>.
105      */
106     @Override
107     public final boolean equals(Object o) {
108         return o.equals(mid);
109     }
110 
111     /**
112      * <p>Gets the string representation of the current ID  instance.</p>
113      * @return The string representation of the current ID  instance.
114      * @since 1.0
115      * @javascript Re-compilers must ignore the virtual invocation of this method, that is, 
116      * replacing it with its current instance.
117      */
118     @Override
119     public final String toString() {
120         return mid.toString();
121     }
122 }