• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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 package com.android.ide.eclipse.adt.internal.editors.layout.configuration;
18 
19 import static com.android.ide.common.resources.configuration.LanguageQualifier.FAKE_LANG_VALUE;
20 import static com.android.ide.common.resources.configuration.RegionQualifier.FAKE_REGION_VALUE;
21 
22 import com.android.annotations.NonNull;
23 import com.android.ide.common.resources.configuration.LanguageQualifier;
24 import com.android.ide.common.resources.configuration.RegionQualifier;
25 import com.google.common.base.Objects;
26 
27 import org.eclipse.swt.graphics.Image;
28 
29 /** A language,region pair */
30 public class Locale {
31     /** A special marker region qualifier representing any region */
32     public static final RegionQualifier ANY_REGION = new RegionQualifier(FAKE_REGION_VALUE);
33 
34     /** A special marker language qualifier representing any language */
35     public static final LanguageQualifier ANY_LANGUAGE = new LanguageQualifier(FAKE_LANG_VALUE);
36 
37     /** A locale which matches any language and region */
38     public static final Locale ANY = new Locale(ANY_LANGUAGE, ANY_REGION);
39 
40     /** The language qualifier, or {@link #ANY_LANGUAGE} if this locale matches any language  */
41     @NonNull
42     public final LanguageQualifier language;
43 
44     /** The language qualifier, or {@link #ANY_REGION} if this locale matches any region  */
45     @NonNull
46     public final RegionQualifier region;
47 
48     /**
49      * Constructs a new {@linkplain Locale} matching a given language in a given locale.
50      *
51      * @param language the language
52      * @param region the region
53      */
Locale(@onNull LanguageQualifier language, @NonNull RegionQualifier region)54     private Locale(@NonNull LanguageQualifier language, @NonNull RegionQualifier region) {
55         if (language.getValue().equals(FAKE_LANG_VALUE)) {
56             language = ANY_LANGUAGE;
57         }
58         if (region.getValue().equals(FAKE_REGION_VALUE)) {
59             region = ANY_REGION;
60         }
61         this.language = language;
62         this.region = region;
63     }
64 
65     /**
66      * Constructs a new {@linkplain Locale} matching a given language in a given specific locale.
67      *
68      * @param language the language
69      * @param region the region
70      * @return a locale with the given language and region
71      */
72     @NonNull
create( @onNull LanguageQualifier language, @NonNull RegionQualifier region)73     public static Locale create(
74             @NonNull LanguageQualifier language,
75             @NonNull RegionQualifier region) {
76         return new Locale(language, region);
77     }
78 
79     /**
80      * Constructs a new {@linkplain Locale} for the given language, matching any regions.
81      *
82      * @param language the language
83      * @return a locale with the given language and region
84      */
create(@onNull LanguageQualifier language)85     public static Locale create(@NonNull LanguageQualifier language) {
86         return new Locale(language, ANY_REGION);
87     }
88 
89     /**
90      * Returns a flag image to use for this locale
91      *
92      * @return a flag image, or a default globe icon
93      */
94     @NonNull
getFlagImage()95     public Image getFlagImage() {
96         Image image = null;
97         String languageCode = hasLanguage() ? language.getValue() : null;
98         String regionCode = hasRegion() ? region.getValue() : null;
99         LocaleManager icons = LocaleManager.get();
100         if (languageCode == null && regionCode == null) {
101             return LocaleManager.getGlobeIcon();
102         } else {
103             image = icons.getFlag(languageCode, regionCode);
104             if (image == null) {
105                 image = LocaleManager.getEmptyIcon();
106             }
107 
108             return image;
109         }
110     }
111 
112     /**
113      * Returns true if this locale specifies a specific language. This is true
114      * for all locales except {@link #ANY}.
115      *
116      * @return true if this locale specifies a specific language
117      */
hasLanguage()118     public boolean hasLanguage() {
119         return language != ANY_LANGUAGE;
120     }
121 
122     /**
123      * Returns true if this locale specifies a specific region
124      *
125      * @return true if this locale specifies a region
126      */
hasRegion()127     public boolean hasRegion() {
128         return region != ANY_REGION;
129     }
130 
131     @Override
hashCode()132     public int hashCode() {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + ((language == null) ? 0 : language.hashCode());
136         result = prime * result + ((region == null) ? 0 : region.hashCode());
137         return result;
138     }
139 
140     @Override
equals(Object obj)141     public boolean equals(Object obj) {
142         if (this == obj)
143             return true;
144         if (obj == null)
145             return false;
146         if (getClass() != obj.getClass())
147             return false;
148         Locale other = (Locale) obj;
149         if (language == null) {
150             if (other.language != null)
151                 return false;
152         } else if (!language.equals(other.language))
153             return false;
154         if (region == null) {
155             if (other.region != null)
156                 return false;
157         } else if (!region.equals(other.region))
158             return false;
159         return true;
160     }
161 
162     @Override
toString()163     public String toString() {
164         return Objects.toStringHelper(this).omitNullValues()
165             .addValue(language.getValue())
166             .addValue(region.getValue())
167             .toString();
168     }
169 }
170