1 /* $Id: Range.java 1 2010-02-24 13:03:05Z haidw $ */
\r
2 package dol.helper.profiler;
\r
4 import java.util.Collections;
\r
5 import java.util.Vector;
\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
14 public class Range {
\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
23 * Default constructor. When merging this range with another
\r
24 * range, always the resulting range will have the values of
\r
31 * Construct a range with equal upper and lower bound.
\r
33 * @param value upper and lower bound of range
\r
35 public Range(int value) {
\r
38 _initialLower = _lower;
\r
39 _initialUpper = _upper;
\r
43 * Construct a range with the two given bounds.
\r
45 * @param valueA upper or lower bound
\r
46 * @param valueB upper or lower bound
\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
56 * Construct a range with upper and lower bounds that are the minimum
\r
57 * and maximum of the given vector.
\r
59 * @param values vector whose minimum and maximum value are used as
\r
60 * bounds for the range
\r
62 public Range(Vector<Integer> values) {
\r
63 if (values == null || values.size() == 0) {
\r
67 _lower = Collections.min(values);
\r
68 _upper = Collections.max(values);
\r
72 * Return lower bound of this range.
\r
74 * @return lower bound
\r
76 protected int getLower() {
\r
81 * Return upper bound of this range.
\r
83 * @return upper bound
\r
85 protected int getUpper() {
\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
95 * @param range to combine with this range
\r
97 public void merge(Range range) {
\r
101 _lower = Math.min(_lower, range.getLower());
\r
102 _upper = Math.max(_upper, range.getUpper());
\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
111 * @param value integer value to combine with this range
\r
113 public void merge(int value) {
\r
114 _lower = Math.min(_lower, value);
\r
115 _upper = Math.max(_upper, value);
\r
119 * Create a string representation of the range.
\r
121 * @return string representation of the range
\r
123 public String toString() {
\r
124 if (_upper == _lower)
\r
125 return Integer.toString(_upper);
\r
126 return _lower + DELIMITER + _upper;
\r
130 * Create a range based on a string representation of a range.
\r
132 * @param string representation of a range
\r
134 * @throws IllegalArgumentException
\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
145 return new Range(Integer.valueOf(m.group(1)),
\r
146 Integer.valueOf(m.group(2)));
\r
150 * Reset this range to the values it had when it has been constructed.
\r
152 public void reset() {
\r
153 _lower = _initialLower;
\r
154 _upper = _initialUpper;
\r
158 * Test cases for range class.
\r
160 public static void main(String args[]) throws Exception {
\r
161 Range a = new Range(1);
\r
162 System.out.println(a);
\r
164 Range b = new Range(1, 3);
\r
165 System.out.println(b);
\r
167 Range c = new Range(4, 3);
\r
168 System.out.println(c);
\r
171 System.out.println(b);
\r
173 Range d = Range.valueOf("-123" + Range.DELIMITER + "-456");
\r
174 System.out.println(d);
\r