1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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 
17 #ifndef SCOPED_ICU_LOCALE_H_included
18 #define SCOPED_ICU_LOCALE_H_included
19 
20 #include "JNIHelp.h"
21 #include "ScopedUtfChars.h"
22 #include "unicode/locid.h"
23 
24 class ScopedIcuLocale {
25  public:
ScopedIcuLocale(JNIEnv * env,jstring javaLocaleName)26   ScopedIcuLocale(JNIEnv* env, jstring javaLocaleName) : mEnv(env) {
27     mLocale.setToBogus();
28 
29     if (javaLocaleName == NULL) {
30       jniThrowNullPointerException(mEnv, "javaLocaleName == null");
31       return;
32     }
33 
34     const ScopedUtfChars localeName(env, javaLocaleName);
35     if (localeName.c_str() == NULL) {
36       return;
37     }
38 
39     mLocale = icu::Locale::createFromName(localeName.c_str());
40   }
41 
~ScopedIcuLocale()42   ~ScopedIcuLocale() {
43   }
44 
valid()45   bool valid() const {
46     return !mLocale.isBogus();
47   }
48 
locale()49   icu::Locale& locale() {
50     return mLocale;
51   }
52 
53  private:
54   JNIEnv* const mEnv;
55   icu::Locale mLocale;
56 
57   // Disallow copy and assignment.
58   ScopedIcuLocale(const ScopedIcuLocale&);
59   void operator=(const ScopedIcuLocale&);
60 };
61 
62 #endif  // SCOPED_ICU_LOCALE_H_included
63