2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.vector;
21 import java.lang.reflect.*;
22 import peersim.config.*;
23 import peersim.core.*;
26 * Vectors can be written through this class. Typically {@link Control} classes
27 * use this to manipulate vectors.
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).
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.
40 // ============================ fields ===================================
41 // =======================================================================
43 private final String protocol;
44 private final String methodn;
45 private final String prefix;
47 /** Identifier of the protocol that defines the vector */
50 /** Setter method name */
51 private String methodName;
54 private Method method=null;
56 /** Parameter type of setter method */
60 // ========================== initialization =============================
61 // =======================================================================
65 * Constructs a Setter class based on the configuration.
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
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
76 * The parameter <code>prefix + "." + methodn</code> is read, with the default
77 * value <code>setValue</code>.
79 public Setter(String prefix, String protocol, String methodn) {
82 this.protocol=protocol;
86 // --------------------------------------------------------------------------
90 if( method!=null) return;
92 // Read configuration parameter
93 pid = Configuration.getPid(prefix + "." + protocol);
94 methodName = Configuration.getString(prefix+"."+methodn,"setValue");
96 Class clazz = Network.prototype.getProtocol(pid).getClass();
98 method = GetterSetterFinder.getSetterMethod(clazz, methodName);
99 } catch (NoSuchMethodException e) {
100 throw new IllegalParameterException(prefix + "." +
103 // Obtain the type of the field
104 type = GetterSetterFinder.getSetterType(method);
108 // =============================== methods =============================
109 // =====================================================================
113 * @return type of parameter of setter method
115 public Class getType() {
121 // --------------------------------------------------------------------------
124 * @return true if the setter type is long or int
126 public boolean isInteger() {
129 return type==long.class || type==int.class;
132 // --------------------------------------------------------------------------
135 * Sets the given integer value.
136 * @param n The node to set the value on. The protocol is defined
138 * @param val the value to set.
140 public void set(Node n, long val) {
148 method.invoke(n.getProtocol(pid),val);
153 method.invoke(n.getProtocol(pid),(int)val);
159 throw new RuntimeException("While using setter "+methodName,e);
163 throw new RuntimeException("type has to be int or long");
166 // --------------------------------------------------------------------------
169 * Sets the given real value.
170 * @param n The node to set the value on. The protocol is defined
172 * @param val the value to set.
174 public void set(Node n, double val) {
180 if(type==double.class)
182 method.invoke(n.getProtocol(pid),val);
185 if(type==float.class)
187 method.invoke(n.getProtocol(pid),(float)val);
193 throw new RuntimeException("While using setter "+methodName,e);
196 throw new RuntimeException("type has to be double or float");
199 // --------------------------------------------------------------------------
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
206 * @param val the value to set.
208 public void set(int i, long val) { set(Network.get(i),val); }
210 // --------------------------------------------------------------------------
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
217 * @param val the value to set.
219 public void set(int i, double val) { set(Network.get(i),val); }