Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / vector / Setter.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>setValue</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 Setter {
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 /** Setter method name */
51 private String methodName;
52
53 /** Setter method */
54 private Method method=null;
55
56 /** Parameter type of setter method */
57 private Class type;
58
59
60 // ========================== initialization =============================
61 // =======================================================================
62
63
64 /**
65  * Constructs a Setter class based on the configuration.
66  * Note that the
67  * actual initialization is delayed until the first access to the class,
68  * so that if a class is not used, no unnecessary error messages and exceptions
69  * are generated.
70  * @param prefix the configuration prefix to use when reading the configuration
71  * @param protocol the configuration parameter name that contains
72  * the protocol we want to manipulate using a setter method.
73  * The parameter <code>prefix + "." + protocol</code> is read.
74  * @param methodn the configuration parameter name that contains the setter
75  * method name.
76  * The parameter <code>prefix + "." + methodn</code> is read, with the default
77  * value <code>setValue</code>.
78  */
79 public Setter(String prefix, String protocol, String methodn) {
80         
81         this.prefix=prefix;
82         this.protocol=protocol;
83         this.methodn=methodn;
84 }
85
86 // --------------------------------------------------------------------------
87
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,"setValue");
95         // Search the method
96         Class clazz = Network.prototype.getProtocol(pid).getClass();
97         try {
98                 method = GetterSetterFinder.getSetterMethod(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.getSetterType(method);
105 }
106
107
108 // =============================== methods =============================
109 // =====================================================================
110
111
112 /**
113 * @return type of parameter of setter method
114 */
115 public Class getType() {
116
117         init();
118         return type;
119 }
120
121 // --------------------------------------------------------------------------
122
123 /**
124 * @return true if the setter type is long or int
125 */
126 public boolean isInteger() {
127
128         init();
129         return type==long.class || type==int.class;
130 }
131
132 // --------------------------------------------------------------------------
133
134 /**
135 * Sets the given integer value.
136 * @param n The node to set the value on. The protocol is defined
137 * by {@link #pid}.
138 * @param val the value to set.
139 */
140 public void set(Node n, long val) {
141         
142         init();
143         
144         try 
145         {
146                 if(type==long.class)
147                 {
148                         method.invoke(n.getProtocol(pid),val);
149                         return;
150                 }
151                 if(type==int.class)
152                 {
153                         method.invoke(n.getProtocol(pid),(int)val);
154                         return;
155                 }
156         }
157         catch (Exception e)
158         {
159                 throw new RuntimeException("While using setter "+methodName,e);
160                 
161         }
162         
163         throw new RuntimeException("type has to be int or long");
164 }
165
166 // --------------------------------------------------------------------------
167
168 /**
169 * Sets the given real value.
170 * @param n The node to set the value on. The protocol is defined
171 * by {@link #pid}.
172 * @param val the value to set.
173 */
174 public void set(Node n, double val) {
175         
176         init();
177         
178         try
179         {
180                 if(type==double.class)
181                 {
182                         method.invoke(n.getProtocol(pid),val);
183                         return;
184                 }
185                 if(type==float.class)
186                 {
187                         method.invoke(n.getProtocol(pid),(float)val);
188                         return;
189                 }
190         }
191         catch (Exception e)
192         {
193                 throw new RuntimeException("While using setter "+methodName,e);
194         }
195         
196         throw new RuntimeException("type has to be double or float");
197 }
198
199 // --------------------------------------------------------------------------
200
201 /**
202 * Sets the given integer value.
203 * @param i The index of the node to set the value on in the network.
204 * The protocol is defined
205 * by {@link #pid}.
206 * @param val the value to set.
207 */
208 public void set(int i, long val) { set(Network.get(i),val); }
209
210 // --------------------------------------------------------------------------
211
212 /**
213 * Sets the given real value.
214 * @param i The index of the node to set the value on in the network.
215 * The protocol is defined
216 * by {@link #pid}.
217 * @param val the value to set.
218 */
219 public void set(int i, double val) { set(Network.get(i),val); }
220
221 }
222