Proposal : Range class ~~~~~~~~~~~~~~~~~~~~~~ Aastha Bhardwaj Created: Aug 10, 2000 Revised: Oct 10, 2000 Revised: Dec 18, 2000 1. Motivation The motivation for this class is to make implementation of Negotiable objects easier. It is expected that many Negotiable's will want to express their capabilities or their support for some parameter as being a range of values. No class providing the functionality of the Range class was found in the JDK core, which is why this class is being proposed. 2. The Range class package javax.media.jai.util; /** * A class to represent ranges of values. A range is defined to contain * all the values between the minimum and maximum values, where * the minimum/maximum value can be considered either included or excluded * from the range. * *

This example creates a range of Integers whose minimum * value is 1 and the maximum value is 5. The range is inclusive at both * ends: * *

* Range intRange = new Range(Integer.class, new Integer(1), new Integer(5)); * * *

A Range can be unbounded at either or both of its ends. * An unbounded end is specified by passing null for the value of that end. * A Range unbounded at both of its ends represents a range of * all possible values for the Class of elements in that * Range. The isMinIncluded() method will always * return true for a Range unbounded on the minimum side and * correspondingly the isMaxIncluded() method will always * return true for a Range unbounded on the maximum side. * *

An empty range is defined as a Range whose minimum value * is greater than it's maximum value if the ends are included, or as a * Range whose minimum value is greater than or equal to it's * maximum value, if the minimum or the maximum value is excluded. */ public class Range implements Serializable { /** * Constructs a Range object given the Class * of the elements in the Range, the minimum value and * the maximum value. The minimum and the maximum value are considered * inclusive. * *

An unbounded range can be specified by passing in a null for * either of the two values, in which case the Range is * unbounded on one side, or for both, in which case the * Range represents an all inclusive set. * * @param elementClass The Class of the Range * elements. * @param minValue The lowest value included in the Range. * @param maxValue The highest value included in the Range. * * @throws IllegalArgumentException if minValue and maxValue are both null, * and elementClass is not one of the * subclasses of Comparable. * @throws IllegalArgumentException if minValue is not the same * Class as elementClass. * @throws IllegalArgumentException if maxValue is not the same * Class as elementClass. */ public Range(Class elementClass, Comparable minValue, Comparable maxValue) {} /** * Constructs a Range object given the Class * of the elements in the Range, the minimum value and * the maximum value. Whether the minimum value and the maximum value * are considered inclusive is specified via the * isMinIncluded and isMaxIncluded variables. * *

An unbounded range can be specified by passing in a null for * either of the two values, in which case the Range is * unbounded at one end, or for both, in which case the * Range represents an all inclusive set. If null is passed * in for either variable, the boolean variables have * no effect. * * @param elementClass The Class of the Range * elements. * @param minValue The lowest value for the Range. * @param isMinIncluded A boolean that defines whether the minimum value is * included in the Range. * @param maxValue The highest value for the Range. * @param isMaxIncluded A boolean that defines whether the maximum value is * included in the Range. * * @throws IllegalArgumentException if minValue and maxValue are both null, * and elementClass is not one of the * subclasses of Comparable. * @throws IllegalArgumentException if minValue is not the same * Class as elementClass. * @throws IllegalArgumentException if maxValue is not the same * Class as elementClass. */ public Range(Class elementClass, Comparable minValue, boolean isMinIncluded, Comparable maxValue, boolean isMaxIncluded) {} /** * Returns true if the minimum value is included within this * Range. If the range is unbounded at this end, this * method will return true. */ public boolean isMinIncluded() {} /** * Returns true if the maximum value is included within this * Range. If the range is unbounded at this end, this * method will return true. */ public boolean isMaxIncluded() {} /** * Returns the Class of the elements of this Range. */ public Class getElementClass() {} /** * Returns the minimum value of this Range. * Returns null if the Range is unbounded at this end. */ public Comparable getMinValue() {} /** * Returns the maximum value of this Range. * Returns null if the Range is unbounded at this end. */ public Comparable getMaxValue() {} /** * Returns true if the specified value is within this Range, * i.e. is either equal to or greater than the minimum value of this * Range and is either lesser than or equal to the maximum * value of this Range. * * @param value The value to be checked for being within this * Range. * @throws IllegalArgumentException if the Class of the value * parameter is not the same as the elementClass of this Range. */ public boolean contains(Comparable value) {} /** * Returns true if the supplied Range is fully contained * within this Range. Fully contained is defined as having * the minimum and maximum values of the fully contained range lie * within the range of values of the containing Range. * * @throws IllegalArgumentException if the Class of the * elements of the given Range is not the same as the * Class of the elements of this Range. * @throws IllegalArgumentException if the given Range is null */ public boolean contains(Range range) {} /** * Returns true if this Range intersects the * given Range. * * @throws IllegalArgumentException if the Class of the * elements of the given Range is not the same as the * Class of the elements of this Range. * @throws IllegalArgumentException if the given Range is null */ public boolean intersects(Range range) {} /** * Returns the union of this Range with the given * Range. If this Range and the given * Range are disjoint, the Range returned * as a result of the union will have a minimum value set to the * minimum of the two disjoint range's minimum values, and the maximum * set to the maximum of the two disjoint range's maximum values, thus * including the disjoint range within it. * * @throws IllegalArgumentException if the Class of the * elements of the given Range is not the same as the * Class of the elements of this Range. * @throws IllegalArgumentException if the given Range is null */ public Range union(Range range) {} /** * Returns the intersection of this Range with the * given Range. * * @throws IllegalArgumentException if the Class of the * elements of the given Range is not the same as the * Class of the elements of this Range. * @throws IllegalArgumentException if the given Range is null */ public Range intersect(Range range) {} /** * Returns the Range of values that are in this * Range but not in the given Range. If * the subtraction results in two disjoint Ranges, they * will be returned as two elements of a Range array, * otherwise the resultant Range will be returned as the * first element of a one element array. * * When this Range and the given Range are * both unbounded at both the ends (i.e both the Ranges * are all inclusive), this method will return null as the first * element of one element array, as a result of the subtraction. * * When this Range is completely contained in the * given Range, an empty Range is returned. * * @throws IllegalArgumentException if the Class of the * elements of the given Range is not the same as the * Class of the elements of this Range. */ public Range[] subtract(Range range) {} /** * Returns true if this Range and the given * Range both have elements of the same Class, * their minimum and maximum values are the same, and their isMinIncluded() * and isMaxIncluded() methods return the same values. * * If this Range and the given Range are both * empty and the Class of their elements is the same, they * will be found to be equal and true will be returned. */ public boolean equals(Object other) {} /** * Returns true if this Range is empty, i.e. if the minimum * value is greater than the maximum value, if both are included, or if * the minimum value is greater than equal to the maximum value if either * the minimum or maximum value is excluded. */ public boolean isEmpty() {} /** * Returns a String representation of this Range. */ public String toString() {} } 3. Enumeration The other class that is expected to be commonly used is the concept of a enumeration of values. This can be adequately represented using the java.util.Enumeration interface.