001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search.filter;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    
019    import java.util.Arrays;
020    import java.util.HashSet;
021    import java.util.Set;
022    
023    /**
024     * @author Michael C. Han
025     */
026    public class TermsFilter extends BaseFilter {
027    
028            public TermsFilter(String field) {
029                    _field = field;
030            }
031    
032            @Override
033            public <T> T accept(FilterVisitor<T> filterVisitor) {
034                    return filterVisitor.visit(this);
035            }
036    
037            public void addValue(String value) {
038                    _values.add(value);
039            }
040    
041            public void addValues(String... values) {
042                    _values.addAll(Arrays.asList(values));
043            }
044    
045            public String getField() {
046                    return _field;
047            }
048    
049            @Override
050            public int getSortOrder() {
051                    return 4;
052            }
053    
054            public String[] getValues() {
055                    return _values.toArray(new String[_values.size()]);
056            }
057    
058            public boolean isEmpty() {
059                    return _values.isEmpty();
060            }
061    
062            @Override
063            public String toString() {
064                    StringBundler sb = new StringBundler(7);
065    
066                    sb.append("{(");
067                    sb.append(_field);
068                    sb.append("=");
069                    sb.append(_values);
070                    sb.append("), ");
071                    sb.append(super.toString());
072                    sb.append("}");
073    
074                    return sb.toString();
075            }
076    
077            private final String _field;
078            private final Set<String> _values = new HashSet<>();
079    
080    }