add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / NewCollationAnalyzerTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
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.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 import java.util.Locale;
23 import java.util.StringTokenizer;
24
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.benchmark.byTask.PerfRunData;
27
28 /**
29  * Task to support benchmarking collation.
30  * <p>
31  * <ul>
32  *  <li> <code>NewCollationAnalyzer</code> with the default jdk impl
33  *  <li> <code>NewCollationAnalyzer(impl:icu)</code> specify an impl (jdk,icu)
34  * </ul>
35  * </p>
36  */
37 public class NewCollationAnalyzerTask extends PerfTask {
38   public enum Implementation { 
39     JDK("org.apache.lucene.collation.CollationKeyAnalyzer", 
40         "java.text.Collator"),
41     ICU("org.apache.lucene.collation.ICUCollationKeyAnalyzer", 
42         "com.ibm.icu.text.Collator");
43     
44     String className;
45     String collatorClassName;
46     
47     Implementation(String className, String collatorClassName) {
48       this.className = className;
49       this.collatorClassName = collatorClassName;
50     }
51   }
52   
53   private Implementation impl = Implementation.JDK;
54
55   public NewCollationAnalyzerTask(PerfRunData runData) {
56     super(runData);
57   }
58
59   static Analyzer createAnalyzer(Locale locale, Implementation impl)
60       throws Exception {
61     final Class<?> collatorClazz = Class.forName(impl.collatorClassName);
62     Method collatorMethod = collatorClazz.getMethod("getInstance",
63         new Class[] {Locale.class});
64     Object collator = collatorMethod.invoke(null, locale);
65     
66     final Class<? extends Analyzer> clazz = Class.forName(impl.className)
67         .asSubclass(Analyzer.class);
68     Constructor<? extends Analyzer> ctor = clazz.getConstructor(collatorClazz);
69     return ctor.newInstance(collator);
70   }
71   
72   @Override
73   public int doLogic() throws Exception {
74     try {
75       Locale locale = getRunData().getLocale();
76       if (locale == null) throw new RuntimeException(
77           "Locale must be set with the NewLocale task!");
78       Analyzer analyzer = createAnalyzer(locale, impl);
79       getRunData().setAnalyzer(analyzer);
80       System.out.println("Changed Analyzer to: "
81           + analyzer.getClass().getName() + "(" + locale + ")");
82     } catch (Exception e) {
83       throw new RuntimeException("Error creating Analyzer: impl=" + impl, e);
84     }
85     return 1;
86   }
87   
88   @Override
89   public void setParams(String params) {
90     super.setParams(params);
91     
92     StringTokenizer st = new StringTokenizer(params, ",");
93     while (st.hasMoreTokens()) {
94       String param = st.nextToken();
95       StringTokenizer expr = new StringTokenizer(param, ":");
96       String key = expr.nextToken();
97       String value = expr.nextToken();
98       // for now we only support the "impl" parameter.
99       // TODO: add strength, decomposition, etc
100       if (key.equals("impl")) {
101         if (value.equalsIgnoreCase("icu"))
102           impl = Implementation.ICU;
103         else if (value.equalsIgnoreCase("jdk"))
104           impl = Implementation.JDK;
105         else
106           throw new RuntimeException("Unknown parameter " + param);
107       } else {
108         throw new RuntimeException("Unknown parameter " + param);
109       }
110     }
111   }
112
113   @Override
114   public boolean supportsParams() {
115     return true;
116   }
117 }