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>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).
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 /** Getter method name */
51 private String methodName;
54 private Method method = null;
56 /** Parameter type of getter method */
60 // ========================== initialization =============================
61 // =======================================================================
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
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
75 * The parameter <code>prefix + "." + methodn</code> is read, with the default
76 * value <code>getValue</code>.
78 public Getter(String prefix, String protocol, String methodn) {
81 this.protocol=protocol;
85 // --------------------------------------------------------------------------
87 /** Performs actual initialization */
90 if( method!=null) return;
92 // Read configuration parameter
93 pid = Configuration.getPid(prefix + "." + protocol);
94 methodName = Configuration.getString(prefix+"."+methodn,"getValue");
96 Class clazz = Network.prototype.getProtocol(pid).getClass();
98 method = GetterSetterFinder.getGetterMethod(clazz, methodName);
99 } catch (NoSuchMethodException e) {
100 throw new IllegalParameterException(prefix + "." +
103 // Obtain the type of the field
104 type = GetterSetterFinder.getGetterType(method);
108 // =============================== methods =============================
109 // =====================================================================
112 * @return type of return value of getter method
114 public Class getType() {
120 // --------------------------------------------------------------------------
123 * Gets the given value as a Number.
124 * @param n The node to get the value on. The protocol is defined
126 * @return the read value.
128 public Number get(Node n) {
134 Object ret =method.invoke(n.getProtocol(pid));
135 if (ret instanceof Boolean)
136 return ((Boolean) ret) ? 1 : 0;
142 throw new RuntimeException("While using getter "+methodName,e);
146 // --------------------------------------------------------------------------
149 * Gets the given integer value.
150 * @param n The node to get the value on. The protocol is defined
152 * @return the read value.
154 public long getLong(Node n) {
158 if(type==long.class || type==int.class)
163 method.invoke(n.getProtocol(pid))).longValue();
167 throw new RuntimeException(
168 "While using getter "+methodName,e);
171 else throw new RuntimeException("type has to be int or long");
174 // --------------------------------------------------------------------------
177 * Gets the given real value.
178 * @param n The node to get the value on. The protocol is defined
180 * @return the read value.
182 public double getDouble(Node n) {
186 if(type==double.class || type==float.class)
191 method.invoke(n.getProtocol(pid))).doubleValue();
195 throw new RuntimeException(
196 "While using getter "+methodName,e);
199 else throw new RuntimeException(
200 "type has to be double or float");
203 // --------------------------------------------------------------------------
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
210 * @return the read value.
212 public Number get(int i) { return get(Network.get(i)); }
214 // --------------------------------------------------------------------------
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
221 * @return the read value.
223 public long getLong(int i) { return getLong(Network.get(i)); }
225 // --------------------------------------------------------------------------
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
232 * @return the read value.
234 public double getDouble(int i) { return getDouble(Network.get(i)); }