xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/util/GrammarDerivation.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.util;
2 
3 import com.google.common.base.MoreObjects;
4 import com.google.common.base.MoreObjects.ToStringHelper;
5 import com.ibm.icu.util.Freezable;
6 import java.util.Map;
7 import java.util.Objects;
8 import java.util.TreeMap;
9 import org.unicode.cldr.util.GrammarInfo.GrammaticalFeature;
10 
11 public class GrammarDerivation implements Freezable<GrammarDerivation> {
12 
13     public enum CompoundUnitStructure {
14         per,
15         times,
16         power,
17         prefix
18     }
19 
20     public class Values {
21         public final String value0;
22         public final String value1;
23 
Values(String... values)24         public Values(String... values) {
25             super();
26             this.value0 = values[0];
27             this.value1 = values.length == 2 ? values[0] : null;
28         }
29 
30         @Override
toString()31         public String toString() {
32             final ToStringHelper temp =
33                     MoreObjects.toStringHelper(getClass()).add("value0", value0);
34             if (value1 != null) {
35                 temp.add("value1", value1);
36             }
37             return temp.toString();
38         }
39     }
40 
41     private Map<GrammaticalFeature, Map<CompoundUnitStructure, Values>> data = new TreeMap<>();
42 
add(String featureStr, String structureStr, String... values)43     public void add(String featureStr, String structureStr, String... values) {
44         GrammaticalFeature feature = GrammaticalFeature.fromName(featureStr);
45         CompoundUnitStructure structure = CompoundUnitStructure.valueOf(structureStr);
46         Map<CompoundUnitStructure, Values> structureToValues = data.get(feature);
47         if (structureToValues == null) {
48             data.put(feature, structureToValues = new TreeMap<>());
49         }
50         structureToValues.put(structure, new Values(values));
51     }
52 
get(GrammaticalFeature feature, CompoundUnitStructure structure)53     public Values get(GrammaticalFeature feature, CompoundUnitStructure structure) {
54         Map<CompoundUnitStructure, Values> structureToValues = data.get(feature);
55         if (structureToValues == null) {
56             return null;
57         }
58         return structureToValues.get(structure);
59     }
60 
61     @Override
isFrozen()62     public boolean isFrozen() {
63         return data instanceof TreeMap;
64     }
65 
66     @Override
freeze()67     public GrammarDerivation freeze() {
68         if (!isFrozen()) {
69             data = CldrUtility.protectCollection(data);
70         }
71         return this;
72     }
73 
74     @Override
cloneAsThawed()75     public GrammarDerivation cloneAsThawed() {
76         throw new UnsupportedOperationException();
77     }
78 
79     @Override
hashCode()80     public int hashCode() {
81         return Objects.hash(data);
82     }
83 
84     @Override
toString()85     public String toString() {
86         return MoreObjects.toStringHelper(getClass()).add("data", data).toString();
87     }
88 }
89