add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / NewLocaleTask.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.util.Locale;
21 import java.util.StringTokenizer;
22
23 import org.apache.lucene.benchmark.byTask.PerfRunData;
24
25 /**
26  * Set a {@link java.util.Locale} for use in benchmarking.
27  * <p>
28  * Locales can be specified in the following ways:
29  * <ul>
30  *  <li><code>de</code>: Language "de"
31  *  <li><code>en,US</code>: Language "en", country "US"
32  *  <li><code>no,NO,NY</code>: Language "no", country "NO", variant "NY" 
33  *  <li><code>ROOT</code>: The root (language-agnostic) Locale
34  *  <li>&lt;empty string&gt;: Erase the Locale (null)
35  * </ul>
36  * </p>
37  */
38 public class NewLocaleTask extends PerfTask {
39   private String language;
40   private String country;
41   private String variant;
42   
43   /**
44    * Create a new {@link java.util.Locale} and set it it in the getRunData() for
45    * use by all future tasks.
46    */
47   public NewLocaleTask(PerfRunData runData) {
48     super(runData);
49   }
50
51   static Locale createLocale(String language, String country, String variant) {
52     if (language == null || language.length() == 0) 
53       return null;
54     
55     String lang = language;
56     if (lang.equalsIgnoreCase("ROOT"))
57       lang = ""; // empty language is the root locale in the JDK
58       
59     return new Locale(lang, country, variant);
60   }
61   
62   @Override
63   public int doLogic() throws Exception {
64     Locale locale = createLocale(language, country, variant);
65     getRunData().setLocale(locale);
66     System.out.println("Changed Locale to: " + 
67         (locale == null ? "null" : 
68         (locale.getDisplayName().length() == 0) ? "root locale" : locale));
69     return 1;
70   }
71   
72   @Override
73   public void setParams(String params) {
74     super.setParams(params);
75     language = country = variant = "";
76     StringTokenizer st = new StringTokenizer(params, ",");
77     if (st.hasMoreTokens())
78       language = st.nextToken();
79     if (st.hasMoreTokens())
80       country = st.nextToken();
81     if (st.hasMoreTokens())
82       variant = st.nextToken();
83   }
84
85   @Override
86   public boolean supportsParams() {
87     return true;
88   }
89 }