ValueRangeBucket.java
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Value
*/
package com.day.cq.search.facets.buckets;
import com.day.cq.search.Predicate;
import com.day.cq.search.facets.buckets.PredefinedBucket;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
public class ValueRangeBucket
extends PredefinedBucket {
private final Comparable from;
private final Comparable to;
private boolean fromIncluded;
private boolean toIncluded;
public ValueRangeBucket(String name, Comparable from, boolean fromIncluded, Comparable to, boolean toIncluded, Predicate predicate) {
super(predicate, name);
this.from = from;
this.to = to;
this.fromIncluded = fromIncluded;
this.toIncluded = toIncluded;
}
public Comparable getFrom() {
return this.from;
}
public Comparable getTo() {
return this.to;
}
@Override
public void acceptValue(Value value) throws RepositoryException {
if (this.matches(this.valueToComparable(value))) {
this.increment();
}
}
protected Comparable valueToComparable(Value value) throws RepositoryException {
switch (value.getType()) {
case 5: {
return Long.valueOf(value.getLong());
}
case 4: {
return Double.valueOf(value.getDouble());
}
case 3: {
return Long.valueOf(value.getLong());
}
}
return value.getString();
}
protected boolean matches(Comparable c) {
try {
Comparable value = this.coerce(c);
if (value == null) {
return false;
}
if (this.from == null && this.to == null) {
return true;
}
if (this.from != null && (this.fromIncluded ? this.from.compareTo(value) > 0 : this.from.compareTo(value) >= 0)) {
return false;
}
if (this.to != null && (this.toIncluded ? this.to.compareTo(value) < 0 : this.to.compareTo(value) <= 0)) {
return false;
}
return true;
}
catch (ClassCastException e) {
return false;
}
}
private Comparable coerce(Comparable value) {
return this.coerceValue(this.from != null ? this.from.getClass() : this.to.getClass(), value);
}
protected Comparable coerceValue(Class<? extends Comparable> type, Comparable value) {
if (value.getClass() == type) {
return value;
}
if (type == Long.class) {
try {
return Long.valueOf(Long.parseLong(value.toString()));
}
catch (NumberFormatException e) {
return null;
}
}
if (type == Integer.class) {
try {
return Integer.valueOf(Integer.parseInt(value.toString()));
}
catch (NumberFormatException e) {
return null;
}
}
if (type == Double.class) {
try {
return Double.valueOf(Double.parseDouble(value.toString()));
}
catch (NumberFormatException e) {
return null;
}
}
if (type == String.class) {
return value.toString();
}
return null;
}
}