KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > SelectableRange


1 /**
2  * com.mckoi.database.SelectableRange 12 Aug 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 /**
28  * An object that represents a range of values to select from a list. A range
29  * has a start value, an end value, and whether we should pick inclusive or
30  * exclusive of the end value. The start value may be a concrete value from
31  * the set or it may be a flag that represents the start or end of the list.
32  * <p>
33  * For example, to select the first item from a set the range would be;
34  * <pre>
35  * RANGE:
36  * start = FIRST_VALUE, first
37  * end = LAST_VALUE, first
38  * </pre>
39  * To select the last item from a set the range would be;
40  * <pre>
41  * RANGE:
42  * start = FIRST_VALUE, last
43  * end = LAST_VALUE, last
44  * </pre>
45  * To select the range of values between '10' and '15' then range would be;
46  * <pre>
47  * RANGE:
48  * start = FIRST_VALUE, '10'
49  * end = LAST_VALUE, '15'
50  * </pre>
51  * Note that the the start value may not compare less than the end value. For
52  * example, start can not be 'last' and end can not be 'first'.
53  *
54  * @author Tobias Downer
55  */

56
57 public final class SelectableRange {
58
59   // ---------- Statics ----------
60

61   /**
62    * An object that represents the first value in the set.
63    * <p>
64    * Note that these objects have no (NULL) type.
65    */

66   public static final TObject FIRST_IN_SET =
67                              new TObject(TType.NULL_TYPE, "[FIRST_IN_SET]");
68
69   /**
70    * An object that represents the last value in the set.
71    * <p>
72    * Note that these objects have no (NULL) type.
73    */

74   public static final TObject LAST_IN_SET =
75                               new TObject(TType.NULL_TYPE, "[LAST_IN_SET]");
76
77   /**
78    * Represents the various points in the set on the value to represent the
79    * set range.
80    */

81   public static final byte FIRST_VALUE = 1,
82                            LAST_VALUE = 2,
83                            BEFORE_FIRST_VALUE = 3,
84                            AFTER_LAST_VALUE = 4;
85
86   // ---------- Members ----------
87

88   /**
89    * The start of the range to select from the set.
90    */

91   private TObject start;
92
93   /**
94    * The end of the range to select from the set.
95    */

96   private TObject end;
97
98   /**
99    * Denotes the place for the range to start with respect to the start value.
100    * Either FIRST_VALUE or AFTER_LAST_VALUE.
101    */

102   private byte set_start_flag;
103
104   /**
105    * Denotes the place for the range to end with respect to the end value.
106    * Either BEFORE_FIRST_VALUE or LAST_VALUE.
107    */

108   private byte set_end_flag;
109
110   /**
111    * Constructs the range.
112    */

113   public SelectableRange(byte set_start_flag, TObject start,
114                          byte set_end_flag, TObject end) {
115     this.start = start;
116     this.end = end;
117     this.set_start_flag = set_start_flag;
118     this.set_end_flag = set_end_flag;
119   }
120
121   /**
122    * Returns the start of the range.
123    * NOTE: This may return FIRST_IN_SET or LAST_IN_SET.
124    */

125   public TObject getStart() {
126     return start;
127   }
128
129   /**
130    * Returns the end of the range.
131    * NOTE: This may return FIRST_IN_SET or LAST_IN_SET.
132    */

133   public TObject getEnd() {
134     return end;
135   }
136
137   /**
138    * Returns the place for the range to start (either FIRST_VALUE or
139    * AFTER_LAST_VALUE)
140    */

141   public byte getStartFlag() {
142     return set_start_flag;
143   }
144
145   /**
146    * Returns the place for the range to end (either BEFORE_FIRST_VALUE or
147    * LAST VALUE).
148    */

149   public byte getEndFlag() {
150     return set_end_flag;
151   }
152
153
154   /**
155    * Outputs this range as a string.
156    */

157   public String JavaDoc toString() {
158     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
159     if (getStartFlag() == FIRST_VALUE) {
160       buf.append("FIRST_VALUE ");
161     }
162     else if (getStartFlag() == AFTER_LAST_VALUE) {
163       buf.append("AFTER_LAST_VALUE ");
164     }
165     buf.append(getStart());
166     buf.append(" -> ");
167     if (getEndFlag() == LAST_VALUE) {
168       buf.append("LAST_VALUE ");
169     }
170     else if (getEndFlag() == BEFORE_FIRST_VALUE) {
171       buf.append("BEFORE_FIRST_VALUE ");
172     }
173     buf.append(getEnd());
174     return new String JavaDoc(buf);
175   }
176
177   /**
178    * Returns true if this range is equal to the given range.
179    */

180   public boolean equals(Object JavaDoc ob) {
181     if (super.equals(ob)) {
182       return true;
183     }
184
185     SelectableRange dest_range = (SelectableRange) ob;
186     return (getStart().valuesEqual(dest_range.getStart()) &&
187             getEnd().valuesEqual(dest_range.getEnd()) &&
188             getStartFlag() == dest_range.getStartFlag() &&
189             getEndFlag() == dest_range.getEndFlag());
190   }
191
192   // ---------- Statics ----------
193

194   /**
195    * The range that represents the entire range (including null).
196    */

197   public static final SelectableRange FULL_RANGE =
198       new SelectableRange(FIRST_VALUE, FIRST_IN_SET, LAST_VALUE, LAST_IN_SET);
199
200   /**
201    * The range that represents the entire range (not including null).
202    */

203   public static final SelectableRange FULL_RANGE_NO_NULLS =
204       new SelectableRange(AFTER_LAST_VALUE, TObject.nullVal(),
205                           LAST_VALUE, LAST_IN_SET);
206
207
208 }
209
Popular Tags