home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (NewCollationAnalyzerTask.java)

This example Lucene source code file (NewCollationAnalyzerTask.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

analyzer, analyzer, class, exception, implementation, implementation, locale, newcollationanalyzertask, override, reflection, runtimeexception, string, string, stringtokenizer, stringtokenizer, util

The Lucene NewCollationAnalyzerTask.java source code

package org.apache.lucene.benchmark.byTask.tasks;

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.StringTokenizer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.benchmark.byTask.PerfRunData;

 * Task to support benchmarking collation.
 * <p>
 * <ul>
 *  <li> NewCollationAnalyzer with the default jdk impl
 *  <li> NewCollationAnalyzer(impl:icu) specify an impl (jdk,icu)
 * </ul>
 * </p>
public class NewCollationAnalyzerTask extends PerfTask {
  public enum Implementation { 
    String className;
    String collatorClassName;
    Implementation(String className, String collatorClassName) {
      this.className = className;
      this.collatorClassName = collatorClassName;
  private Implementation impl = Implementation.JDK;

  public NewCollationAnalyzerTask(PerfRunData runData) {

  static Analyzer createAnalyzer(Locale locale, Implementation impl)
      throws Exception {
    final Class<?> collatorClazz = Class.forName(impl.collatorClassName);
    Method collatorMethod = collatorClazz.getMethod("getInstance",
        new Class[] {Locale.class});
    Object collator = collatorMethod.invoke(null, locale);
    final Class<? extends Analyzer> clazz = Class.forName(impl.className)
    Constructor<? extends Analyzer> ctor = clazz.getConstructor(collatorClazz);
    return ctor.newInstance(collator);
  public int doLogic() throws Exception {
    try {
      Locale locale = getRunData().getLocale();
      if (locale == null) throw new RuntimeException(
          "Locale must be set with the NewLocale task!");
      Analyzer analyzer = createAnalyzer(locale, impl);
      System.out.println("Changed Analyzer to: "
          + analyzer.getClass().getName() + "(" + locale + ")");
    } catch (Exception e) {
      throw new RuntimeException("Error creating Analyzer: impl=" + impl, e);
    return 1;
  public void setParams(String params) {
    StringTokenizer st = new StringTokenizer(params, ",");
    while (st.hasMoreTokens()) {
      String param = st.nextToken();
      StringTokenizer expr = new StringTokenizer(param, ":");
      String key = expr.nextToken();
      String value = expr.nextToken();
      // for now we only support the "impl" parameter.
      // TODO: add strength, decomposition, etc
      if (key.equals("impl")) {
        if (value.equalsIgnoreCase("icu"))
          impl = Implementation.ICU;
        else if (value.equalsIgnoreCase("jdk"))
          impl = Implementation.JDK;
          throw new RuntimeException("Unknown parameter " + param);
      } else {
        throw new RuntimeException("Unknown parameter " + param);

  public boolean supportsParams() {
    return true;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene NewCollationAnalyzerTask.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.