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.config.*;
22 import peersim.core.*;
25 * Initializes a protocol vector with values in the range [{@value #PAR_MIN},
26 * {@value #PAR_MAX}] (inclusive both ends), linearly increasing.
30 public class LinearDistribution extends VectControl
33 //--------------------------------------------------------------------------
35 //--------------------------------------------------------------------------
38 * Upper end of the interval..
41 private static final String PAR_MAX = "max";
44 * Lower end of the interval. Defaults to -{@value #PAR_MAX}.
47 private static final String PAR_MIN = "min";
49 // --------------------------------------------------------------------------
51 // --------------------------------------------------------------------------
54 private final Number min;
57 private final Number max;
60 private final double step;
62 // --------------------------------------------------------------------------
64 // --------------------------------------------------------------------------
67 * Standard constructor that reads the configuration parameters.
68 * Invoked by the simulation engine.
69 * @param prefix the configuration prefix for this class
71 public LinearDistribution(String prefix)
75 // Read parameters based on type
76 if (setter.isInteger()) {
77 max=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MAX));
78 min=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MIN,
80 step= (max.longValue()-min.longValue())/
81 ((double)(Network.size()-1));
82 } else { // we know it's double or float
83 max = new Double(Configuration.getDouble(prefix+"."+PAR_MAX));
84 min = new Double(Configuration.getDouble(prefix+"."+PAR_MIN,
86 step= (max.doubleValue()-min.doubleValue())/(Network.size()-1);
90 // --------------------------------------------------------------------------
92 // --------------------------------------------------------------------------
95 * Initializes a protocol vector with values in the range [{@value #PAR_MIN},
96 * {@value #PAR_MAX}] (inclusive both ends), linearly increasing.
97 * @return always false
99 public boolean execute() {
101 if ( setter.isInteger() )
103 for(int i=0; i<Network.size(); ++i)
105 // we avoid the entire expression being cast to double
106 setter.set(i,Math.round(i*step)+min.longValue());
111 for(int i=0; i<Network.size(); ++i)
113 setter.set(i,i*step+min.doubleValue());