source: vlo/branches/vlo-3.3-oeaw/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/pojo/FacetSelection.java @ 6788

Last change on this file since 6788 was 6788, checked in by davor.ostojic@oeaw.ac.at, 9 years ago
File size: 4.2 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 com.google.common.collect.Lists;
20import java.io.Serializable;
21import java.util.ArrayList;
22import java.util.Collection;
23
24/**
25 * Represents the selection for a single facet
26 *
27 * @author twagoo
28 */
29public class FacetSelection implements Serializable {
30
31    private FacetSelectionType selectionType;
32    private final Collection<String> values;
33
34    /**
35     * Creates an {@link FacetSelectionType#AND} selection for the specified
36     * values
37     *
38     * @param values
39     */
40    public FacetSelection(Collection<String> values) {
41        this(FacetSelectionType.AND, values);
42    }
43
44    /**
45     * Creates an empty selection with the specified type
46     *
47     * @param type
48     */
49    public FacetSelection(FacetSelectionType type) {
50        this(type, Lists.<String>newArrayList());
51    }
52
53    public FacetSelection(FacetSelectionType selectionType, Collection<String> values) {
54        this.selectionType = selectionType; //values.size() > 1? FacetSelectionType.OR : FacetSelectionType.AND;
55        // always store as array list, which is modifiable and serialisable
56        if (values instanceof ArrayList) {
57            this.values = values;
58        } else {
59            // copy to new list
60            this.values = Lists.newArrayList(values);
61        }
62    }
63
64    /**
65     *
66     * @return type of selection
67     */
68    public FacetSelectionType getSelectionType() {
69        return selectionType;
70    }
71
72    /**
73     *
74     * @return values subject to selection type
75     */
76    public Collection<String> getValues() {
77        return values;
78    }
79   
80    public void mergeValues(Collection<String> values){
81        if(values == null || values.isEmpty())
82                return;
83        for(String val: values)
84                if(!this.values.contains(val))
85                        this.values.add(val);
86       
87        //change type of selection to OR if more values are selected
88        if(this.values.size() > 1)
89                this.selectionType = FacetSelectionType.OR;
90       
91    }
92   
93    public void removeValues(Collection<String> valuesToBeRemoved){
94        if(valuesToBeRemoved != null){
95                for(String val: valuesToBeRemoved){
96                        this.values.remove(val);
97                }
98        }
99       
100        if(this.values.size() <= 1)
101                this.selectionType = FacetSelectionType.AND;
102    }
103   
104
105    public FacetSelection getCopy() {
106        return new FacetSelection(selectionType, values);
107    }
108
109    @Override
110    public int hashCode() {
111        int hash = 3;
112        hash = 79 * hash + (this.selectionType != null ? this.selectionType.hashCode() : 0);
113        hash = 79 * hash + (this.values != null ? this.values.hashCode() : 0);
114        return hash;
115    }
116
117    @Override
118    public boolean equals(Object obj) {
119        if (obj == null) {
120            return false;
121        }
122        if (getClass() != obj.getClass()) {
123            return false;
124        }
125        final FacetSelection other = (FacetSelection) obj;
126        if (this.selectionType != other.selectionType) {
127            return false;
128        }
129        if (this.values != other.values && (this.values == null || !this.values.equals(other.values))) {
130            return false;
131        }
132        return true;
133    }
134
135    /**
136     *
137     * @return whether this instance represents an actual selection
138     */
139    public boolean isEmpty() {
140        return selectionType != FacetSelectionType.NOT_EMPTY // 'not empty' does not require any value, other types do
141                && (values == null || values.isEmpty());
142    }
143
144    @Override
145    public String toString() {
146        return String.format("%s: %s", selectionType, values);
147    }
148
149}
Note: See TracBrowser for help on using the repository browser.