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