001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.impl;
018:
019: import org.compass.core.CompassDetachedHits;
020: import org.compass.core.CompassException;
021: import org.compass.core.CompassHighlightedText;
022: import org.compass.core.CompassHit;
023: import org.compass.core.CompassQuery;
024: import org.compass.core.Resource;
025: import org.compass.core.mapping.AliasMapping;
026: import org.compass.core.mapping.osem.ClassMapping;
027: import org.compass.core.spi.InternalCompassHits;
028: import org.compass.core.spi.InternalCompassSession;
029:
030: /**
031: * @author kimchy
032: */
033: public class DefaultCompassDetachedHits extends AbstractCompassHits
034: implements CompassDetachedHits {
035:
036: private int length;
037:
038: private int totalLength;
039:
040: private Resource[] resources;
041:
042: private Object[] datas;
043:
044: private float[] scores;
045:
046: private CompassHit[] hits;
047:
048: private CompassHighlightedText[] highlightedText;
049:
050: private CompassQuery query;
051:
052: private CompassQuery suggestedQuery;
053:
054: public DefaultCompassDetachedHits(InternalCompassHits hits,
055: InternalCompassSession session, int from, int size,
056: CompassQuery query, CompassQuery suggestedQuery)
057: throws CompassException, IllegalArgumentException {
058: this .query = query;
059: this .suggestedQuery = suggestedQuery;
060: this .length = size;
061: if (from < 0) {
062: throw new IllegalArgumentException(
063: "Can't preload with negative from [" + from + "]");
064: }
065: if ((from + size) > hits.getLength()) {
066: this .length = hits.getLength() - from;
067: }
068: if (this .length < 0) {
069: this .length = 0;
070: }
071: this .totalLength = hits.getLength();
072: resources = new Resource[this .length];
073: scores = new float[this .length];
074: datas = new Object[this .length];
075: this .hits = new CompassHit[this .length];
076: highlightedText = new CompassHighlightedText[this .length];
077: for (int i = 0; i < this .length; i++) {
078: int location = from + i;
079: resources[i] = hits.resource(location);
080: scores[i] = hits.score(location);
081: this .hits[i] = new DefaultCompassHit(this , i);
082: highlightedText[i] = hits.highlightedText(location);
083: AliasMapping aliasMapping = session.getMapping()
084: .getAliasMapping(resources[i].getAlias());
085: if (aliasMapping instanceof ClassMapping) {
086: datas[i] = session.getByResource(resources[i]);
087: }
088: }
089: }
090:
091: public CompassQuery getQuery() {
092: return this .query;
093: }
094:
095: public CompassQuery getSuggestedQuery() {
096: return this .suggestedQuery;
097: }
098:
099: public float score(int n) throws CompassException,
100: IllegalArgumentException {
101: return scores[n];
102: }
103:
104: public Resource resource(int n) throws CompassException,
105: IllegalArgumentException {
106: return resources[n];
107: }
108:
109: public Object data(int n) throws CompassException,
110: IllegalArgumentException {
111: return datas[n];
112: }
113:
114: public CompassHighlightedText highlightedText(int n)
115: throws CompassException {
116: return highlightedText[n];
117: }
118:
119: public CompassHit hit(int n) throws CompassException {
120: return hits[n];
121: }
122:
123: public int getLength() {
124: return length;
125: }
126:
127: public int getTotalLength() {
128: return totalLength;
129: }
130:
131: public int totalLength() {
132: return totalLength;
133: }
134:
135: public Resource[] getResources() throws CompassException {
136: return resources;
137: }
138:
139: public Object[] getDatas() throws CompassException {
140: return datas;
141: }
142:
143: public CompassHit[] getHits() throws CompassException {
144: return hits;
145: }
146:
147: }
|