001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
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: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.util;
033:
034: import java.util.Comparator;
035: import java.util.TreeMap;
036:
037: import com.vividsolutions.jts.util.Assert;
038:
039: public class Range {
040: private boolean includingMin;
041: private boolean includingMax;
042: private Object max;
043: private Object min;
044:
045: public String toString() {
046: return min + " - " + max;
047: }
048:
049: public Range() {
050: this (new NegativeInfinity(), false, new PositiveInfinity(),
051: false);
052: }
053:
054: //NegativeInfinity and PositiveInfinity are classes so they can be
055: //serialized via Java2XML. [Jon Aquino]
056: public static final class NegativeInfinity {
057: public String toString() {
058: // Empty string, for range display in TreeLayerNamePanel. [Jon Aquino 2005-07-25]
059: return "";
060: }
061: }
062:
063: public static final class PositiveInfinity {
064: public String toString() {
065: return "";
066: }
067: }
068:
069: public Range(Object min, boolean includingMin, Object max,
070: boolean includingMax) {
071: Assert
072: .isTrue(!(min.equals(max) && (!includingMin || !includingMax)));
073: this .min = min;
074: this .max = max;
075: this .includingMin = includingMin;
076: this .includingMax = includingMax;
077: }
078:
079: public boolean equals(Object obj) {
080: return Range.RANGE_COMPARATOR.compare(this , obj) == 0;
081: }
082:
083: public boolean isIncludingMax() {
084: return includingMax;
085: }
086:
087: public boolean isIncludingMin() {
088: return includingMin;
089: }
090:
091: public Object getMax() {
092: return max;
093: }
094:
095: public Object getMin() {
096: return min;
097: }
098:
099: public void setIncludingMax(boolean b) {
100: includingMax = b;
101: }
102:
103: public void setIncludingMin(boolean b) {
104: includingMin = b;
105: }
106:
107: public void setMax(Object object) {
108: max = object;
109: }
110:
111: public void setMin(Object object) {
112: min = object;
113: }
114:
115: private static final Comparator INFINITY_COMPARATOR = new Comparator() {
116: public int compare(Object o1, Object o2) {
117: if (o1 instanceof PositiveInfinity
118: || o2 instanceof NegativeInfinity) {
119: return +1;
120: }
121: if (o1 instanceof NegativeInfinity
122: || o2 instanceof PositiveInfinity) {
123: return -1;
124: }
125: return ((Comparable) o1).compareTo(o2);
126: }
127: };
128:
129: public static final Comparator RANGE_COMPARATOR = new Comparator() {
130: public int compare(Object o1, Object o2) {
131: Range range1 = o1 instanceof Range ? (Range) o1
132: : new Range(o1, true, o1, true);
133: Range range2 = o2 instanceof Range ? (Range) o2
134: : new Range(o2, true, o2, true);
135: int max1ComparedToMin2 = INFINITY_COMPARATOR.compare(range1
136: .getMax(), range2.getMin());
137: if (max1ComparedToMin2 < 0
138: || (max1ComparedToMin2 == 0 && (!range1
139: .isIncludingMax() || !range2
140: .isIncludingMin()))) {
141: return -1;
142: }
143: int min1ComparedToMax2 = INFINITY_COMPARATOR.compare(range1
144: .getMin(), range2.getMax());
145: if (min1ComparedToMax2 > 0
146: || (min1ComparedToMax2 == 0 && (!range1
147: .isIncludingMin() || !range2
148: .isIncludingMax()))) {
149: return +1;
150: }
151: return 0;
152: }
153: };
154:
155: //Trivial, but necessary for Java2XML serialization. [Jon Aquino]
156: public static class RangeTreeMap extends TreeMap {
157: public RangeTreeMap() {
158: super(RANGE_COMPARATOR);
159: }
160: }
161:
162: }
|