]> granicus.if.org Git - icu/commitdiff
ICU-12812 clean up equals()
authorMark Davis <mark@macchiato.com>
Thu, 23 Mar 2017 15:16:26 +0000 (15:16 +0000)
committerMark Davis <mark@macchiato.com>
Thu, 23 Mar 2017 15:16:26 +0000 (15:16 +0000)
X-SVN-Rev: 39918

icu4j/main/classes/core/src/com/ibm/icu/impl/locale/XCldrStub.java
icu4j/main/classes/core/src/com/ibm/icu/impl/locale/XLikelySubtags.java
icu4j/main/classes/core/src/com/ibm/icu/impl/locale/XLocaleDistance.java

index 6d22a2779dd7de1e5e7ab30f2eb32986d3865877..6320ddd14305640a12fae48be84dae0c664fb7d3 100644 (file)
@@ -111,21 +111,15 @@ public class XCldrStub {
         }
         @Override
         public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (!(obj instanceof Multimap)) {
-                return false;
-            }
-            Multimap<?,?> other = (Multimap<?,?>) obj;
-            return map.equals(other.map);
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && map.equals(((Multimap<?,?>) obj).map));
         }
 
         @Override
         public int hashCode() {
-            // Not designed, return an arbitrary constant -123 for now
-            assert false;
-            return -123;
+            return map.hashCode();
         }
     }
 
@@ -228,21 +222,21 @@ public class XCldrStub {
     }
 
 
-//    public static class Counter<T> implements Iterable<T>{
-//        private Map<T,Long> data;
-//        @Override
-//        public Iterator<T> iterator() {
-//            return data.keySet().iterator();
-//        }
-//        public long get(T s) {
-//            Long result = data.get(s);
-//            return result != null ? result : 0L;
-//        }
-//        public void add(T item, int count) {
-//            Long result = data.get(item);
-//            data.put(item, result == null ? count : result + count);
-//        }
-//    }
+    //    public static class Counter<T> implements Iterable<T>{
+    //        private Map<T,Long> data;
+    //        @Override
+    //        public Iterator<T> iterator() {
+    //            return data.keySet().iterator();
+    //        }
+    //        public long get(T s) {
+    //            Long result = data.get(s);
+    //            return result != null ? result : 0L;
+    //        }
+    //        public void add(T item, int count) {
+    //            Long result = data.get(item);
+    //            data.put(item, result == null ? count : result + count);
+    //        }
+    //    }
 
     public static <T> String join(T[] source, String separator) {
         StringBuilder result = new StringBuilder();
@@ -368,23 +362,23 @@ public class XCldrStub {
                     canonicalName = new File(relativeFileName).getCanonicalPath();
                 } catch (Exception e1) {
                     throw new ICUUncheckedIOException("Couldn't open file: " + file + "; relative to class: "
-                        + className, e);
+                            + className, e);
                 }
                 throw new ICUUncheckedIOException("Couldn't open file " + file + "; in path " + canonicalName + "; relative to class: "
-                    + className, e);
+                        + className, e);
             }
         }
         public static String getRelativeFileName(Class<?> class1, String filename) {
             URL resource = class1 == null ?
                     FileUtilities.class.getResource(filename) : class1.getResource(filename);
-            String resourceString = resource.toString();
-            if (resourceString.startsWith("file:")) {
-                return resourceString.substring(5);
-            } else if (resourceString.startsWith("jar:file:")) {
-                return resourceString.substring(9);
-            } else {
-                throw new ICUUncheckedIOException("File not found: " + resourceString);
-            }
+                    String resourceString = resource.toString();
+                    if (resourceString.startsWith("file:")) {
+                        return resourceString.substring(5);
+                    } else if (resourceString.startsWith("jar:file:")) {
+                        return resourceString.substring(9);
+                    } else {
+                        throw new ICUUncheckedIOException("File not found: " + resourceString);
+                    }
         }
     }
 
index 371b2bc24d5db27cfefc69abaea2b201d821bba9..22fd4d269e822a36d0faf565805f2d597a156b9c 100644 (file)
@@ -116,26 +116,26 @@ public class XLikelySubtags {
         // #1 is language
         // #2 is script
         // #3 is region
-//        static final String pat =
-//                "language_id = (unicode_language_subtag)"
-//                        + "(?:sep(unicode_script_subtag))?"
-//                        + "(?:sep(unicode_region_subtag))?;\n"
-//                        + "unicode_language_subtag = alpha{2,3}|alpha{5,8};\n"
-//                        + "unicode_script_subtag = alpha{4};\n"
-//                        + "unicode_region_subtag  = alpha{2}|digit{3};\n"
-//                        + "sep    = [-_];\n"
-//                        + "digit  = [0-9];\n"
-//                        + "alpha   = [A-Za-z];\n"
-//                        ;
-//        static {
-//            System.out.println(pat);
-//            System.out.println(new UnicodeRegex().compileBnf(pat));
-//        }
-//        static final Pattern LANGUAGE_PATTERN = Pattern.compile(
-//                "([a-zA-Z0-9]+)" // (?:[-_]([a-zA-Z0-9]+))?(?:[-_]([a-zA-Z0-9]+))?"
-//                //new UnicodeRegex().compileBnf(pat)
-//                );
-//
+        //        static final String pat =
+        //                "language_id = (unicode_language_subtag)"
+        //                        + "(?:sep(unicode_script_subtag))?"
+        //                        + "(?:sep(unicode_region_subtag))?;\n"
+        //                        + "unicode_language_subtag = alpha{2,3}|alpha{5,8};\n"
+        //                        + "unicode_script_subtag = alpha{4};\n"
+        //                        + "unicode_region_subtag  = alpha{2}|digit{3};\n"
+        //                        + "sep    = [-_];\n"
+        //                        + "digit  = [0-9];\n"
+        //                        + "alpha   = [A-Za-z];\n"
+        //                        ;
+        //        static {
+        //            System.out.println(pat);
+        //            System.out.println(new UnicodeRegex().compileBnf(pat));
+        //        }
+        //        static final Pattern LANGUAGE_PATTERN = Pattern.compile(
+        //                "([a-zA-Z0-9]+)" // (?:[-_]([a-zA-Z0-9]+))?(?:[-_]([a-zA-Z0-9]+))?"
+        //                //new UnicodeRegex().compileBnf(pat)
+        //                );
+        //
         // TODO: fix this to check for format. Not required, since this is only called internally, but safer for the future.
         static LSR from(String languageIdentifier) {
             String[] parts = languageIdentifier.split("[-_]");
@@ -197,16 +197,13 @@ public class XLikelySubtags {
         }
         @Override
         public boolean equals(Object obj) {
-            if (this == obj) {
-                return false;
-            }
-            if (!(obj instanceof LSR)) {
-                return false;
-            }
-            LSR other = (LSR) obj;
-            return language.equals(other.language)
+            LSR other;
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && language.equals((other = (LSR) obj).language)
                     && script.equals(other.script)
-                    && region.equals(other.region);
+                    && region.equals(other.region));
         }
         @Override
         public int hashCode() {
@@ -247,8 +244,8 @@ public class XLikelySubtags {
 
         Maker maker = Maker.TREEMAP;
         Map<String, Map<String, Map<String, LSR>>> result = maker.make();
-//        Splitter bar = Splitter.on('_');
-//        int last = -1;
+        //        Splitter bar = Splitter.on('_');
+        //        int last = -1;
         // set the base data
         Map<LSR,LSR> internCache = new HashMap<LSR,LSR>();
         for (Entry<String, String> sourceTarget : rawData.entrySet()) {
@@ -265,13 +262,13 @@ public class XLikelySubtags {
             set(result, language, script, region, languageTarget, scriptTarget, regionTarget, internCache);
             // now add aliases
             Collection<String> languageAliases = LSR.LANGUAGE_ALIASES.getAliases(language);
-//            if (languageAliases.isEmpty()) {
-//                languageAliases = Collections.singleton(language);
-//            }
+            //            if (languageAliases.isEmpty()) {
+            //                languageAliases = Collections.singleton(language);
+            //            }
             Collection<String> regionAliases = LSR.REGION_ALIASES.getAliases(region);
-//            if (regionAliases.isEmpty()) {
-//                regionAliases = Collections.singleton(region);
-//            }
+            //            if (regionAliases.isEmpty()) {
+            //                regionAliases = Collections.singleton(region);
+            //            }
             for (String languageAlias : languageAliases) {
                 for (String regionAlias : regionAliases) {
                     if (languageAlias.equals(language) && regionAlias.equals(region)) {
@@ -320,34 +317,34 @@ public class XLikelySubtags {
         return result;
     }
 
-//    private void getAliasInfo(Map<String, R2<List<String>, String>> aliasInfo, Multimap<String, String> canonicalToAlias) {
-//        for (Entry<String, R2<List<String>, String>> e : aliasInfo.entrySet()) {
-//            final String alias = e.getKey();
-//            if (alias.contains("_")) {
-//                continue; // only do simple aliasing
-//            }
-//            String canonical = getCanonical(e.getValue());
-//            canonicalToAlias.put(canonical, alias);
-//        }
-//    }
-
-//    private static String getCanonical(R2<List<String>, String> aliasAndReason) {
-//        if (aliasAndReason == null) {
-//            return null;
-//        }
-//        if (aliasAndReason.get1().equals("overlong")) {
-//            return null;
-//        }
-//        List<String> value = aliasAndReason.get0();
-//        if (value.size() != 1) {
-//            return null;
-//        }
-//        final String canonical = value.iterator().next();
-//        if (canonical.contains("_")) {
-//            return null; // only do simple aliasing
-//        }
-//        return canonical;
-//    }
+    //    private void getAliasInfo(Map<String, R2<List<String>, String>> aliasInfo, Multimap<String, String> canonicalToAlias) {
+    //        for (Entry<String, R2<List<String>, String>> e : aliasInfo.entrySet()) {
+    //            final String alias = e.getKey();
+    //            if (alias.contains("_")) {
+    //                continue; // only do simple aliasing
+    //            }
+    //            String canonical = getCanonical(e.getValue());
+    //            canonicalToAlias.put(canonical, alias);
+    //        }
+    //    }
+
+    //    private static String getCanonical(R2<List<String>, String> aliasAndReason) {
+    //        if (aliasAndReason == null) {
+    //            return null;
+    //        }
+    //        if (aliasAndReason.get1().equals("overlong")) {
+    //            return null;
+    //        }
+    //        List<String> value = aliasAndReason.get0();
+    //        if (value.size() != 1) {
+    //            return null;
+    //        }
+    //        final String canonical = value.iterator().next();
+    //        if (canonical.contains("_")) {
+    //            return null; // only do simple aliasing
+    //        }
+    //        return canonical;
+    //    }
 
     private void set(Map<String, Map<String, Map<String, LSR>>> langTable, final String language, final String script, final String region,
             final String languageTarget, final String scriptTarget, final String regionTarget, Map<LSR, LSR> internCache) {
@@ -363,10 +360,10 @@ public class XLikelySubtags {
     private void set(Map<String, Map<String, Map<String, LSR>>> langTable, final String language, final String script, final String region, LSR newValue) {
         Map<String, Map<String, LSR>> scriptTable = Maker.TREEMAP.getSubtable(langTable, language);
         Map<String, LSR> regionTable = Maker.TREEMAP.getSubtable(scriptTable, script);
-//        LSR oldValue = regionTable.get(region);
-//        if (oldValue != null) {
-//            int debug = 0;
-//        }
+        //        LSR oldValue = regionTable.get(region);
+        //        if (oldValue != null) {
+        //            int debug = 0;
+        //        }
         regionTable.put(region, newValue);
     }
 
index 9f98b3765ccc4052caad075c0f9d39c720332716..b24d70a2b3398477f9e494a11f9b3e0c3b4cd2f8 100644 (file)
@@ -83,7 +83,7 @@ public class XLocaleDistance {
         ICUResourceBundle languageMatchingInfo = suppData.findTopLevel("languageMatchingInfo");
         ICUResourceBundle writtenParadigmLocales = (ICUResourceBundle) languageMatchingInfo.get("written")
                 .get("paradigmLocales");
-//      paradigmLocales{ "en", "en-GB",... }
+        //      paradigmLocales{ "en", "en-GB",... }
         HashSet<String> paradigmLocales = new HashSet<String>(Arrays.asList(writtenParadigmLocales.getStringArray()));
         return Collections.unmodifiableSet(paradigmLocales);
     }
@@ -94,7 +94,7 @@ public class XLocaleDistance {
         ICUResourceBundle languageMatchingInfo = suppData.findTopLevel("languageMatchingInfo");
         ICUResourceBundle writtenMatchVariables = (ICUResourceBundle) languageMatchingInfo.get("written")
                 .get("matchVariable");
-//        matchVariable{ americas{"019"} cnsar{"HK+MO"} ...}
+        //        matchVariable{ americas{"019"} cnsar{"HK+MO"} ...}
 
         HashMap<String,String> matchVariables = new HashMap<String,String>();
         for (Enumeration<String> enumer = writtenMatchVariables.getKeys(); enumer.hasMoreElements(); ) {
@@ -140,17 +140,17 @@ public class XLocaleDistance {
         .putAll("QO", "AQ", "BV", "CC", "CX", "GS", "HM", "IO", "TF", "UM", "AC", "CP", "DG", "TA")
         ;
         //Can't use following, because data from CLDR is discarded
-//        ICUResourceBundle suppData = LocaleMatcher.getICUSupplementalData();
-//        UResourceBundle territoryContainment = suppData.get("territoryContainment");
-//        for (int i = 0 ; i < territoryContainment.getSize(); i++) {
-//            UResourceBundle mapping = territoryContainment.get(i);
-//            String parent = mapping.getKey();
-//            for (int j = 0 ; j < mapping.getSize(); j++) {
-//                String child = mapping.getString(j);
-//                containment.put(parent,child);
-//                System.out.println(parent + " => " + child);
-//            }
-//        }
+        //        ICUResourceBundle suppData = LocaleMatcher.getICUSupplementalData();
+        //        UResourceBundle territoryContainment = suppData.get("territoryContainment");
+        //        for (int i = 0 ; i < territoryContainment.getSize(); i++) {
+        //            UResourceBundle mapping = territoryContainment.get(i);
+        //            String parent = mapping.getKey();
+        //            for (int j = 0 ; j < mapping.getSize(); j++) {
+        //                String child = mapping.getString(j);
+        //                containment.put(parent,child);
+        //                System.out.println(parent + " => " + child);
+        //            }
+        //        }
         TreeMultimap<String,String> containmentResolved = TreeMultimap.create();
         fill("001", containment, containmentResolved);
         return ImmutableMultimap.copyOf(containmentResolved);
@@ -173,8 +173,8 @@ public class XLocaleDistance {
     static final Multimap<String,String> CONTAINER_TO_CONTAINED;
     static final Multimap<String,String> CONTAINER_TO_CONTAINED_FINAL;
     static {
-//         Multimap<String, String> containerToContainedTemp = xGetContainment();
-//         fill(Region.getInstance("001"), containerToContainedTemp);
+        //         Multimap<String, String> containerToContainedTemp = xGetContainment();
+        //         fill(Region.getInstance("001"), containerToContainedTemp);
 
         CONTAINER_TO_CONTAINED = xGetContainment();
         Multimap<String, String> containerToFinalContainedBuilder = TreeMultimap.create();
@@ -235,12 +235,10 @@ public class XLocaleDistance {
 
         @Override
         public boolean equals(Object obj) {
-            if (!(obj instanceof DistanceNode)) {
-                return false;
-            }
-            DistanceNode other = (DistanceNode) obj;
-            return distance == other.distance
-                    && this.getClass().equals(other.getClass());
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && distance == ((DistanceNode) obj).distance);
         }
         @Override
         public int hashCode() {
@@ -334,14 +332,10 @@ public class XLocaleDistance {
         }
         @Override
         public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (!(obj instanceof IdMakerFull)) {
-                return false;
-            }
-            IdMakerFull<?> other = (IdMakerFull<?>) obj;
-            return intToObject.equals(other.intToObject);
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && intToObject.equals(((IdMakerFull<?>) obj).intToObject));
         }
         @Override
         public int hashCode() {
@@ -359,16 +353,13 @@ public class XLocaleDistance {
 
         @Override
         public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (!(obj instanceof StringDistanceNode)) {
-                return false;
-            }
-            StringDistanceNode other = (StringDistanceNode) obj;
-            return distance == other.distance
+            StringDistanceNode other;
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && distance == (other = (StringDistanceNode) obj).distance
                     && Utility.equals(distanceTable, other.distanceTable)
-                    && super.equals(other);
+                    && super.equals(other));
         }
         @Override
         public int hashCode() {
@@ -438,11 +429,10 @@ public class XLocaleDistance {
 
         @Override
         public boolean equals(Object obj) {
-            if (!(obj instanceof StringDistanceTable)) {
-                return false;
-            }
-            StringDistanceTable other = (StringDistanceTable) obj;
-            return subtables.equals(other.subtables);
+            return this == obj ||
+                    (obj != null
+                    && obj.getClass() == this.getClass()
+                    && subtables.equals(((StringDistanceTable) obj).subtables));
         }
         @Override
         public int hashCode() {
@@ -927,9 +917,9 @@ public class XLocaleDistance {
             //                        + "\" distance=\""
             //                        + rule[2]
             //                            + "\"/>");
-//            if (rule[0].equals("en_*_*") || rule[1].equals("*_*_*")) {
-//                int debug = 0;
-//            }
+            //            if (rule[0].equals("en_*_*") || rule[1].equals("*_*_*")) {
+            //                int debug = 0;
+            //            }
             List<String> desiredBase = new ArrayList<String>(bar.splitToList(rule[0]));
             List<String> supportedBase = new ArrayList<String>(bar.splitToList(rule[1]));
             Integer distance = 100-Integer.parseInt(rule[2]);