Debian Codegraph @

src/java/org/astrogrid/samp/web/Callback.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.List;
4 import java.util.Map;
5 import org.astrogrid.samp.SampMap;
6 import org.astrogrid.samp.SampUtils;
7
8 /**
9 * Map representing a client callback from the hub.
10 * It normally contains a callback method name and a list of parameters.
11 * An instance of this class can be used to correspond to one of the calls
12 * in the {@link org.astrogrid.samp.client.CallableClient} interface.
13 *
14 * @author Mark Taylor
15 * @since 2 Feb 2011
16 */
17 class Callback extends SampMap {
18
19 /** Key for the callback method name (a string). */
20 public static final String METHODNAME_KEY = "samp.methodName";
21
22 /** Key for the callback parameters (a list). */
23 public static final String PARAMS_KEY = "samp.params";
24
25 private static final String[] KNOWN_KEYS = new String[] {
26 METHODNAME_KEY,
27 PARAMS_KEY,
28 };
29
30 /**
31 * Constructs an empty callback.
32 */
33 public Callback() {
34 super( KNOWN_KEYS );
35 }
36
37 /**
38 * Constructs a callback based on an existing map.
39 *
40 * @param map contents
41 */
42 public Callback( Map map ) {
43 this();
44 putAll( map );
45 }
46
47 /**
48 * Constructs a callback given a method name and parameter list.
49 */
50 public Callback( String methodName, List params ) {
51 this();
52 setMethodName( methodName );
53 setParams( params );
54 }
55
56 /**
57 * Sets the method name.
58 *
59 * @param methodName method name
60 */
61 public void setMethodName( String methodName ) {
62 put( METHODNAME_KEY, methodName );
63 }
64
65 /**
66 * Returns the method name.
67 *
68 * @return method name
69 */
70 public String getMethodName() {
71 return getString( METHODNAME_KEY );
72 }
73
74 /**
75 * Sets the parameter list.
76 *
77 * @param params parameter list
78 */
79 public void setParams( List params ) {
80 SampUtils.checkList( params );
81 put( PARAMS_KEY, params );
82 }
83
84 /**
85 * Returns the parameter list.
86 *
87 * @return parameter list
88 */
89 public List getParams() {
90 return getList( PARAMS_KEY );
91 }
92
93 public void check() {
94 super.check();
95 checkHasKeys( new String[] { METHODNAME_KEY, PARAMS_KEY, } );
96 SampUtils.checkString( getMethodName() );
97 SampUtils.checkList( getParams() );
98 }
99
100 /**
101 * Returns a given map as a Callback object.
102 *
103 * @param map map
104 * @return callback
105 */
106 public static Callback asCallback( Map map ) {
107 return ( map instanceof Callback || map == null )
108 ? (Callback) map
109 : new Callback( map );
110 }
111 }