1 /* 2 * Copyright (c) 2021 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 public class LocaleMaskItem implements Comparable<LocaleMaskItem> { 30 private static final Logger logger = Logger.getLogger("LocaleMaskItem"); 31 private String languageTag; 32 private String maskString; 33 private long mask; 34 private int rank = -1; // Mask index 35 private int offset = -1; 36 private ArrayList<LocaleConfig> configs; 37 LocaleMaskItem(String languageTag, ArrayList<LocaleConfig> configs)38 public LocaleMaskItem(String languageTag, ArrayList<LocaleConfig> configs) { 39 this.languageTag = languageTag; 40 this.configs = configs; 41 configs.sort(new Comparator<LocaleConfig>() { 42 @Override 43 public int compare(LocaleConfig first, LocaleConfig second) { 44 if (first.nameId == second.nameId) { 45 return 0; 46 } else if (first.nameId < second.nameId) { 47 return -1; 48 } else { 49 return 1; 50 } 51 } 52 }); 53 try { 54 ULocale locale = ULocale.forLanguageTag(languageTag); 55 long[] temp = new long[1]; 56 this.maskString = Utils.getMask(locale, temp); 57 this.mask = temp[0]; 58 } catch (UnsupportedEncodingException e) { 59 logger.log(Level.SEVERE, "Get mask failed"); 60 } 61 } 62 63 getRank()64 public int getRank() { 65 return rank; 66 } 67 getLanguageTag()68 public String getLanguageTag() { 69 return languageTag; 70 } 71 getMask()72 public long getMask() { 73 return mask; 74 } 75 getMaskString()76 public String getMaskString() { 77 return maskString; 78 } 79 setRank(int rank)80 public void setRank(int rank) { 81 this.rank = rank; 82 } 83 setOffset(int offset)84 public void setOffset(int offset) { 85 this.offset = offset; 86 } 87 getOffset()88 public int getOffset() { 89 return offset; 90 } 91 getConfigs()92 public ArrayList<LocaleConfig> getConfigs() { 93 return configs; 94 } 95 96 @Override compareTo(LocaleMaskItem another)97 public int compareTo(LocaleMaskItem another) { 98 if (mask == another.getMask()) { 99 return 0; 100 } else if (mask < another.getMask()) { 101 return -1; 102 } else { 103 return 1; 104 } 105 } 106 } 107