DistinctValuesFacetExtractor.java
3.77 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.jcr.ValueFactory
*/
package com.day.cq.search.facets.extractors;
import com.day.cq.search.Predicate;
import com.day.cq.search.facets.Bucket;
import com.day.cq.search.facets.Facet;
import com.day.cq.search.facets.buckets.SimpleBucket;
import com.day.cq.search.facets.extractors.FacetImpl;
import com.day.cq.search.facets.extractors.PropertyFacetExtractor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
public class DistinctValuesFacetExtractor
extends PropertyFacetExtractor {
private final String nodePathFilter;
private final Pattern nodePathPattern;
private Predicate predicateTemplate;
private String valueParameterName;
private Map<String, SimpleBucket> bucketMap = new HashMap<String, SimpleBucket>();
public DistinctValuesFacetExtractor(String propertyRelPath, String nodePathFilter, Predicate predicateTemplate, String valueParameterName) {
super(propertyRelPath);
this.predicateTemplate = predicateTemplate;
this.valueParameterName = valueParameterName;
this.nodePathFilter = nodePathFilter == null ? "(.*)" : nodePathFilter;
this.nodePathPattern = Pattern.compile(this.nodePathFilter);
}
@Override
public Facet getFacet() {
if (this.bucketMap.size() == 0) {
return null;
}
return new FacetImpl(this.bucketMap.values());
}
@Override
protected void handleValue(Value value) throws RepositoryException {
String val = this.getBucketValue(value.getString());
if (val == null) {
return;
}
SimpleBucket b = this.bucketMap.get(val);
if (b == null) {
Predicate p = this.predicateTemplate.clone();
p.set(this.valueParameterName, val);
b = new SimpleBucket(p, val);
this.bucketMap.put(val, b);
}
b.increment();
}
protected String getBucketValue(String value) {
return value;
}
@Override
protected List<Value> filter(List<Value> values, ValueFactory vf) throws RepositoryException {
if (this.nodePathPattern == null) {
return values;
}
ArrayList<Value> filtered = new ArrayList<Value>(values.size());
for (Value value : values) {
Matcher m = this.nodePathPattern.matcher(value.getString());
if (!m.matches()) continue;
for (int g = 1; g <= m.groupCount(); ++g) {
String s = m.group(g);
if (s == null) continue;
filtered.add(vf.createValue(s));
}
}
return filtered;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
DistinctValuesFacetExtractor other = (DistinctValuesFacetExtractor)obj;
if (this.propertyRelPath != other.propertyRelPath && !this.propertyRelPath.equals(other.propertyRelPath)) {
return false;
}
if (this.nodePathFilter != other.nodePathFilter && !this.nodePathFilter.equals(other.nodePathFilter)) {
return false;
}
return true;
}
public int hashCode() {
int hash = 7;
hash = 31 * hash + (this.propertyRelPath == null ? 0 : this.propertyRelPath.hashCode());
hash = 31 * hash + (this.nodePathFilter == null ? 0 : this.nodePathFilter.hashCode());
return hash;
}
}