001: /**
002: * com.mckoi.database.SelectableRange 12 Aug 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: /**
026: * An object that represents a range of values to select from a list. A range
027: * has a start value, an end value, and whether we should pick inclusive or
028: * exclusive of the end value. The start value may be a concrete value from
029: * the set or it may be a flag that represents the start or end of the list.
030: * <p>
031: * For example, to select the first item from a set the range would be;
032: * <pre>
033: * RANGE:
034: * start = FIRST_VALUE, first
035: * end = LAST_VALUE, first
036: * </pre>
037: * To select the last item from a set the range would be;
038: * <pre>
039: * RANGE:
040: * start = FIRST_VALUE, last
041: * end = LAST_VALUE, last
042: * </pre>
043: * To select the range of values between '10' and '15' then range would be;
044: * <pre>
045: * RANGE:
046: * start = FIRST_VALUE, '10'
047: * end = LAST_VALUE, '15'
048: * </pre>
049: * Note that the the start value may not compare less than the end value. For
050: * example, start can not be 'last' and end can not be 'first'.
051: *
052: * @author Tobias Downer
053: */
054:
055: public final class SelectableRange {
056:
057: // ---------- Statics ----------
058:
059: /**
060: * An object that represents the first value in the set.
061: * <p>
062: * Note that these objects have no (NULL) type.
063: */
064: public static final TObject FIRST_IN_SET = new TObject(
065: TType.NULL_TYPE, "[FIRST_IN_SET]");
066:
067: /**
068: * An object that represents the last value in the set.
069: * <p>
070: * Note that these objects have no (NULL) type.
071: */
072: public static final TObject LAST_IN_SET = new TObject(
073: TType.NULL_TYPE, "[LAST_IN_SET]");
074:
075: /**
076: * Represents the various points in the set on the value to represent the
077: * set range.
078: */
079: public static final byte FIRST_VALUE = 1, LAST_VALUE = 2,
080: BEFORE_FIRST_VALUE = 3, AFTER_LAST_VALUE = 4;
081:
082: // ---------- Members ----------
083:
084: /**
085: * The start of the range to select from the set.
086: */
087: private TObject start;
088:
089: /**
090: * The end of the range to select from the set.
091: */
092: private TObject end;
093:
094: /**
095: * Denotes the place for the range to start with respect to the start value.
096: * Either FIRST_VALUE or AFTER_LAST_VALUE.
097: */
098: private byte set_start_flag;
099:
100: /**
101: * Denotes the place for the range to end with respect to the end value.
102: * Either BEFORE_FIRST_VALUE or LAST_VALUE.
103: */
104: private byte set_end_flag;
105:
106: /**
107: * Constructs the range.
108: */
109: public SelectableRange(byte set_start_flag, TObject start,
110: byte set_end_flag, TObject end) {
111: this .start = start;
112: this .end = end;
113: this .set_start_flag = set_start_flag;
114: this .set_end_flag = set_end_flag;
115: }
116:
117: /**
118: * Returns the start of the range.
119: * NOTE: This may return FIRST_IN_SET or LAST_IN_SET.
120: */
121: public TObject getStart() {
122: return start;
123: }
124:
125: /**
126: * Returns the end of the range.
127: * NOTE: This may return FIRST_IN_SET or LAST_IN_SET.
128: */
129: public TObject getEnd() {
130: return end;
131: }
132:
133: /**
134: * Returns the place for the range to start (either FIRST_VALUE or
135: * AFTER_LAST_VALUE)
136: */
137: public byte getStartFlag() {
138: return set_start_flag;
139: }
140:
141: /**
142: * Returns the place for the range to end (either BEFORE_FIRST_VALUE or
143: * LAST VALUE).
144: */
145: public byte getEndFlag() {
146: return set_end_flag;
147: }
148:
149: /**
150: * Outputs this range as a string.
151: */
152: public String toString() {
153: StringBuffer buf = new StringBuffer();
154: if (getStartFlag() == FIRST_VALUE) {
155: buf.append("FIRST_VALUE ");
156: } else if (getStartFlag() == AFTER_LAST_VALUE) {
157: buf.append("AFTER_LAST_VALUE ");
158: }
159: buf.append(getStart());
160: buf.append(" -> ");
161: if (getEndFlag() == LAST_VALUE) {
162: buf.append("LAST_VALUE ");
163: } else if (getEndFlag() == BEFORE_FIRST_VALUE) {
164: buf.append("BEFORE_FIRST_VALUE ");
165: }
166: buf.append(getEnd());
167: return new String(buf);
168: }
169:
170: /**
171: * Returns true if this range is equal to the given range.
172: */
173: public boolean equals(Object ob) {
174: if (super .equals(ob)) {
175: return true;
176: }
177:
178: SelectableRange dest_range = (SelectableRange) ob;
179: return (getStart().valuesEqual(dest_range.getStart())
180: && getEnd().valuesEqual(dest_range.getEnd())
181: && getStartFlag() == dest_range.getStartFlag() && getEndFlag() == dest_range
182: .getEndFlag());
183: }
184:
185: // ---------- Statics ----------
186:
187: /**
188: * The range that represents the entire range (including null).
189: */
190: public static final SelectableRange FULL_RANGE = new SelectableRange(
191: FIRST_VALUE, FIRST_IN_SET, LAST_VALUE, LAST_IN_SET);
192:
193: /**
194: * The range that represents the entire range (not including null).
195: */
196: public static final SelectableRange FULL_RANGE_NO_NULLS = new SelectableRange(
197: AFTER_LAST_VALUE, TObject.nullVal(), LAST_VALUE,
198: LAST_IN_SET);
199:
200: }
|