1 package org.unicode.cldr.util;
2 
3 import java.util.Objects;
4 import java.util.concurrent.Callable;
5 import java.util.concurrent.ExecutionException;
6 
7 import org.unicode.cldr.test.CoverageLevel2;
8 
9 import com.google.common.cache.Cache;
10 import com.google.common.cache.CacheBuilder;
11 
12 public class CoverageInfo {
13     private final static int MAXLOCALES = 50;
14 
15     private final static class XPathWithLocation {
16         private final String xpath;
17         private final String location;
18         private final int hashCode;
19 
XPathWithLocation(String xpath, String location)20         public XPathWithLocation(String xpath, String location) {
21             this.xpath = xpath;
22             this.location = location;
23             this.hashCode = Objects.hash(
24                 this.xpath,
25                 this.location);
26         }
27 
28         @Override
hashCode()29         public int hashCode() {
30             return hashCode;
31         }
32 
33         @Override
equals(Object other)34         public boolean equals(Object other) {
35             if (other == null) {
36                 return false;
37             }
38             if (this == other) {
39                 return true;
40             }
41             if (hashCode != other.hashCode()) {
42                 return false;
43             }
44             if (!getClass().equals(other.getClass())) {
45                 return false;
46             }
47             XPathWithLocation o = (XPathWithLocation) other;
48             if (location != null && !location.equals(o.location)) {
49                 return false;
50             }
51             if (xpath != null && !xpath.equals(o.xpath)) {
52                 return false;
53             }
54             return true;
55         }
56 
getXPath()57         public String getXPath() {
58             return xpath;
59         }
60 
getLocation()61         public String getLocation() {
62             return location;
63         }
64 
65     }
66 
67     private Cache<String, CoverageLevel2> localeToCoverageLevelInfo = CacheBuilder.newBuilder().maximumSize(MAXLOCALES).build();
68     private Cache<XPathWithLocation, Level> coverageCache = CacheBuilder.newBuilder().maximumSize(MAXLOCALES).build();
69 
70     private final SupplementalDataInfo supplementalDataInfo;
71 
CoverageInfo(SupplementalDataInfo coverageInfoGettable)72     public CoverageInfo(SupplementalDataInfo coverageInfoGettable) {
73         this.supplementalDataInfo = coverageInfoGettable;
74     }
75 
76     /**
77      * Used to get the coverage value for a path. This is generally the most
78      * efficient way for tools to get coverage.
79      *
80      * @param xpath
81      * @param loc
82      * @return
83      */
getCoverageLevel(String xpath, String loc)84     public Level getCoverageLevel(String xpath, String loc) {
85         Level result = null;
86         final XPathWithLocation xpLoc = new XPathWithLocation(xpath, loc);
87         try {
88             result = coverageCache.get(xpLoc, new Callable<Level>() {
89 
90                 @Override
91                 public Level call() throws Exception {
92                     final String location = xpLoc.getLocation();
93                     CoverageLevel2 cov = localeToCoverageLevelInfo.get(location, new Callable<CoverageLevel2>() {
94 
95                         @Override
96                         public CoverageLevel2 call() throws Exception {
97                             return CoverageLevel2.getInstance(supplementalDataInfo, location);
98                         }
99                     });
100                     Level result = cov.getLevel(xpLoc.getXPath());
101                     return result;
102                 }
103             });
104         } catch (ExecutionException e) {
105             e.printStackTrace();
106         }
107         return result;
108     }
109 
110     /**
111      * Used to get the coverage value for a path. Note, it is more efficient to create
112      * a CoverageLevel2 for a language, and keep it around.
113      *
114      * @param xpath
115      * @param loc
116      * @return
117      */
getCoverageValue(String xpath, String loc)118     public int getCoverageValue(String xpath, String loc) {
119         return getCoverageLevel(xpath, loc).getLevel();
120     }
121 
122 }