dol: initial dol commit
[jump.git] / dol / src / dol / helper / profiler / Range.java
1 /* $Id: Range.java 1 2010-02-24 13:03:05Z haidw $ */\r
2 package dol.helper.profiler;\r
3 \r
4 import java.util.Collections;\r
5 import java.util.Vector;\r
6 \r
7 /**\r
8  * Tuple of numbers representing an integer range. Except for an\r
9  * uninitialized range when using the default constructor, the\r
10  * range values are sorted, such that {@link #getLower()} returns\r
11  * the lower bound and {@link #getUpper()} the upper bound of the\r
12  * range.\r
13  */\r
14 public class Range {\r
15 \r
16     protected int _lower = Integer.MAX_VALUE;\r
17     protected int _upper = Integer.MIN_VALUE;\r
18     protected int _initialLower = Integer.MAX_VALUE;\r
19     protected int _initialUpper = Integer.MIN_VALUE;\r
20     public static final String DELIMITER = "-";\r
21 \r
22     /**\r
23      * Default constructor. When merging this range with another\r
24      * range, always the resulting range will have the values of\r
25      * the other range.\r
26      */\r
27     public Range() {\r
28     }\r
29 \r
30     /**\r
31      * Construct a range with equal upper and lower bound.\r
32      *\r
33      * @param value upper and lower bound of range\r
34      */\r
35     public Range(int value) {\r
36         _lower = value;\r
37         _upper = value;\r
38         _initialLower = _lower;\r
39         _initialUpper = _upper;\r
40     }\r
41 \r
42     /**\r
43      * Construct a range with the two given bounds.\r
44      *\r
45      * @param valueA upper or lower bound\r
46      * @param valueB upper or lower bound\r
47      */\r
48     public Range(int valueA, int valueB) {\r
49         _lower = Math.min(valueA, valueB);\r
50         _upper = Math.max(valueA, valueB);\r
51         _initialLower = _lower;\r
52         _initialUpper = _upper;\r
53     }\r
54 \r
55     /**\r
56      * Construct a range with upper and lower bounds that are the minimum\r
57      * and maximum of the given vector.\r
58      *\r
59      * @param values vector whose minimum and maximum value are used as\r
60      *               bounds for the range\r
61      */\r
62     public Range(Vector<Integer> values) {\r
63         if (values == null || values.size() == 0) {\r
64             return;\r
65         }\r
66 \r
67         _lower = Collections.min(values);\r
68         _upper = Collections.max(values);\r
69     }\r
70 \r
71     /**\r
72      * Return lower bound of this range.\r
73      *\r
74      * @return lower bound\r
75      */\r
76     protected int getLower() {\r
77         return _lower;\r
78     }\r
79 \r
80     /**\r
81      * Return upper bound of this range.\r
82      *\r
83      * @return upper bound\r
84      */\r
85     protected int getUpper() {\r
86         return _upper;\r
87     }\r
88 \r
89     /**\r
90      * Combine range with another range. The lower bound of the range is\r
91      * the minimum of the lower bounds of this range and the other range.\r
92      * The upper bound of the range is the maximum of the upper bounds of\r
93      * this range and the other range.\r
94      *\r
95      * @param range to combine with this range\r
96      */\r
97     public void merge(Range range) {\r
98         if (range == null)\r
99             return;\r
100 \r
101         _lower = Math.min(_lower, range.getLower());\r
102         _upper = Math.max(_upper, range.getUpper());\r
103     }\r
104 \r
105     /**\r
106      * Combine range with a value. The lower bound of the range is\r
107      * the minimum of the lower bound of this range and the value.\r
108      * The upper bound of the range is the maximum of the upper bound of\r
109      * this range and the value.\r
110      *\r
111      * @param value integer value to combine with this range\r
112      */\r
113     public void merge(int value) {\r
114         _lower = Math.min(_lower, value);\r
115         _upper = Math.max(_upper, value);\r
116     }\r
117 \r
118     /**\r
119      * Create a string representation of the range.\r
120      *\r
121      * @return string representation of the range\r
122      */\r
123     public String toString() {\r
124         if (_upper == _lower)\r
125             return Integer.toString(_upper);\r
126         return _lower + DELIMITER + _upper;\r
127     }\r
128 \r
129     /**\r
130      * Create a range based on a string representation of a range.\r
131      *\r
132      * @param string representation of a range\r
133      * @return range\r
134      * @throws IllegalArgumentException\r
135      */\r
136     public static Range valueOf(String string) throws IllegalArgumentException {\r
137         java.util.regex.Pattern pattern =\r
138                 java.util.regex.Pattern.compile(\r
139                 "([-]?[0-9]+)[" + DELIMITER + "]?([-]?[0-9]+)?");\r
140         java.util.regex.Matcher m = pattern.matcher(string);\r
141         if (!m.matches()) {\r
142             throw new IllegalArgumentException("String does not " +\r
143                     "represent a range.");\r
144         }\r
145         return new Range(Integer.valueOf(m.group(1)),\r
146                 Integer.valueOf(m.group(2)));\r
147     }\r
148 \r
149     /**\r
150      * Reset this range to the values it had when it has been constructed.\r
151      */\r
152     public void reset() {\r
153         _lower = _initialLower;\r
154         _upper = _initialUpper;\r
155     }\r
156 \r
157     /**\r
158      * Test cases for range class.\r
159      */\r
160     public static void main(String args[]) throws Exception {\r
161         Range a = new Range(1);\r
162         System.out.println(a);\r
163 \r
164         Range b = new Range(1, 3);\r
165         System.out.println(b);\r
166 \r
167         Range c = new Range(4, 3);\r
168         System.out.println(c);\r
169 \r
170         b.merge(c);\r
171         System.out.println(b);\r
172 \r
173         Range d = Range.valueOf("-123" + Range.DELIMITER + "-456");\r
174         System.out.println(d);\r
175     }\r
176 }