01: package org.geotools.filter.function;
02:
03: /**
04: * Classifies into ranges of minimum and maximum values.
05: *
06: * @author Cory Horner, Refractions Research
07: *
08: */
09: public final class RangedClassifier extends Classifier {
10:
11: Comparable min[];
12: Comparable max[];
13:
14: public RangedClassifier(Comparable min[], Comparable max[]) {
15: this .min = min;
16: this .max = max;
17: //initialize titles
18: this .titles = new String[min.length];
19: for (int i = 0; i < titles.length; i++) {
20: titles[i] = truncateZeros(min[i].toString()) + ".."
21: + truncateZeros(max[i].toString());
22: }
23: }
24:
25: private String truncateZeros(String str) {
26: if (str.indexOf(".") > -1) {
27: while (str.endsWith("0")) {
28: str = str.substring(0, str.length() - 1);
29: }
30: if (str.endsWith(".")) {
31: str = str.substring(0, str.length() - 1);
32: }
33: }
34: return str;
35: }
36:
37: public int getSize() {
38: return Math.min(min.length, max.length);
39: }
40:
41: public Object getMin(int slot) {
42: return min[slot];
43: }
44:
45: public Object getMax(int slot) {
46: return max[slot];
47: }
48:
49: public int classify(Object value) {
50: return classify((Comparable) value);
51: }
52:
53: private int classify(Comparable val) {
54: Comparable value = val;
55: if (val instanceof Integer) { //convert to double as java is stupid
56: value = new Double(((Integer) val).intValue());
57: }
58: //check each slot and see if: min <= value < max
59: int last = min.length - 1;
60: for (int i = 0; i <= last; i++) {
61: Comparable localMin = (Comparable) this .min[i];
62: Comparable localMax = (Comparable) this .max[i];
63:
64: if (localMin.compareTo(value) < 1
65: && localMax.compareTo(value) > 0) {
66: return i;
67: }
68: }
69: if (max[last].compareTo(value) == 0) { //if value = max, put it in the last slot
70: return last;
71: }
72: return -1;
73: }
74:
75: }
|