Range

From EggeWiki
Revision as of 08:44, 12 September 2007 by Brianegge (talk | contribs) (New page: Martin Fowler has written about the http://martinfowler.com/eaaDev/Range.html Range Base Pattern for representing a range of values. Here's a generic base class I wrote in Java which ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Martin Fowler has written about the [Range Base Pattern] for representing a range of values. Here's a generic base class I wrote in Java which has the same basic functionality. I usually subclass this and add type specific methods.

<geshi lang="java"> /**

* A generic range.  I would prefer to define this as Range<E extends Comparable<? super E>>,
* but then pre 1.5 types can't be used with this.
* Additionally, you run into hell when you have Base implements Comparable<Base> and want to have
* Derived extends Base.  Derived can't implement Comparable<Derived> because it's already defined differently
* in the base class.
*/

public class Range<E extends Comparable> {

   protected final E min;
   protected final E max;
   public Range(E min, E max) {
       GuardHelper.notNullMultiple(min, max);
       validate(min, max);
       this.min = min;
       this.max = max;
   }
   protected void validate(E min, E max) {
       if (min.compareTo(max) > 0) {
           throw new IllegalArgumentException(min + " < " + max);
       }
   }
   public boolean contains(E value) {
       return value != null && min.compareTo(value) <= 0
               && max.compareTo(value) >= 0;
   }
   public E getMinimum() {
       return min;
   }
   public E getMaximum() {
       return max;
   }
   public int hashCode() {
       return min.hashCode() + max.hashCode();
   }
   public boolean equals(Object o) {
       if (this == o) {
           return true;
       }
       if (o == null || getClass() != o.getClass()) {
           return false;
       }
       Range range = (Range) o;
       return max.equals(range.max) && min.equals(range.min);
   }


   @Override
   public String toString() {
       return min + ".." + max;
   }

} </geshi>