sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1544245 - in /sis/branches/JDK7/core: sis-referencing/src/main/java/org/apache/sis/internal/referencing/ sis-referencing/src/main/java/org/apache/sis/referencing/cs/ sis-referencing/src/test/java/org/apache/sis/internal/referencing/ sis-re...
Date Thu, 21 Nov 2013 16:26:43 GMT
Author: desruisseaux
Date: Thu Nov 21 16:26:43 2013
New Revision: 1544245

URL: http://svn.apache.org/r1544245
Log:
Consolidation: merged Directions with AxisDirections static class.
Simplified the Directions methods by invoking the appropriate CharSequences method where appropriate.

Added:
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/AxisDirectionsTest.java
      - copied, changed from r1544021, sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/DirectionsTest.java
Removed:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/Directions.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/DirectionsTest.java
Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java?rev=1544245&r1=1544244&r2=1544245&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
[UTF-8] Thu Nov 21 16:26:43 2013
@@ -20,9 +20,11 @@ import java.util.Map;
 import java.util.HashMap;
 import org.opengis.referencing.cs.AxisDirection;
 import org.opengis.referencing.cs.CoordinateSystem;
+import org.apache.sis.util.Characters;
 import org.apache.sis.util.Static;
 
 import static org.opengis.referencing.cs.AxisDirection.*;
+import static org.apache.sis.util.CharSequences.*;
 
 // Related to JDK7
 import java.util.Objects;
@@ -174,4 +176,106 @@ public final class AxisDirections extend
         }
         return fallback;
     }
+
+    /**
+     * Searches for a axis direction having the given name in the specified list of directions.
+     * This method compares the given name with the name of each {@code AxisDirection} in
a lenient way:
+     *
+     * <ul>
+     *   <li>Comparisons are case-insensitive.</li>
+     *   <li>Any character which is not a letter or a digit is ignored. For example
{@code "NorthEast"},
+     *       {@code "North-East"} and {@code "NORTH_EAST"} are considered equivalent.</li>
+     *   <li>This method accepts abbreviations as well, for example if the given {@code
name} is {@code "W"},
+     *       then it will be considered equivalent to {@code "WEST"}.</li>
+     * </ul>
+     *
+     * @param  name The name of the axis direction to search.
+     * @param  directions The list of axis directions in which to search.
+     * @return The first axis direction having a name matching the given one, or {@code null}
if none.
+     */
+    public static AxisDirection find(final String name, final AxisDirection[] directions)
{
+        for (final AxisDirection candidate : directions) {
+            final String identifier = candidate.name();
+            if (equalsFiltered(name, identifier, Characters.Filter.LETTERS_AND_DIGITS, true)
+                    || isAcronymForWords(name, identifier))
+            {
+                return candidate;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Searches pre-defined {@link AxisDirection} for a given name. This method searches
for a match in the set
+     * of known axis directions as returned by {@link AxisDirections#values()}, plus a few
special cases like
+     * "<cite>Geocentre &gt; equator/90°E</cite>". The later are used in
the EPSG database for geocentric CRS.
+     *
+     * <p>This method does not know about {@link DirectionAlongMeridian}. The later
is a parser which may create
+     * new directions, while this method searches only in a set of predefined directions
and never create new ones.</p>
+     *
+     * @param  name The name of the axis direction to search.
+     * @return The first axis direction having a name matching the given one, or {@code null}
if none.
+     */
+    public static AxisDirection valueOf(String name) {
+        name = trimWhitespaces(name.replace('_', ' '));
+        final AxisDirection[] directions = AxisDirection.values();
+        AxisDirection candidate = find(name, directions);
+        if (candidate == null) {
+            /*
+             * No match found when using the pre-defined axis name. Searches among
+             * the set of geocentric directions. Expected directions are:
+             *
+             *    Geocentre > equator/PM      or    Geocentre > equator/0°E
+             *    Geocentre > equator/90dE    or    Geocentre > equator/90°E
+             *    Geocentre > north pole
+             */
+            int d = name.indexOf('>');
+            if (d >= 0 && equalsIgnoreCase(name, 0, skipTrailingWhitespaces(name,
0, d), "Geocentre")) {
+                final int length = name.length();
+                d = skipLeadingWhitespaces(name, d+1, length);
+                int s = name.indexOf('/', d);
+                if (s < 0) {
+                    if (equalsIgnoreCase(name, d, length, "north pole")) {
+                        return GEOCENTRIC_Z; // "Geocentre > north pole"
+                    }
+                } else if (equalsIgnoreCase(name, d, skipTrailingWhitespaces(name, d, s),
"equator")) {
+                    s = skipLeadingWhitespaces(name, s+1, length);
+                    if (equalsIgnoreCase(name, s, length, "PM")) {
+                        return GEOCENTRIC_X; // "Geocentre > equator/PM"
+                    }
+                    /*
+                     * At this point, the name may be "Geocentre > equator/0°E",
+                     * "Geocentre > equator/90°E" or "Geocentre > equator/90dE".
+                     * Parse the number, limiting the scan to 6 characters for
+                     * avoiding a NumberFormatException.
+                     */
+                    final int stopAt = Math.min(s + 6, length);
+                    for (int i=s; i<stopAt; i++) {
+                        final char c = name.charAt(i);
+                        if (c < '0' || c > '9') {
+                            if (i == s) break;
+                            final int n = Integer.parseInt(name.substring(s, i));
+                            i = skipLeadingWhitespaces(name, i, length);
+                            if (equalsIgnoreCase(name, i, length, "°E") || equalsIgnoreCase(name,
i, length, "dE")) {
+                                switch (n) {
+                                    case  0: return GEOCENTRIC_X; // "Geocentre > equator/0°E"
+                                    case 90: return GEOCENTRIC_Y; // "Geocentre > equator/90°E"
+                                }
+                            }
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        return candidate;
+    }
+
+    /**
+     * Returns {@code true} if the given sub-sequence is equal to the given keyword, ignoring
case.
+     */
+    private static boolean equalsIgnoreCase(final String name, final int lower, final int
upper, final String keyword) {
+        final int length = upper - lower;
+        return (length == keyword.length()) && name.regionMatches(true, lower, keyword,
0, length);
+    }
 }

Copied: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/AxisDirectionsTest.java
(from r1544021, sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/DirectionsTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/AxisDirectionsTest.java?p2=sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/AxisDirectionsTest.java&p1=sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/DirectionsTest.java&r1=1544021&r2=1544245&rev=1544245&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/DirectionsTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/AxisDirectionsTest.java
[UTF-8] Thu Nov 21 16:26:43 2013
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sis.referencing.cs;
+package org.apache.sis.internal.referencing;
 
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestCase;
@@ -25,67 +25,67 @@ import static org.opengis.referencing.cs
 
 
 /**
- * Tests the {@link Directions} class.
+ * Tests the {@link AxisDirections} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4 (derived from geotk-3.00)
  * @version 0.4
  * @module
  */
-public final strictfp class DirectionsTest extends TestCase {
+public final strictfp class AxisDirectionsTest extends TestCase {
     /**
-     * Tests the standards directions.
+     * Tests {@link AxisDirections#valueOf(String)} for the North, South, East and West directions.
      */
     @Test
-    public void testCompass() {
-        assertSame(NORTH, Directions.find("NORTH"));
-        assertSame(SOUTH, Directions.find("South"));
-        assertSame(EAST,  Directions.find("east"));
-        assertSame(WEST,  Directions.find(" west "));
-        assertSame(WEST,  Directions.find("W"));
-        assertSame(NORTH, Directions.find(" N "));
-        assertSame(SOUTH, Directions.find("s"));
+    public void testValueOfCardinalDirection() {
+        assertSame(NORTH, AxisDirections.valueOf("NORTH"));
+        assertSame(SOUTH, AxisDirections.valueOf("South"));
+        assertSame(EAST,  AxisDirections.valueOf("east"));
+        assertSame(WEST,  AxisDirections.valueOf(" west "));
+        assertSame(WEST,  AxisDirections.valueOf("W"));
+        assertSame(NORTH, AxisDirections.valueOf(" N "));
+        assertSame(SOUTH, AxisDirections.valueOf("s"));
     }
 
     /**
-     * Tests mixin of the 4 compass directions.
+     * Tests {@link AxisDirections#valueOf(String)} for directions like North-East and South-South-West.
      */
     @Test
-    @DependsOnMethod("testCompass")
-    public void testMixin() {
-        assertSame(NORTH_EAST,       Directions.find("North-East"));
-        assertSame(SOUTH_SOUTH_WEST, Directions.find("South South West"));
-        assertSame(NORTH_EAST,       Directions.find("NE"));
-        assertSame(SOUTH_SOUTH_WEST, Directions.find("SSW"));
+    @DependsOnMethod("testValueOfCardinalDirection")
+    public void testValueOfInterCardinalDirection() {
+        assertSame(NORTH_EAST,       AxisDirections.valueOf("North-East"));
+        assertSame(SOUTH_SOUTH_WEST, AxisDirections.valueOf("South South West"));
+        assertSame(NORTH_EAST,       AxisDirections.valueOf("NE"));
+        assertSame(SOUTH_SOUTH_WEST, AxisDirections.valueOf("SSW"));
     }
 
     /**
-     * Tests the geocentric X direction.
+     * Tests {@link AxisDirections#valueOf(String)} for the geocentric X direction.
      */
     @Test
-    public void testGeocentricX() {
-        assertSame(GEOCENTRIC_X, Directions.find("Geocentre > equator/PM"));
-        assertSame(GEOCENTRIC_X, Directions.find("Geocentre>equator / PM"));
-        assertSame(GEOCENTRIC_X, Directions.find("Geocentre > equator/0°E"));
+    public void testValueOfGeocentricX() {
+        assertSame(GEOCENTRIC_X, AxisDirections.valueOf("Geocentre > equator/PM"));
+        assertSame(GEOCENTRIC_X, AxisDirections.valueOf("Geocentre>equator / PM"));
+        assertSame(GEOCENTRIC_X, AxisDirections.valueOf("Geocentre > equator/0°E"));
     }
 
     /**
-     * Tests the geocentric Y direction.
+     * Tests {@link AxisDirections#valueOf(String)} for the geocentric Y direction.
      */
     @Test
-    public void testGeocentricY() {
-        assertSame(GEOCENTRIC_Y, Directions.find("Geocentre > equator/90°E"));
-        assertSame(GEOCENTRIC_Y, Directions.find("Geocentre > equator/90dE"));
-        assertSame(GEOCENTRIC_Y, Directions.find("Geocentre>equator / 90dE"));
-        assertSame(GEOCENTRIC_Y, Directions.find("GEOCENTRE > EQUATOR/90dE"));
+    public void testValueOfGeocentricY() {
+        assertSame(GEOCENTRIC_Y, AxisDirections.valueOf("Geocentre > equator/90°E"));
+        assertSame(GEOCENTRIC_Y, AxisDirections.valueOf("Geocentre > equator/90dE"));
+        assertSame(GEOCENTRIC_Y, AxisDirections.valueOf("Geocentre>equator / 90dE"));
+        assertSame(GEOCENTRIC_Y, AxisDirections.valueOf("GEOCENTRE > EQUATOR/90dE"));
     }
 
     /**
-     * Tests the geocentric Z direction.
+     * Tests {@link AxisDirections#valueOf(String)} for the geocentric Z direction.
      */
     @Test
-    public void testGeocentricZ() {
-        assertSame(GEOCENTRIC_Z, Directions.find("Geocentre > north pole"));
-        assertSame(GEOCENTRIC_Z, Directions.find("Geocentre>north pole "));
+    public void testValueOfGeocentricZ() {
+        assertSame(GEOCENTRIC_Z, AxisDirections.valueOf("Geocentre > north pole"));
+        assertSame(GEOCENTRIC_Z, AxisDirections.valueOf("Geocentre>north pole "));
     }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java?rev=1544245&r1=1544244&r2=1544245&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] Thu Nov 21 16:26:43 2013
@@ -43,6 +43,7 @@ import org.junit.BeforeClass;
 
     org.apache.sis.internal.referencing.FormulasTest.class,
     org.apache.sis.internal.referencing.VerticalDatumTypesTest.class,
+    org.apache.sis.internal.referencing.AxisDirectionsTest.class,
     org.apache.sis.io.wkt.ConventionTest.class,
     org.apache.sis.io.wkt.SymbolsTest.class,
     org.apache.sis.io.wkt.FormatterTest.class,
@@ -55,7 +56,6 @@ import org.junit.BeforeClass;
     org.apache.sis.referencing.datum.DefaultPrimeMeridianTest.class,
     org.apache.sis.referencing.datum.DefaultVerticalDatumTest.class,
     org.apache.sis.referencing.datum.DefaultGeodeticDatumTest.class,
-    org.apache.sis.referencing.cs.DirectionsTest.class,
     org.apache.sis.referencing.StandardDefinitionsTest.class,
     org.apache.sis.referencing.GeodeticObjectsTest.class,
 

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java?rev=1544245&r1=1544244&r2=1544245&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] Thu Nov 21 16:26:43 2013
@@ -1561,8 +1561,8 @@ cmp:    while (ia < lga) {
     }
 
     /**
-     * Returns {@code true} if the given texts are equal, optionally ignoring case and filtered-out
-     * characters. This method is sometime used for comparing identifiers in a lenient way.
+     * Returns {@code true} if the given texts are equal, optionally ignoring case and filtered-out
characters.
+     * This method is sometime used for comparing identifiers in a lenient way.
      *
      * <p><b>Example:</b> the following call compares the two strings ignoring
case and any
      * characters which are not {@linkplain Character#isLetterOrDigit(int) letter or digit}.
@@ -1570,7 +1570,7 @@ cmp:    while (ia < lga) {
      * ignored:</p>
      *
      * {@preformat java
-     *     assert equals("WGS84", "WGS_84", Characters.Filter.LETTERS_AND_DIGITS, true) ==
true;
+     *     assert equalsFiltered("WGS84", "WGS_84", Characters.Filter.LETTERS_AND_DIGITS,
true) == true;
      * }
      *
      * @param  s1 The first string to compare, or {@code null}.



Mime
View raw message