001: package org.springunit.examples;
002:
003: import org.apache.commons.lang.builder.EqualsBuilder;
004: import org.apache.commons.lang.builder.HashCodeBuilder;
005: import org.apache.commons.lang.builder.ToStringBuilder;
006: import org.apache.commons.lang.builder.ToStringStyle;
007:
008: public class Range<T extends Comparable<T>> {
009:
010: /**
011: * Create range without specified bounds.<br/>
012: */
013: public Range() {
014: this (null, null);
015: }
016:
017: /**
018: * Create range with <code>upper</code>
019: * and <code>lower</code> bounds.<br/>
020: * @param upper Upper bound of range
021: * @param lower Lower bound of range
022: */
023: public Range(T upper, T lower) {
024: setUpper(upper);
025: setLower(lower);
026: }
027:
028: /**
029: * Is this equal to obj?<br/>
030: */
031: public boolean equals(Object obj) {
032: if (this == obj) {
033: return true;
034: }
035: if (!(obj instanceof Range)) {
036: return false;
037: }
038: Range other = (Range) obj;
039: return new EqualsBuilder().append(getUpper(), other.getUpper())
040: .append(getLower(), other.getLower()).isEquals();
041: }
042:
043: /**
044: * Lower bound of range.<br/>
045: * @return T
046: */
047: public T getLower() {
048: return this .lower;
049: }
050:
051: /**
052: * Upper bound of range.<br/>
053: * @return T
054: */
055: public T getUpper() {
056: return this .upper;
057: }
058:
059: /**
060: * Hash code.<br/>
061: */
062: public int hashCode() {
063: return new HashCodeBuilder(17, 37).append(getUpper()).append(
064: getLower()).toHashCode();
065: }
066:
067: /**
068: * Is item between upper and lower
069: * bounds?
070: */
071: public boolean isWithin(T item) {
072: return item.compareTo(this .lower) >= 0
073: && item.compareTo(this .upper) <= 0;
074: }
075:
076: /**
077: * Lower bound of range.<br/>
078: * @param lower New lower bound
079: */
080: public void setLower(T lower) {
081: this .lower = lower;
082: }
083:
084: /**
085: * Upper bound of range.<br/>
086: * @param upper New upper bound
087: */
088: public void setUpper(T upper) {
089: this .upper = upper;
090: }
091:
092: /**
093: * String representation.<br/>
094: */
095: public String toString() {
096: return new ToStringBuilder(this , ToStringStyle.MULTI_LINE_STYLE)
097: .append("upper", getUpper())
098: .append("lower", getLower()).toString();
099: }
100:
101: private T lower;
102: private T upper;
103:
104: }
|