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 peersim.core.*;
22 import peersim.dynamics.*;
25 * Sets values in a protocol vector by copying the values of another
27 * The source is defined by {@value #PAR_SOURCE},
28 * and getter method {@value peersim.vector.VectControl#PAR_GETTER}.
30 * This dynamics class can copy any primitive field in the source
31 * protocol to any primitive field in the destination protocol,
32 * provided that the former field is associated with a getter method,
33 * while the latter is associated with a setter method.
37 public class VectCopy extends VectControl implements NodeInitializer {
40 //--------------------------------------------------------------------------
42 //--------------------------------------------------------------------------
45 * The identifier of the protocol to be copied.
46 * The vector values are copied from this vector.
49 private static final String PAR_SOURCE = "source";
52 // --------------------------------------------------------------------------
54 // --------------------------------------------------------------------------
57 private final Getter source;
59 //--------------------------------------------------------------------------
61 //--------------------------------------------------------------------------
64 * Standard constructor that reads the configuration parameters.
65 * Invoked by the simulation engine.
66 * @param prefix the configuration prefix for this class
68 public VectCopy(String prefix)
71 source = new Getter(prefix,PAR_SOURCE,PAR_GETTER);
74 //--------------------------------------------------------------------------
76 //--------------------------------------------------------------------------
79 * Sets values in a protocol vector by copying the values of another
80 * protocol vector. The source is defined by {@value #PAR_SOURCE},
81 * and getter method {@value peersim.vector.VectControl#PAR_GETTER}.
82 * @return always false
84 public boolean execute() {
86 int size = Network.size();
87 for (int i = 0; i < size; i++) {
88 Number ret = source.get(i);
89 if(setter.isInteger()) setter.set(i,ret.longValue());
90 else setter.set(i,ret.doubleValue());
96 //--------------------------------------------------------------------------
99 * Sets the value by copying the value of another
100 * protocol. The source is defined by {@value #PAR_SOURCE},
101 * and getter method {@value peersim.vector.VectControl#PAR_GETTER}.
102 * @param n the node to initialize
104 public void initialize(Node n) {
106 Number ret = source.get(n);
107 if(setter.isInteger()) setter.set(n,ret.longValue());
108 else setter.set(n,ret.doubleValue());
111 //--------------------------------------------------------------------------