Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]

    1   package org.apache.lucene.search.spans;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.IOException;
   21   
   22   import java.util.Collection;
   23   import java.util.Set;
   24   import java.util.ArrayList;
   25   
   26   import org.apache.lucene.index.IndexReader;
   27   import org.apache.lucene.index.Term;
   28   import org.apache.lucene.search.Query;
   29   import org.apache.lucene.util.ToStringUtils;
   30   
   31   /** Matches spans near the beginning of a field. */
   32   public class SpanFirstQuery extends SpanQuery implements Cloneable {
   33     private SpanQuery match;
   34     private int end;
   35   
   36     /** Construct a SpanFirstQuery matching spans in <code>match</code> whose end
   37      * position is less than or equal to <code>end</code>. */
   38     public SpanFirstQuery(SpanQuery match, int end) {
   39       this.match = match;
   40       this.end = end;
   41     }
   42   
   43     /** Return the SpanQuery whose matches are filtered. */
   44     public SpanQuery getMatch() { return match; }
   45   
   46     /** Return the maximum end position permitted in a match. */
   47     public int getEnd() { return end; }
   48   
   49     @Override
   50     public String getField() { return match.getField(); }
   51   
   52     @Override
   53     public String toString(String field) {
   54       StringBuilder buffer = new StringBuilder();
   55       buffer.append("spanFirst(");
   56       buffer.append(match.toString(field));
   57       buffer.append(", ");
   58       buffer.append(end);
   59       buffer.append(")");
   60       buffer.append(ToStringUtils.boost(getBoost()));
   61       return buffer.toString();
   62     }
   63     
   64     @Override
   65     public Object clone() {
   66       SpanFirstQuery spanFirstQuery = new SpanFirstQuery((SpanQuery) match.clone(), end);
   67       spanFirstQuery.setBoost(getBoost());
   68       return spanFirstQuery;
   69     }
   70     
   71     @Override
   72     public void extractTerms(Set<Term> terms) {
   73   	    match.extractTerms(terms);
   74     }
   75   
   76     @Override
   77     public Spans getSpans(final IndexReader reader) throws IOException {
   78       return new Spans() {
   79           private Spans spans = match.getSpans(reader);
   80   
   81           @Override
   82           public boolean next() throws IOException {
   83             while (spans.next()) {                  // scan to next match
   84               if (end() <= end)
   85                 return true;
   86             }
   87             return false;
   88           }
   89   
   90           @Override
   91           public boolean skipTo(int target) throws IOException {
   92             if (!spans.skipTo(target))
   93               return false;
   94   
   95             return spans.end() <= end || next();
   96   
   97           }
   98   
   99           @Override
  100           public int doc() { return spans.doc(); }
  101           @Override
  102           public int start() { return spans.start(); }
  103           @Override
  104           public int end() { return spans.end(); }
  105   
  106         // TODO: Remove warning after API has been finalized
  107         @Override
  108         public Collection<byte[]> getPayload() throws IOException {
  109           ArrayList<byte[]> result = null;
  110           if (spans.isPayloadAvailable()) {
  111             result = new ArrayList<byte[]>(spans.getPayload());
  112           }
  113           return result;//TODO: any way to avoid the new construction?
  114         }
  115   
  116         // TODO: Remove warning after API has been finalized
  117         @Override
  118         public boolean isPayloadAvailable() {
  119           return spans.isPayloadAvailable();
  120         }
  121   
  122         @Override
  123         public String toString() {
  124             return "spans(" + SpanFirstQuery.this.toString() + ")";
  125           }
  126   
  127         };
  128     }
  129   
  130     @Override
  131     public Query rewrite(IndexReader reader) throws IOException {
  132       SpanFirstQuery clone = null;
  133   
  134       SpanQuery rewritten = (SpanQuery) match.rewrite(reader);
  135       if (rewritten != match) {
  136         clone = (SpanFirstQuery) this.clone();
  137         clone.match = rewritten;
  138       }
  139   
  140       if (clone != null) {
  141         return clone;                        // some clauses rewrote
  142       } else {
  143         return this;                         // no clauses rewrote
  144       }
  145     }
  146   
  147     @Override
  148     public boolean equals(Object o) {
  149       if (this == o) return true;
  150       if (!(o instanceof SpanFirstQuery)) return false;
  151   
  152       SpanFirstQuery other = (SpanFirstQuery)o;
  153       return this.end == other.end
  154            && this.match.equals(other.match)
  155            && this.getBoost() == other.getBoost();
  156     }
  157   
  158     @Override
  159     public int hashCode() {
  160       int h = match.hashCode();
  161       h ^= (h << 8) | (h >>> 25);  // reversible
  162       h ^= Float.floatToRawIntBits(getBoost()) ^ end;
  163       return h;
  164     }
  165   
  166   
  167   }

Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]