Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.lucene.analysis;
   19   
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   
   23   /**
   24    * Base utility class for implementing a {@link CharFilter}.
   25    * You subclass this, and then record mappings by calling
   26    * {@link #addOffCorrectMap}, and then invoke the correct
   27    * method to correct an offset.
   28    *
   29    * <p><b>NOTE</b>: This class is not particularly efficient.
   30    * For example, a new class instance is created for every
   31    * call to {@link #addOffCorrectMap}, which is then appended
   32    * to a private list.
   33    */
   34   public abstract class BaseCharFilter extends CharFilter {
   35   
   36     private List<OffCorrectMap> pcmList;
   37     
   38     public BaseCharFilter(CharStream in) {
   39       super(in);
   40     }
   41   
   42     /** Retrieve the corrected offset.  Note that this method
   43      *  is slow, if you correct positions far before the most
   44      *  recently added position, as it's a simple linear
   45      *  search backwards through all offset corrections added
   46      *  by {@link #addOffCorrectMap}. */
   47     @Override
   48     protected int correct(int currentOff) {
   49       if (pcmList == null || pcmList.isEmpty()) {
   50         return currentOff;
   51       }
   52       for (int i = pcmList.size() - 1; i >= 0; i--) {
   53         if (currentOff >=  pcmList.get(i).off) {
   54           return currentOff + pcmList.get(i).cumulativeDiff;
   55         }
   56       }
   57       return currentOff;
   58     }
   59     
   60     protected int getLastCumulativeDiff() {
   61       return pcmList == null || pcmList.isEmpty() ?
   62         0 : pcmList.get(pcmList.size() - 1).cumulativeDiff;
   63     }
   64   
   65     protected void addOffCorrectMap(int off, int cumulativeDiff) {
   66       if (pcmList == null) {
   67         pcmList = new ArrayList<OffCorrectMap>();
   68       }
   69       pcmList.add(new OffCorrectMap(off, cumulativeDiff));
   70     }
   71   
   72     static class OffCorrectMap {
   73   
   74       int off;
   75       int cumulativeDiff;
   76   
   77       OffCorrectMap(int off, int cumulativeDiff) {
   78         this.off = off;
   79         this.cumulativeDiff = cumulativeDiff;
   80       }
   81   
   82       @Override
   83       public String toString() {
   84         StringBuilder sb = new StringBuilder();
   85         sb.append('(');
   86         sb.append(off);
   87         sb.append(',');
   88         sb.append(cumulativeDiff);
   89         sb.append(')');
   90         return sb.toString();
   91       }
   92     }
   93   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]