1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.global.i18n;
17 
18 import java.io.UnsupportedEncodingException;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.logging.Logger;
22 import java.util.logging.Level;
23 
24 import com.ibm.icu.util.ULocale;
25 
26 /**
27  * Represent the mask value of a locale
28  *
29  * @since 2022-8-22
30  */
31 public class LocaleMaskItem implements Comparable<LocaleMaskItem> {
32     private static final Logger logger = Logger.getLogger("LocaleMaskItem");
33     private String languageTag;
34     private String maskString;
35     private long mask;
36     private int rank = -1; // Mask index
37     private int offset = -1;
38     private ArrayList<LocaleConfig> configs;
39 
40     /**
41      * Constructor of class LocaleMaskItem.
42      *
43      * @param languageTag language tag
44      * @param configs locale config list
45      */
LocaleMaskItem(String languageTag, ArrayList<LocaleConfig> configs)46     public LocaleMaskItem(String languageTag, ArrayList<LocaleConfig> configs) {
47         this.languageTag = languageTag;
48         this.configs = configs;
49         configs.sort(new Comparator<LocaleConfig>() {
50             @Override
51             public int compare(LocaleConfig first, LocaleConfig second) {
52                 if (first.nameId == second.nameId) {
53                     return 0;
54                 } else if (first.nameId < second.nameId) {
55                     return -1;
56                 } else {
57                     return 1;
58                 }
59             }
60         });
61         try {
62             ULocale locale = ULocale.forLanguageTag(languageTag);
63             long[] temp = new long[1];
64             this.maskString = Utils.getMask(locale, temp);
65             this.mask = temp[0];
66         } catch (UnsupportedEncodingException e) {
67             logger.log(Level.SEVERE, "Get mask failed");
68         }
69     }
70 
71 
getRank()72     public int getRank() {
73         return rank;
74     }
75 
getLanguageTag()76     public String getLanguageTag() {
77         return languageTag;
78     }
79 
getMask()80     public long getMask() {
81         return mask;
82     }
83 
getMaskString()84     public String getMaskString() {
85         return maskString;
86     }
87 
setRank(int rank)88     public void setRank(int rank) {
89         this.rank = rank;
90     }
91 
setOffset(int offset)92     public void setOffset(int offset) {
93         this.offset = offset;
94     }
95 
getOffset()96     public int getOffset() {
97         return offset;
98     }
99 
getConfigs()100     public ArrayList<LocaleConfig> getConfigs() {
101         return configs;
102     }
103 
104     @Override
compareTo(LocaleMaskItem another)105     public int compareTo(LocaleMaskItem another) {
106         if (mask == another.getMask()) {
107             return 0;
108         } else if (mask < another.getMask()) {
109             return -1;
110         } else {
111             return 1;
112         }
113     }
114 }
115