Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / util / StringListParser.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18
19 package peersim.util;
20
21 import java.math.*;
22 import java.util.*;
23
24 import org.lsmp.djep.groupJep.*;
25
26 import peersim.config.*;
27
28 /**
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.
32  * <p>
33  * The language for range expression is the following: 
34  * <pre>
35  *   [rangelist] := [range] | [range],[rangelist]
36  *   [range] := value | min:max | min:max|step | 
37  *      min:max*|step
38  * </pre>
39  * where <tt>value</tt>, <tt>min</tt>, <tt>max</tt> and <tt>step</tt>
40  * are numeric atoms that defines ranges.
41  * <p>
42  * For example, the following range expression:
43  * <pre>
44  *   5,9:11,13:17|2,32:128*|2
45  * </pre>
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).
49  * 
50  * @author Alberto Montresor
51  * @version $Revision: 1.8 $
52  */
53 public class StringListParser
54 {
55
56 /** Disable instance construction */
57 private StringListParser() { }
58
59 /**
60  * Parse the specified string.
61  * 
62  * @param s the string to be parsed
63  * @return an array of strings containing all the values defined by the
64  *   range string
65  */
66 public static String[] parseList(String s)
67 {
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]);
72         }
73         return list.toArray(new String[list.size()]);
74 }
75
76 private static void parseItem(List<String> list, String item)
77 {
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]);
83         } else {
84                 throw new IllegalArgumentException("Element " + item
85                                 + "should be formatted as <start>:<stop> or <value>");
86         }
87 }
88
89 private static void parseSingleItem(List<String> list, String item)
90 {
91         list.add(item);
92 }
93
94 private static void parseRangeItem(List<String> list, String start, String stop)
95 {
96         Number vstart;
97         Number vstop;
98         Number vinc;
99         boolean sum;
100         
101         GroupJep jep = new GroupJep(new Operators());
102         jep.parseExpression(start);
103         vstart = (Number) jep.getValueAsObject(); 
104         int pos = stop.indexOf("|*");
105         if (pos >= 0) {
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(); 
111                 sum = false;
112         } else {
113                 pos = stop.indexOf("|");
114                 // The string contains an additive factor
115                 if (pos >= 0) {
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(); 
121                         sum = true;
122                 } else {
123                         jep.parseExpression(stop);
124                         vstop = (Number) jep.getValueAsObject(); 
125                         vinc = BigInteger.ONE;
126                         sum = true;
127                 }
128         }
129         
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(); 
134                 if (sum) {
135                         for (long i = vvstart; i <= vvstop; i += vvinc)
136                                 list.add("" + i);
137                 } else {
138                         for (long i = vvstart; i <= vvstop; i *= vvinc)
139                                 list.add("" + i);
140                 }
141         } else {
142                 double vvstart = vstart.doubleValue();
143                 double vvstop  =  vstop.doubleValue();
144                 double vvinc   =   vinc.doubleValue(); 
145                 if (sum) {
146                         for (double i = vvstart; i <= vvstop; i += vvinc) 
147                                 list.add("" + i);
148                 } else {
149                         for (double i = vvstart; i <= vvstop; i *= vvinc)
150                                 list.add("" + i);
151                 }
152         }
153 }
154
155 /*
156 public static void main(String[] args)
157 {
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("");
162 }
163 */
164 }