View Javadoc

1   //$Id: ComparableRange.java 250 2006-08-25 21:30:26Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   ********************************************************************************/
38  package junitx.ddtunit.data;
39  
40  public class ComparableRange<T> implements IRange<T> {
41      private Comparable<T> start;
42  
43      private boolean startIncluded;
44  
45      private Comparable<T> end;
46  
47      private boolean endIncluded;
48  
49      /**
50       * Instanciate range and set range delimiting objects as included in Range
51       */
52      public ComparableRange() {
53          this.startIncluded = true;
54          this.endIncluded = true;
55      }
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see junitx.ddtunit.data.IRange#isInRange(java.lang.Comparable)
61       */
62      public boolean isInRange(Comparable<T> actual) {
63  
64          if (this.start == null || this.end == null) {
65              throw new IllegalArgumentException(
66                      "Start and end object for range must be provided.");
67          }
68          boolean check = false;
69          if ((this.startIncluded && this.start.compareTo((T) actual) <= 0)
70                  || this.start.compareTo((T) actual) < 0) {
71              if ((this.endIncluded && this.end.compareTo((T) actual) >= 0)
72                      || this.end.compareTo((T) actual) > 0) {
73                  check = true;
74              }
75          }
76          return check;
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see junitx.ddtunit.data.IRange#getEnd()
83       */
84      public Comparable<T> getEnd() {
85          return this.end;
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see junitx.ddtunit.data.IRange#setEnd(java.lang.Comparable)
92       */
93      public void setEnd(Comparable<T> end) {
94          if (this.start != null) {
95              if (this.start.compareTo((T) end) > 0) {
96                  throw new IllegalArgumentException(
97                          "End element of range is lower than start");
98              }
99          }
100         this.end = end;
101     }
102 
103     /*
104      * (non-Javadoc)
105      * 
106      * @see junitx.ddtunit.data.IRange#isEndIncluded()
107      */
108     public boolean isEndIncluded() {
109         return this.endIncluded;
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see junitx.ddtunit.data.IRange#setEndIncluded(boolean)
116      */
117     public void setEndIncluded(boolean endIncluded) {
118         this.endIncluded = endIncluded;
119     }
120 
121     /*
122      * (non-Javadoc)
123      * 
124      * @see junitx.ddtunit.data.IRange#getStart()
125      */
126     public Comparable getStart() {
127         return this.start;
128     }
129 
130     /*
131      * (non-Javadoc)
132      * 
133      * @see junitx.ddtunit.data.IRange#setStart(java.lang.Comparable)
134      */
135     public void setStart(Comparable<T> start) {
136         if (this.end != null) {
137             if (this.end.compareTo((T) start) < 0) {
138                 throw new IllegalArgumentException(
139                         "Start element of range is higher than end");
140             }
141         }
142         this.start = start;
143     }
144 
145     /*
146      * (non-Javadoc)
147      * 
148      * @see junitx.ddtunit.data.IRange#isStartIncluded()
149      */
150     public boolean isStartIncluded() {
151         return this.startIncluded;
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see junitx.ddtunit.data.IRange#setStartIncluded(boolean)
158      */
159     public void setStartIncluded(boolean startIncluded) {
160         this.startIncluded = startIncluded;
161     }
162 
163     public String toString() {
164         StringBuffer sb = new StringBuffer("Range :");
165         if (this.startIncluded)
166             sb.append("[");
167         else
168             sb.append("]");
169         sb.append(this.start).append(", ").append(end);
170         if (this.endIncluded)
171             sb.append("]");
172         else
173             sb.append("[");
174         return sb.toString();
175     }
176 }