WordOnlineSearcher.java :  » Search » fuzzy-search-tools » ru » fuzzysearch » Java Open Source

Java Open Source » Search » fuzzy search tools 
fuzzy search tools » ru » fuzzysearch » WordOnlineSearcher.java
package ru.fuzzysearch;

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

public abstract class WordOnlineSearcher implements OnlineSearcher {

  /**
   *       
   */
  protected static interface Visitor {

    public void read(CharSequence string, int index);
  }

  /**
   *    reader'.
   * 
   * @param reader
   *             
   * @param visitor
   *                 .
   */
  protected void readText(Reader reader, Visitor visitor) {
    try {
      StringBuilder stringBuilder = new StringBuilder();
      int readerIndex = 0;

      int sym;
      while ((sym = reader.read()) >= 0) {
        char ch = (char) sym;
        if (Character.isLetterOrDigit(ch))
          stringBuilder.append(Character.toUpperCase(ch));
        else if (stringBuilder.length() > 0) {
          visitor.read(stringBuilder, readerIndex - stringBuilder.length());
          stringBuilder.setLength(0);
        }
        ++readerIndex;
      }
      if (stringBuilder.length() > 0) visitor.read(stringBuilder, readerIndex - stringBuilder.length());
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.