Index: geoapi/src/main/java/org/opengis/util/CodeList.java =================================================================== --- geoapi/src/main/java/org/opengis/util/CodeList.java (révision 1408) +++ geoapi/src/main/java/org/opengis/util/CodeList.java (copie de travail) @@ -77,7 +77,8 @@ * the {@code values} argument. This list is used for {@code values()} * method implementations. * - * @param name The code name. + * @param name The code name. This is usually the name of the public static final field + * declared in the interface. * @param values The collection to add the element to. */ @SuppressWarnings("unchecked") @@ -211,7 +212,9 @@ /** * Returns {@code true} if the given name matches the {@linkplain #name name}, * {@linkplain #identifier identifier} or any other identification string of - * this code list element. The comparison is case-insensitive. + * this code list element. The comparison is case-insensitive and ignores + * {@linkplain Character#isWhitespace(char) white spaces} (which are normally + * not present in identifiers). * * @param name The name to check. * @return {@code true} if the given name matches the code name or identifier. @@ -222,14 +225,68 @@ if (name == null) { return false; } - if (name.equalsIgnoreCase(this.name)) { + if (matches(name, this.name)) { return true; } final String identifier = identifier(); - return (identifier != null) && name.equalsIgnoreCase(identifier); + return (identifier != null) && matches(name, identifier); } /** + * Returns {@code true} if the given strings are equal, ignoring case, + * whitespaces and the '_' character. + */ + private static boolean matches(final String name, final String expected) { + final int el = expected.length(); + final int length = name.length(); + int ei=0; + for (int i=0; i= el) { + return false; // The name has more significant characters than expected. + } + ce = expected.charAt(ei++); + } while (!isSignificant(ce)); + + // Compare the characters in the same way than String.equalsIgnoreCase(String). + if (cn == ce) { + continue; + } + cn = Character.toUpperCase(cn); + ce = Character.toUpperCase(ce); + if (cn == ce) { + continue; + } + cn = Character.toLowerCase(cn); + ce = Character.toLowerCase(ce); + if (cn == ce) { + continue; + } + return false; + } + } + while (ei < el) { + if (isSignificant(expected.charAt(ei++))) { + return false; // The name has less significant characters than expected. + } + } + return true; + } + + /** + * Returns {@code true} if the given character should be taken in account when comparing two + * strings. Current implementation ignores only the whitespaces, so this method is merely a + * placeholder where more elaborated conditions could be added in the future. + */ + private static boolean isSignificant(final char c) { + return !Character.isWhitespace(c); + } + + /** * Returns the list of codes of the same kind than this code. * * @return The codes of the same kind than this code. Index: geoapi/src/test/java/org/opengis/util/CodeListTest.java =================================================================== --- geoapi/src/test/java/org/opengis/util/CodeListTest.java (révision 1408) +++ geoapi/src/test/java/org/opengis/util/CodeListTest.java (copie de travail) @@ -54,7 +54,7 @@ assertTrue (code.matches("UTF8")); assertTrue (code.matches("UTF_8")); assertTrue (code.matches("UTF-8")); - assertFalse (code.matches("UTF 8")); + assertTrue (code.matches("UTF 8")); assertSame (code, CharacterSet.valueOf("UTF_8")); assertSame (code, CharacterSet.valueOf("UTF-8")); assertSame (code, CharacterSet.valueOf("UTF8"));