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;
016    
017    import com.liferay.portal.kernel.json.JSON;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Bruno Farache
030     */
031    public class HitsImpl implements Hits {
032    
033            public HitsImpl() {
034            }
035    
036            @Override
037            public void addGroupedHits(String groupValue, Hits hits) {
038                    _groupedHits.put(groupValue, hits);
039            }
040    
041            @Override
042            public void addStatsResults(StatsResults statsResults) {
043                    _statsResults.put(statsResults.getField(), statsResults);
044            }
045    
046            @Override
047            public void copy(Hits hits) {
048                    setDocs(hits.getDocs());
049                    setLength(hits.getLength());
050                    setQuery(hits.getQuery());
051                    setQuerySuggestions(hits.getQuerySuggestions());
052                    setQueryTerms(hits.getQueryTerms());
053                    setScores(hits.getScores());
054                    setSearchTime(hits.getSearchTime());
055                    setSnippets(hits.getSnippets());
056                    setSpellCheckResults(hits.getSpellCheckResults());
057                    setStart(hits.getStart());
058            }
059    
060            @Override
061            public Document doc(int n) {
062                    return _docs[n];
063            }
064    
065            @JSON
066            @Override
067            public String getCollatedSpellCheckResult() {
068                    return _collatedSpellCheckResult;
069            }
070    
071            @JSON
072            @Override
073            public Document[] getDocs() {
074                    return _docs;
075            }
076    
077            @Override
078            public Map<String, Hits> getGroupedHits() {
079                    return Collections.unmodifiableMap(_groupedHits);
080            }
081    
082            @Override
083            public int getLength() {
084                    return _length;
085            }
086    
087            @JSON(include = false)
088            @Override
089            public Query getQuery() {
090                    return _query;
091            }
092    
093            @JSON
094            @Override
095            public String[] getQuerySuggestions() {
096                    if (ArrayUtil.isEmpty(_querySuggestions)) {
097                            return StringPool.EMPTY_ARRAY;
098                    }
099    
100                    return _querySuggestions;
101            }
102    
103            @JSON
104            @Override
105            public String[] getQueryTerms() {
106                    return _queryTerms;
107            }
108    
109            @JSON
110            @Override
111            public float[] getScores() {
112                    return _scores;
113            }
114    
115            @Override
116            public float getSearchTime() {
117                    return _searchTime;
118            }
119    
120            @JSON
121            @Override
122            public String[] getSnippets() {
123                    return _snippets;
124            }
125    
126            @Override
127            public Map<String, List<String>> getSpellCheckResults() {
128                    return _spellCheckResults;
129            }
130    
131            @Override
132            public long getStart() {
133                    return _start;
134            }
135    
136            @Override
137            public Map<String, StatsResults> getStatsResults() {
138                    return Collections.unmodifiableMap(_statsResults);
139            }
140    
141            @Override
142            public boolean hasGroupedHits() {
143                    return !_groupedHits.isEmpty();
144            }
145    
146            @Override
147            public float score(int n) {
148                    return _scores[n];
149            }
150    
151            @Override
152            public void setCollatedSpellCheckResult(String collatedSpellCheckResult) {
153                    _collatedSpellCheckResult = collatedSpellCheckResult;
154            }
155    
156            @Override
157            public void setDocs(Document[] docs) {
158                    _docs = docs;
159            }
160    
161            @Override
162            public void setLength(int length) {
163                    _length = length;
164            }
165    
166            @Override
167            public void setQuery(Query query) {
168                    _query = query;
169            }
170    
171            @Override
172            public void setQuerySuggestions(String[] querySuggestions) {
173                    _querySuggestions = querySuggestions;
174            }
175    
176            @Override
177            public void setQueryTerms(String[] queryTerms) {
178                    _queryTerms = queryTerms;
179            }
180    
181            @Override
182            public void setScores(float[] scores) {
183                    _scores = scores;
184            }
185    
186            @Override
187            public void setSearchTime(float time) {
188                    _searchTime = time;
189            }
190    
191            @Override
192            public void setSnippets(String[] snippets) {
193                    _snippets = snippets;
194            }
195    
196            @Override
197            public void setSpellCheckResults(
198                    Map<String, List<String>> spellCheckResults) {
199    
200                    _spellCheckResults = spellCheckResults;
201            }
202    
203            @Override
204            public void setStart(long start) {
205                    _start = start;
206            }
207    
208            @Override
209            public String snippet(int n) {
210                    return _snippets[n];
211            }
212    
213            @Override
214            public List<Document> toList() {
215                    List<Document> subset = new ArrayList<>(_docs.length);
216    
217                    for (Document _doc : _docs) {
218                            subset.add(_doc);
219                    }
220    
221                    return subset;
222            }
223    
224            private String _collatedSpellCheckResult;
225            private Document[] _docs;
226            private final Map<String, Hits> _groupedHits = new HashMap<>();
227            private int _length;
228            private Query _query;
229            private String[] _querySuggestions;
230            private String[] _queryTerms;
231            private float[] _scores = new float[0];
232            private float _searchTime;
233            private String[] _snippets = {};
234            private Map<String, List<String>> _spellCheckResults;
235            private long _start;
236            private final Map<String, StatsResults> _statsResults = new HashMap<>();
237    
238    }