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