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.
24 import org.lsmp.djep.groupJep.*;
26 import peersim.config.*;
29 * This utility class can be used to parse range expressions. In particular,
30 * it is used by {@link peersim.rangesim.RangeSimulator} to express ranges for
31 * configuration properties.
33 * The language for range expression is the following:
35 * [rangelist] := [range] | [range],[rangelist]
36 * [range] := value | min:max | min:max|step |
39 * where <tt>value</tt>, <tt>min</tt>, <tt>max</tt> and <tt>step</tt>
40 * are numeric atoms that defines ranges.
42 * For example, the following range expression:
44 * 5,9:11,13:17|2,32:128*|2
46 * corresponds to 5 (single value), 9-10-11 (range between 9 and 11,
47 * default increment 1), 13-15-17 (range between 13 and 17, specified
48 * step 2, 32-64-128 (range between 32 and 128, multiplicative step 2).
50 * @author Alberto Montresor
51 * @version $Revision: 1.8 $
53 public class StringListParser
56 /** Disable instance construction */
57 private StringListParser() { }
60 * Parse the specified string.
62 * @param s the string to be parsed
63 * @return an array of strings containing all the values defined by the
66 public static String[] parseList(String s)
68 ArrayList<String> list = new ArrayList<String>();
69 String[] tokens = s.split(",");
70 for (int i = 0; i < tokens.length; i++) {
71 parseItem(list, tokens[i]);
73 return list.toArray(new String[list.size()]);
76 private static void parseItem(List<String> list, String item)
78 String[] array = item.split(":");
79 if (array.length == 1) {
80 parseSingleItem(list, item);
81 } else if (array.length == 2) {
82 parseRangeItem(list, array[0], array[1]);
84 throw new IllegalArgumentException("Element " + item
85 + "should be formatted as <start>:<stop> or <value>");
89 private static void parseSingleItem(List<String> list, String item)
94 private static void parseRangeItem(List<String> list, String start, String stop)
101 GroupJep jep = new GroupJep(new Operators());
102 jep.parseExpression(start);
103 vstart = (Number) jep.getValueAsObject();
104 int pos = stop.indexOf("|*");
106 // The string contains a multiplicative factor
107 jep.parseExpression(stop.substring(0, pos));
108 vstop = (Number) jep.getValueAsObject();
109 jep.parseExpression(stop.substring(pos + 2));
110 vinc = (Number) jep.getValueAsObject();
113 pos = stop.indexOf("|");
114 // The string contains an additive factor
116 // The string contains just the final value
117 jep.parseExpression(stop.substring(0, pos));
118 vstop = (Number) jep.getValueAsObject();
119 jep.parseExpression(stop.substring(pos + 1));
120 vinc = (Number) jep.getValueAsObject();
123 jep.parseExpression(stop);
124 vstop = (Number) jep.getValueAsObject();
125 vinc = BigInteger.ONE;
130 if (vstart instanceof BigInteger && vstart instanceof BigInteger && vinc instanceof BigInteger) {
131 long vvstart = vstart.longValue();
132 long vvstop = vstop.longValue();
133 long vvinc = vinc.longValue();
135 for (long i = vvstart; i <= vvstop; i += vvinc)
138 for (long i = vvstart; i <= vvstop; i *= vvinc)
142 double vvstart = vstart.doubleValue();
143 double vvstop = vstop.doubleValue();
144 double vvinc = vinc.doubleValue();
146 for (double i = vvstart; i <= vvstop; i += vvinc)
149 for (double i = vvstart; i <= vvstop; i *= vvinc)
156 public static void main(String[] args)
158 String[] ret = parseList(args[0]);
159 for (int i = 0; i < ret.length; i++)
160 System.out.print(ret[i] + " ");
161 System.out.println("");