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