/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.analysis;

import org.apache.lucene.analysis.NumericTokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.solr.common.SolrException;
import org.apache.solr.schema.DateField;
import static org.apache.solr.schema.TrieField.TrieTypes;

import java.io.IOException;
import java.io.Reader;

/**
 * Tokenizer for trie fields. It uses NumericTokenStream to create multiple trie encoded string per number.
 * Each string created by this tokenizer for a given number differs from the previous by the given precisionStep.
 * For query time token streams that only contain the highest precision term, use 32/64 as precisionStep.
 * <p/>
 * Refer to {@link org.apache.lucene.search.NumericRangeQuery} for more details.
 *
 * @version $Id$
 * @see org.apache.lucene.search.NumericRangeQuery
 * @see org.apache.solr.schema.TrieField
 * @since solr 1.4
 */
public class TrieTokenizerFactory extends BaseTokenizerFactory {
  protected final int precisionStep;
  protected final TrieTypes type;

  public TrieTokenizerFactory(TrieTypes type, int precisionStep) {
    this.type = type;
    this.precisionStep = precisionStep;
  }

  public TrieTokenizer create(Reader input) {
    return new TrieTokenizer(input, type, precisionStep, TrieTokenizer.getNumericTokenStream(precisionStep));
  }
}

final class TrieTokenizer extends Tokenizer {
  protected static final DateField dateField = new DateField();
  protected final int precisionStep;
  protected final TrieTypes type;
  protected final NumericTokenStream ts;

  static NumericTokenStream getNumericTokenStream(int precisionStep) {
    return new NumericTokenStream(precisionStep);
  }

  public TrieTokenizer(Reader input, TrieTypes type, int precisionStep, NumericTokenStream ts) {
    // must share the attribute source with the NumericTokenStream we delegate to
    super(ts);
    this.type = type;
    this.precisionStep = precisionStep;
    this.ts = ts;

   try {
     reset(input);
   } catch (IOException e) {
     throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to create TrieIndexTokenizer", e);
   }
  }

  @Override
  public void reset(Reader input) throws IOException {
   try {
      super.reset(input);
      input = super.input;
      char[] buf = new char[32];
      int len = input.read(buf);
      String v = new String(buf, 0, len);
      switch (type) {
        case INTEGER:
          ts.setIntValue(Integer.parseInt(v));
          break;
        case FLOAT:
          ts.setFloatValue(Float.parseFloat(v));
          break;
        case LONG:
          ts.setLongValue(Long.parseLong(v));
          break;
        case DOUBLE:
          ts.setDoubleValue(Double.parseDouble(v));
          break;
        case DATE:
          ts.setLongValue(dateField.parseMath(null, v).getTime());
          break;
        default:
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
      }
    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to create TrieIndexTokenizer", e);
    }

    ts.reset();
  }


  @Override
  public boolean incrementToken() throws IOException {
    return ts.incrementToken();
  }
}

@@ -885,16 +885,83 @@
 Stream;%0A
+import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;%0A
 import o
@@ -2401,24 +2401,141 @@
 enStream ts;
+%0A  %0A  protected final OffsetAttribute ofsAtt = addAttribute(OffsetAttribute.class);%0A  protected int startOfs, endOfs;
 %0A%0A  static N
@@ -3297,16 +3297,96 @@
 d(buf);%0A
+      this.startOfs = correctOffset(0);%0A      this.endOfs = correctOffset(len);%0A
       St
@@ -4164,16 +4164,191 @@
 ;%0A    %7D%0A
+  %7D%0A%0A  @Override%0A  public void close() throws IOException %7B%0A    super.close();%0A    ts.close();%0A  %7D%0A  %0A  @Override%0A  public void reset() throws IOException %7B%0A    super.reset();
 %0A    ts.
@@ -4359,17 +4359,16 @@
 t();%0A  %7D
-%0A
 %0A%0A  @Ove
@@ -4432,23 +4432,20 @@
 n %7B%0A    
-return 
+if (
 ts.incre
@@ -4455,16 +4455,213 @@
 tToken()
+) %7B%0A      ofsAtt.setOffset(startOfs, endOfs);%0A      return true;%0A    %7D%0A    return false;%0A  %7D%0A%0A  @Override%0A  public void end() throws IOException %7B%0A    ts.end();%0A    ofsAtt.setOffset(endOfs, endOfs)
 ;%0A  %7D%0A%7D%0A

Fix some minor issues in TrieTokenizer and also add support for offsets (same way like KeywordTokenizer)