01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.http.rpc.remote;
21 
22 import js.Initializer;
23 import js.Js;
24 import js.ObjectLike;
25 import jsx.Returnable;
26 
27 /**
28  * <p>An abstract base class for call-backs.</p>
29  * <p>Call-backs help client-side asynchronous calls to remote object service based on 
30  * XMLHTTP serialized RPC.</p>
31  * 
32  * @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>
33  */
34 public abstract class Callback<T> implements Returnable<T>
35 {
36     private final static ObjectLike cache = new Initializer().var();
37     private static int nextid = 0;
38 
39     private final String id;
40 
41     /**
42      * <p>The default constructor for a call-back object.</p>
43      * <p>This constructor statically caches itself for finalization in asynchronous mode.</p>
44      * @since 1.0
45      */
46     protected Callback() {
47         id = Js.toString(nextid++);
48         cache.var(id, this);
49     }
50 
51     /**
52      * <p>Performs an action before with the returned value.</p>
53      * <p>The implementing class provides the actions with the returned value.</p>
54      * @param ret The value argument to return.
55      * @since 1.0
56      */
57     protected abstract void onCall(T ret);
58 
59     /**
60      * <p>Returns the specified value.</p>
61      * <p>This implementation simply calls {@link #onCall(Object)} with the return value and 
62      * then removes this call-back object from the global cache to initiate finalization.</p>
63      * @param ret The value argument to return.
64      * @since 1.0
65      */
66     public final void onReturn(T ret) {
67         onCall(ret);
68         cache.delete(id);
69     }
70 
71     /**
72      * <p>Converts a {@link Returnable} to a {@link Callback}.</p>
73      * @param rt The {@link Returnable} object to convert.
74      * @return The newly converted {@link Callback} object.
75      * @since 1.0
76      */
77     public static final <T> Callback<T> create(final Returnable<T> rt) {
78         return new Callback<T>() {
79             @Override
80             protected void onCall(T ret) {
81                 rt.onReturn(ret);
82             }
83         };
84     }
85 }