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:
033: package com.vividsolutions.jump.workbench.ui.plugin.analysis;
034:
035: import java.text.ParseException;
036: import java.util.ArrayList;
037: import java.util.Date;
038: import java.util.List;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041: import com.vividsolutions.jump.I18N;
042: import com.vividsolutions.jump.util.FlexibleDateParser;
043: import com.vividsolutions.jump.workbench.ui.GenericNames;
044:
045: /**
046: * A function object for {@link Geometry} functions (which return a Geometry).
047: * Provides metadata about the function.
048: *
049: * @author Martin Davis
050: * @version 1.0
051: */
052: public abstract class AttributePredicate {
053: static AttributePredicate[] method = { new EqualPredicate(),
054: new NotEqualPredicate(), new LessThanPredicate(),
055: new LessThanOrEqualPredicate(), new GreaterThanPredicate(),
056: new GreaterThanOrEqualPredicate(), new ContainsPredicate(),
057: new StartsWithPredicate(), };
058:
059: static List getNames() {
060: List names = new ArrayList();
061: for (int i = 0; i < method.length; i++) {
062: names.add(method[i].name);
063: }
064: return names;
065: }
066:
067: static AttributePredicate getPredicate(String name) {
068: for (int i = 0; i < method.length; i++) {
069: if (method[i].name.equals(name))
070: return method[i];
071: }
072: return null;
073: }
074:
075: private static FlexibleDateParser dateParser = new FlexibleDateParser();
076:
077: private String name;
078: private String description;
079:
080: public AttributePredicate(String name) {
081: this (name, null);
082: }
083:
084: public AttributePredicate(String name, String description) {
085: this .name = name;
086: this .description = description;
087: }
088:
089: public String getName() {
090: return name;
091: }
092:
093: public abstract boolean isTrue(Object arg1, Object arg2);
094:
095: protected boolean compareObjects(Object arg1, Object arg2) {
096: Object o2 = coerce((String) arg2, arg1);
097: if (o2 == null)
098: return false;
099: int comp = compareTo(arg1, o2);
100: if (comp == NOT_COMPARABLE)
101: return false;
102: return testCompareValue(comp);
103: }
104:
105: /**
106: * Subclasses calling compareObjects should override this method
107: * @param comp
108: * @return
109: */
110: protected boolean testCompareValue(int comp) {
111: return false;
112: }
113:
114: public static Object coerce(String constantValue, Object attrVal) {
115: try {
116: if (attrVal instanceof Boolean) {
117: return new Boolean(getBooleanLoose(constantValue));
118: }
119: if (attrVal instanceof Double) {
120: return new Double(constantValue);
121: }
122: if (attrVal instanceof Integer) {
123: return new Integer(constantValue);
124: }
125: if (attrVal instanceof String) {
126: return constantValue;
127: }
128: if (attrVal instanceof Date) {
129: return dateParser.parse(constantValue, true);
130: }
131: } catch (ParseException ex) {
132: // eat it
133: } catch (NumberFormatException ex) {
134: // eat it
135: }
136: // just return it as a String
137: return null;
138: }
139:
140: protected static final int NOT_COMPARABLE = Integer.MIN_VALUE;
141:
142: protected static int compareTo(Object o1, Object o2) {
143: if (o1 instanceof Boolean && o2 instanceof Boolean)
144: return ((Boolean) o1).equals(o2) ? 0 : 1;
145:
146: if (!(o1 instanceof Comparable))
147: return NOT_COMPARABLE;
148: if (!(o2 instanceof Comparable))
149: return NOT_COMPARABLE;
150: return ((Comparable) o1).compareTo((Comparable) o2);
151: }
152:
153: private static boolean getBooleanLoose(String boolStr) {
154: return boolStr.equalsIgnoreCase("true")
155: || boolStr.equalsIgnoreCase("yes")
156: || boolStr.equalsIgnoreCase("1")
157: || boolStr.equalsIgnoreCase("y");
158: }
159:
160: private static class EqualPredicate extends AttributePredicate {
161: public EqualPredicate() {
162: super ("=");
163: }
164:
165: public boolean isTrue(Object arg1, Object arg2) {
166: return compareObjects(arg1, arg2);
167: }
168:
169: protected boolean testCompareValue(int comp) {
170: return comp == 0;
171: }
172: }
173:
174: private static class NotEqualPredicate extends AttributePredicate {
175: public NotEqualPredicate() {
176: super ("<>");
177: }
178:
179: public boolean isTrue(Object arg1, Object arg2) {
180: return compareObjects(arg1, arg2);
181: }
182:
183: protected boolean testCompareValue(int comp) {
184: return comp != 0;
185: }
186: }
187:
188: private static class LessThanPredicate extends AttributePredicate {
189: public LessThanPredicate() {
190: super ("<");
191: }
192:
193: public boolean isTrue(Object arg1, Object arg2) {
194: return compareObjects(arg1, arg2);
195: }
196:
197: protected boolean testCompareValue(int comp) {
198: return comp < 0;
199: }
200: }
201:
202: private static class LessThanOrEqualPredicate extends
203: AttributePredicate {
204: public LessThanOrEqualPredicate() {
205: super ("<=");
206: }
207:
208: public boolean isTrue(Object arg1, Object arg2) {
209: return compareObjects(arg1, arg2);
210: }
211:
212: protected boolean testCompareValue(int comp) {
213: return comp <= 0;
214: }
215: }
216:
217: private static class GreaterThanPredicate extends
218: AttributePredicate {
219: public GreaterThanPredicate() {
220: super (">");
221: }
222:
223: public boolean isTrue(Object arg1, Object arg2) {
224: return compareObjects(arg1, arg2);
225: }
226:
227: protected boolean testCompareValue(int comp) {
228: return comp > 0;
229: }
230: }
231:
232: private static class GreaterThanOrEqualPredicate extends
233: AttributePredicate {
234: public GreaterThanOrEqualPredicate() {
235: super (">=");
236: }
237:
238: public boolean isTrue(Object arg1, Object arg2) {
239: return compareObjects(arg1, arg2);
240: }
241:
242: protected boolean testCompareValue(int comp) {
243: return comp >= 0;
244: }
245: }
246:
247: private static class ContainsPredicate extends AttributePredicate {
248: public ContainsPredicate() {
249: super (GenericNames.CONTAINS);
250: }
251:
252: public boolean isTrue(Object arg1, Object arg2) {
253: return arg1.toString().indexOf(arg2.toString()) >= 0;
254: }
255: }
256:
257: private static class StartsWithPredicate extends AttributePredicate {
258: public StartsWithPredicate() {
259: super (
260: I18N
261: .get("ui.plugin.analysis.AttributePredicate.starts-with"));
262: }
263:
264: public boolean isTrue(Object arg1, Object arg2) {
265: return arg1.toString().startsWith(arg2.toString());
266: }
267: }
268: }
|