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