source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/pojo/FieldValuesFilter.java @ 4951

Last change on this file since 4951 was 4951, checked in by Twan Goosen, 10 years ago

name matching in field value filter: replace StringUtils? with RegEx? (compiled once per value)

File size: 2.6 KB
Line 
1/*
2 * Copyright (C) 2014 CLARIN
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package eu.clarin.cmdi.vlo.pojo;
18
19import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldValuesProvider;
20import java.io.Serializable;
21import java.util.regex.Pattern;
22import org.apache.solr.client.solrj.response.FacetField.Count;
23
24/**
25 * Defines a filter for field values (designed to be used by
26 * {@link FacetFieldValuesProvider})
27 *
28 * @author twagoo
29 */
30public class FieldValuesFilter implements Serializable {
31
32    private String name;
33    private Pattern namePattern;
34    private int minimalOccurence;
35
36    public String getName() {
37        return name;
38    }
39
40    /**
41     *
42     * @param name string that matches should <em>contain</em>
43     */
44    public void setName(String name) {
45        this.name = name;
46        this.namePattern = createNamePattern(name);
47    }
48
49    public int getMinimalOccurence() {
50        return minimalOccurence;
51    }
52
53    /**
54     *
55     * @param minimalOccurence minimal number of occurrences matches should have
56     */
57    public void setMinimalOccurence(int minimalOccurence) {
58        this.minimalOccurence = minimalOccurence;
59    }
60
61    /**
62     *
63     * @param count count (name + count) to check
64     * @return true IFF the {@link Count#getCount() } is more than {@link #getMinimalOccurence()
65     * } {@link Count#getName() } contains {@link #getName() } (case
66     * insensitive)
67     */
68    public boolean matches(Count count) {
69        return count.getCount() >= minimalOccurence
70                && (namePattern == null || namePattern.matcher(count.getName()).find());
71    }
72
73    public boolean isEmpty() {
74        return minimalOccurence == 0 && (name == null || name.isEmpty());
75    }
76
77    private Pattern createNamePattern(String name) {
78        if (name == null) {
79            return null;
80        } else {
81            // make a matching pattern for the name (case insensitive, not parsing RegEx syntax and supporting unicode)
82            return Pattern.compile(name, Pattern.LITERAL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
83        }
84    }
85}
Note: See TracBrowser for help on using the repository browser.