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 
hashCode()28         public int hashCode() {
29             return hashCode;
30         }
31 
equals(Object other)32         public boolean equals(Object other) {
33             if (other == null) {
34                 return false;
35             }
36             if (this == other) {
37                 return true;
38             }
39             if (hashCode != other.hashCode()) {
40                 return false;
41             }
42             if (!getClass().equals(other.getClass())) {
43                 return false;
44             }
45             XPathWithLocation o = (XPathWithLocation) other;
46             if (location != null && !location.equals(o.location)) {
47                 return false;
48             }
49             if (xpath != null && !xpath.equals(o.xpath)) {
50                 return false;
51             }
52             return true;
53         }
54 
getXPath()55         public String getXPath() {
56             return xpath;
57         }
58 
getLocation()59         public String getLocation() {
60             return location;
61         }
62 
63     }
64 
65     private Cache<String, CoverageLevel2> localeToCoverageLevelInfo = CacheBuilder.newBuilder().maximumSize(MAXLOCALES).build();
66     private Cache<XPathWithLocation, Level> coverageCache = CacheBuilder.newBuilder().maximumSize(MAXLOCALES).build();
67 
68     private final SupplementalDataInfo supplementalDataInfo;
69 
CoverageInfo(SupplementalDataInfo coverageInfoGettable)70     public CoverageInfo(SupplementalDataInfo coverageInfoGettable) {
71         this.supplementalDataInfo = coverageInfoGettable;
72     }
73 
74     /**
75      * Used to get the coverage value for a path. This is generally the most
76      * efficient way for tools to get coverage.
77      *
78      * @param xpath
79      * @param loc
80      * @return
81      */
getCoverageLevel(String xpath, String loc)82     public Level getCoverageLevel(String xpath, String loc) {
83         Level result = null;
84         final XPathWithLocation xpLoc = new XPathWithLocation(xpath, loc);
85         try {
86             result = coverageCache.get(xpLoc, new Callable<Level>() {
87 
88                 @Override
89                 public Level call() throws Exception {
90                     final String location = xpLoc.getLocation();
91                     CoverageLevel2 cov = localeToCoverageLevelInfo.get(location, new Callable<CoverageLevel2>() {
92 
93                         @Override
94                         public CoverageLevel2 call() throws Exception {
95                             return CoverageLevel2.getInstance(supplementalDataInfo, location);
96                         }
97                     });
98                     Level result = cov.getLevel(xpLoc.getXPath());
99                     return result;
100                 }
101             });
102         } catch (ExecutionException e) {
103             e.printStackTrace();
104         }
105         return result;
106     }
107 
108     /**
109      * Used to get the coverage value for a path. Note, it is more efficient to create
110      * a CoverageLevel2 for a language, and keep it around.
111      *
112      * @param xpath
113      * @param loc
114      * @return
115      */
getCoverageValue(String xpath, String loc)116     public int getCoverageValue(String xpath, String loc) {
117         return getCoverageLevel(xpath, loc).getLevel();
118     }
119 
120 }