KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > locale > ThreadLocale


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package cintoo.messages.locale;
17
18 import java.util.Locale JavaDoc;
19
20 /**
21  * ThreadLocale stores a locale. The locale can
22  * be either thread local or global.
23  *
24  * @author Stephan J. Schmidt
25  * @version $Id$
26  * @since 1.0
27  */

28 public class ThreadLocale {
29     // true if thread local storage should be used
30
private boolean thread = true;
31
32     private Locale JavaDoc locale;
33
34     // store a locale for each thread with
35
// a thread local object. Thread locals store
36
// a different value for each thread.
37
private ThreadLocal JavaDoc<Locale JavaDoc> threadLocal = new ThreadLocal JavaDoc<Locale JavaDoc>() {
38         protected Locale JavaDoc initialValue() {
39             return Locale.getDefault();
40         }
41     };
42
43     /**
44      * Set if the locale should be global or thread local
45      *
46      * @param thread true if the locale is thread local
47      */

48     public void setThread(boolean thread) {
49         this.thread = thread;
50     }
51
52     /**
53      * Return the current locale, either the global
54      * locale or the thread locale
55      *
56      * @return current locale
57      */

58     public Locale JavaDoc get() {
59         if (thread) {
60             return threadLocal.get();
61         } else {
62             return locale;
63         }
64     }
65
66     /**
67      * Set the locale which should be used, either
68      * the globale locale or the thread locale
69      *
70      * @param locale locale to use
71      */

72     public void set(Locale JavaDoc locale) {
73         if (thread) {
74             threadLocal.set(locale);
75         } else {
76             this.locale = locale;
77         }
78     }
79 }
80
Popular Tags