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