Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / vector / Getter.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.vector;
20
21 import java.lang.reflect.*;
22 import peersim.config.*;
23 import peersim.core.*;
24
25 /**
26  * Vectors can be written through this class. Typically {@link Control} classes
27  * use this to manipulate vectors.
28  * <p>
29  * The method to be used is specified at construction time.
30  * For backward compatibility, if no method is specified, the method
31  * <code>getValue</code> is used. In this way, protocols
32  * implementing the {@link SingleValue} interface can be manipulated using the
33  * old configuration syntax (i.e., without specifying the method).
34  * <p>
35  * Please refer to package {@link peersim.vector} for a detailed description of 
36  * the concept of protocol vector and the role of getters and setters. 
37  */
38 public class Getter {
39
40 // ============================ fields ===================================
41 // =======================================================================
42
43 private final String protocol;
44 private final String methodn;
45 private final String prefix;
46
47 /** Identifier of the protocol that defines the vector */
48 private int pid;
49
50 /** Getter method name */
51 private String methodName;
52
53 /** Getter method */
54 private Method method = null;
55
56 /** Parameter type of getter method */
57 private Class type;
58
59
60 // ========================== initialization =============================
61 // =======================================================================
62
63
64 /**
65  * Constructs a Getter class based on the configuration. Note that the
66  * actual initialization is delayed until the first access to the class,
67  * so that if a class is not used, no unnecessary error messages and exceptions
68  * are generated.
69  * @param prefix the configuration prefix to use when reading the configuration
70  * @param protocol the configuration parameter name that contains
71  * the protocol we want to manipulate using a getter method.
72  * The parameter <code>prefix + "." + protocol</code> is read.
73  * @param methodn the configuration parameter name that contains the getter
74  * method name.
75  * The parameter <code>prefix + "." + methodn</code> is read, with the default
76  * value <code>getValue</code>.
77  */
78 public Getter(String prefix, String protocol, String methodn) {
79
80         this.prefix=prefix;
81         this.protocol=protocol;
82         this.methodn=methodn;
83 }
84
85 // --------------------------------------------------------------------------
86
87 /** Performs actual initialization */
88 private void init() {
89
90         if( method!=null) return;
91
92         // Read configuration parameter
93         pid = Configuration.getPid(prefix + "." + protocol);
94         methodName = Configuration.getString(prefix+"."+methodn,"getValue");
95         // Search the method
96         Class clazz = Network.prototype.getProtocol(pid).getClass();
97         try {
98                 method = GetterSetterFinder.getGetterMethod(clazz, methodName);
99         } catch (NoSuchMethodException e) {
100                 throw new IllegalParameterException(prefix + "." +
101                 methodn, e+"");
102         }
103         // Obtain the type of the field
104         type = GetterSetterFinder.getGetterType(method);
105 }
106
107
108 // =============================== methods =============================
109 // =====================================================================
110
111 /**
112 * @return type of return value of getter method
113 */
114 public Class getType() {
115
116         init();
117         return type;
118 }
119
120 // --------------------------------------------------------------------------
121
122 /**
123 * Gets the given value as a Number.
124 * @param n The node to get the value on. The protocol is defined
125 * by {@link #pid}.
126 * @return the read value.
127 */
128 public Number get(Node n) {
129         
130         init();
131
132         try 
133         {
134                 Object ret =method.invoke(n.getProtocol(pid));
135                 if (ret instanceof Boolean)
136                         return ((Boolean) ret) ? 1 : 0;
137                 else
138                         return (Number) ret;
139         }
140         catch (Exception e)
141         {
142                 throw new RuntimeException("While using getter "+methodName,e);
143         }
144 }
145
146 // --------------------------------------------------------------------------
147
148 /**
149 * Gets the given integer value.
150 * @param n The node to get the value on. The protocol is defined
151 * by {@link #pid}.
152 * @return the read value.
153 */
154 public long getLong(Node n) {
155         
156         init();
157
158         if(type==long.class || type==int.class)
159         {
160                 try 
161                 {
162                         return ((Number)
163                         method.invoke(n.getProtocol(pid))).longValue();
164                 }
165                 catch (Exception e)
166                 {
167                         throw new RuntimeException(
168                         "While using getter "+methodName,e);
169                 }
170         }       
171         else throw new RuntimeException("type has to be int or long");
172 }
173
174 // --------------------------------------------------------------------------
175
176 /**
177 * Gets the given real value.
178 * @param n The node to get the value on. The protocol is defined
179 * by {@link #pid}.
180 * @return the read value.
181 */
182 public double getDouble(Node n) {
183         
184         init();
185
186         if(type==double.class || type==float.class)
187         {
188                 try
189                 {
190                         return ((Number)
191                         method.invoke(n.getProtocol(pid))).doubleValue();
192                 }
193                 catch (Exception e)
194                 {
195                         throw new RuntimeException(
196                         "While using getter "+methodName,e);
197                 }
198         }
199         else throw new RuntimeException(
200                         "type has to be double or float");
201 }
202
203 // --------------------------------------------------------------------------
204
205 /**
206 * Gets the given value as a Number.
207 * @param i The index of the node to get the value on in the network.
208 * The protocol is defined
209 * by {@link #pid}.
210 * @return the read value.
211 */
212 public Number get(int i) { return get(Network.get(i)); }
213
214 // --------------------------------------------------------------------------
215
216 /**
217 * Gets the given integer value.
218 * @param i The index of the node to get the value on in the network.
219 * The protocol is defined
220 * by {@link #pid}.
221 * @return the read value.
222 */
223 public long getLong(int i) { return getLong(Network.get(i)); }
224
225 // --------------------------------------------------------------------------
226
227 /**
228 * Gets the given real value.
229 * @param i The index of the node to get the value on in the network.
230 * The protocol is defined
231 * by {@link #pid}.
232 * @return the read value.
233 */
234 public double getDouble(int i) { return getDouble(Network.get(i)); }
235
236 }
237