001: //$Id: ComparableRange.java 250 2006-08-25 21:30:26Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data;
038:
039: public class ComparableRange<T> implements IRange<T> {
040: private Comparable<T> start;
041:
042: private boolean startIncluded;
043:
044: private Comparable<T> end;
045:
046: private boolean endIncluded;
047:
048: /**
049: * Instanciate range and set range delimiting objects as included in Range
050: */
051: public ComparableRange() {
052: this .startIncluded = true;
053: this .endIncluded = true;
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see junitx.ddtunit.data.IRange#isInRange(java.lang.Comparable)
060: */
061: public boolean isInRange(Comparable<T> actual) {
062:
063: if (this .start == null || this .end == null) {
064: throw new IllegalArgumentException(
065: "Start and end object for range must be provided.");
066: }
067: boolean check = false;
068: if ((this .startIncluded && this .start.compareTo((T) actual) <= 0)
069: || this .start.compareTo((T) actual) < 0) {
070: if ((this .endIncluded && this .end.compareTo((T) actual) >= 0)
071: || this .end.compareTo((T) actual) > 0) {
072: check = true;
073: }
074: }
075: return check;
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see junitx.ddtunit.data.IRange#getEnd()
082: */
083: public Comparable<T> getEnd() {
084: return this .end;
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see junitx.ddtunit.data.IRange#setEnd(java.lang.Comparable)
091: */
092: public void setEnd(Comparable<T> end) {
093: if (this .start != null) {
094: if (this .start.compareTo((T) end) > 0) {
095: throw new IllegalArgumentException(
096: "End element of range is lower than start");
097: }
098: }
099: this .end = end;
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see junitx.ddtunit.data.IRange#isEndIncluded()
106: */
107: public boolean isEndIncluded() {
108: return this .endIncluded;
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see junitx.ddtunit.data.IRange#setEndIncluded(boolean)
115: */
116: public void setEndIncluded(boolean endIncluded) {
117: this .endIncluded = endIncluded;
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see junitx.ddtunit.data.IRange#getStart()
124: */
125: public Comparable getStart() {
126: return this .start;
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see junitx.ddtunit.data.IRange#setStart(java.lang.Comparable)
133: */
134: public void setStart(Comparable<T> start) {
135: if (this .end != null) {
136: if (this .end.compareTo((T) start) < 0) {
137: throw new IllegalArgumentException(
138: "Start element of range is higher than end");
139: }
140: }
141: this .start = start;
142: }
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see junitx.ddtunit.data.IRange#isStartIncluded()
148: */
149: public boolean isStartIncluded() {
150: return this .startIncluded;
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see junitx.ddtunit.data.IRange#setStartIncluded(boolean)
157: */
158: public void setStartIncluded(boolean startIncluded) {
159: this .startIncluded = startIncluded;
160: }
161:
162: public String toString() {
163: StringBuffer sb = new StringBuffer("Range :");
164: if (this .startIncluded)
165: sb.append("[");
166: else
167: sb.append("]");
168: sb.append(this .start).append(", ").append(end);
169: if (this .endIncluded)
170: sb.append("]");
171: else
172: sb.append("[");
173: return sb.toString();
174: }
175: }
|