--- /dev/null
+/* $Id: Range.java 1 2010-02-24 13:03:05Z haidw $ */\r
+package dol.helper.profiler;\r
+\r
+import java.util.Collections;\r
+import java.util.Vector;\r
+\r
+/**\r
+ * Tuple of numbers representing an integer range. Except for an\r
+ * uninitialized range when using the default constructor, the\r
+ * range values are sorted, such that {@link #getLower()} returns\r
+ * the lower bound and {@link #getUpper()} the upper bound of the\r
+ * range.\r
+ */\r
+public class Range {\r
+\r
+ protected int _lower = Integer.MAX_VALUE;\r
+ protected int _upper = Integer.MIN_VALUE;\r
+ protected int _initialLower = Integer.MAX_VALUE;\r
+ protected int _initialUpper = Integer.MIN_VALUE;\r
+ public static final String DELIMITER = "-";\r
+\r
+ /**\r
+ * Default constructor. When merging this range with another\r
+ * range, always the resulting range will have the values of\r
+ * the other range.\r
+ */\r
+ public Range() {\r
+ }\r
+\r
+ /**\r
+ * Construct a range with equal upper and lower bound.\r
+ *\r
+ * @param value upper and lower bound of range\r
+ */\r
+ public Range(int value) {\r
+ _lower = value;\r
+ _upper = value;\r
+ _initialLower = _lower;\r
+ _initialUpper = _upper;\r
+ }\r
+\r
+ /**\r
+ * Construct a range with the two given bounds.\r
+ *\r
+ * @param valueA upper or lower bound\r
+ * @param valueB upper or lower bound\r
+ */\r
+ public Range(int valueA, int valueB) {\r
+ _lower = Math.min(valueA, valueB);\r
+ _upper = Math.max(valueA, valueB);\r
+ _initialLower = _lower;\r
+ _initialUpper = _upper;\r
+ }\r
+\r
+ /**\r
+ * Construct a range with upper and lower bounds that are the minimum\r
+ * and maximum of the given vector.\r
+ *\r
+ * @param values vector whose minimum and maximum value are used as\r
+ * bounds for the range\r
+ */\r
+ public Range(Vector<Integer> values) {\r
+ if (values == null || values.size() == 0) {\r
+ return;\r
+ }\r
+\r
+ _lower = Collections.min(values);\r
+ _upper = Collections.max(values);\r
+ }\r
+\r
+ /**\r
+ * Return lower bound of this range.\r
+ *\r
+ * @return lower bound\r
+ */\r
+ protected int getLower() {\r
+ return _lower;\r
+ }\r
+\r
+ /**\r
+ * Return upper bound of this range.\r
+ *\r
+ * @return upper bound\r
+ */\r
+ protected int getUpper() {\r
+ return _upper;\r
+ }\r
+\r
+ /**\r
+ * Combine range with another range. The lower bound of the range is\r
+ * the minimum of the lower bounds of this range and the other range.\r
+ * The upper bound of the range is the maximum of the upper bounds of\r
+ * this range and the other range.\r
+ *\r
+ * @param range to combine with this range\r
+ */\r
+ public void merge(Range range) {\r
+ if (range == null)\r
+ return;\r
+\r
+ _lower = Math.min(_lower, range.getLower());\r
+ _upper = Math.max(_upper, range.getUpper());\r
+ }\r
+\r
+ /**\r
+ * Combine range with a value. The lower bound of the range is\r
+ * the minimum of the lower bound of this range and the value.\r
+ * The upper bound of the range is the maximum of the upper bound of\r
+ * this range and the value.\r
+ *\r
+ * @param value integer value to combine with this range\r
+ */\r
+ public void merge(int value) {\r
+ _lower = Math.min(_lower, value);\r
+ _upper = Math.max(_upper, value);\r
+ }\r
+\r
+ /**\r
+ * Create a string representation of the range.\r
+ *\r
+ * @return string representation of the range\r
+ */\r
+ public String toString() {\r
+ if (_upper == _lower)\r
+ return Integer.toString(_upper);\r
+ return _lower + DELIMITER + _upper;\r
+ }\r
+\r
+ /**\r
+ * Create a range based on a string representation of a range.\r
+ *\r
+ * @param string representation of a range\r
+ * @return range\r
+ * @throws IllegalArgumentException\r
+ */\r
+ public static Range valueOf(String string) throws IllegalArgumentException {\r
+ java.util.regex.Pattern pattern =\r
+ java.util.regex.Pattern.compile(\r
+ "([-]?[0-9]+)[" + DELIMITER + "]?([-]?[0-9]+)?");\r
+ java.util.regex.Matcher m = pattern.matcher(string);\r
+ if (!m.matches()) {\r
+ throw new IllegalArgumentException("String does not " +\r
+ "represent a range.");\r
+ }\r
+ return new Range(Integer.valueOf(m.group(1)),\r
+ Integer.valueOf(m.group(2)));\r
+ }\r
+\r
+ /**\r
+ * Reset this range to the values it had when it has been constructed.\r
+ */\r
+ public void reset() {\r
+ _lower = _initialLower;\r
+ _upper = _initialUpper;\r
+ }\r
+\r
+ /**\r
+ * Test cases for range class.\r
+ */\r
+ public static void main(String args[]) throws Exception {\r
+ Range a = new Range(1);\r
+ System.out.println(a);\r
+\r
+ Range b = new Range(1, 3);\r
+ System.out.println(b);\r
+\r
+ Range c = new Range(4, 3);\r
+ System.out.println(c);\r
+\r
+ b.merge(c);\r
+ System.out.println(b);\r
+\r
+ Range d = Range.valueOf("-123" + Range.DELIMITER + "-456");\r
+ System.out.println(d);\r
+ }\r
+}
\ No newline at end of file