This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git

commit d2b0c0219ccbfdd80bfa6b1ad7329b2ee8894c5b
Author: Matt Juntunen <[email protected]>
AuthorDate: Sat Jun 30 22:41:19 2018 -0400

    GEOMETRY-7: simplifying PolarCoordinate toString/parse representation
---
 .../euclidean/threed/SphericalCoordinates.java     |  1 -
 .../geometry/euclidean/twod/PolarCoordinates.java  | 88 +++++-----------------
 .../euclidean/threed/SphericalCoordinatesTest.java |  5 +-
 .../euclidean/twod/PolarCoordinatesTest.java       | 17 ++++-
 4 files changed, 34 insertions(+), 77 deletions(-)

diff --git 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
index 609e90b..6b38049 100644
--- 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
+++ 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
@@ -39,7 +39,6 @@ public class SphericalCoordinates implements Spatial, 
Serializable {
         public SphericalCoordinates create(double a1, double a2, double a3) {
             return new SphericalCoordinates(a1, a2, a3);
         }
-
     };
 
     /** Radius value. */
diff --git 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
index 681f646..da80e59 100644
--- 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
+++ 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
@@ -17,12 +17,11 @@
 package org.apache.commons.geometry.euclidean.twod;
 
 import java.io.Serializable;
-import java.text.ParsePosition;
 
 import org.apache.commons.geometry.core.Geometry;
 import org.apache.commons.geometry.core.Spatial;
-import org.apache.commons.geometry.core.util.AbstractCoordinateParser;
 import org.apache.commons.geometry.core.util.Coordinates;
+import org.apache.commons.geometry.core.util.SimpleCoordinateFormat;
 import org.apache.commons.numbers.angle.PlaneAngleRadians;
 
 /** Class representing a set of polar coordinates in 2 dimensional
@@ -35,8 +34,15 @@ public class PolarCoordinates implements Spatial, 
Serializable {
     /** Serializable version UID */
     private static final long serialVersionUID = 20180630L;
 
-    /** Shared parser/formatter instance **/
-    private static final PolarCoordinatesParser PARSER = new 
PolarCoordinatesParser();
+    /** Factory object for delegating instance creation. */
+    private static final Coordinates.Factory2D<PolarCoordinates> FACTORY = new 
Coordinates.Factory2D<PolarCoordinates>() {
+
+        /** {@inheritDoc} */
+        @Override
+        public PolarCoordinates create(double a1, double a2) {
+            return new PolarCoordinates(a1, a2);
+        }
+    };
 
     /** Radius value */
     private final double radius;
@@ -183,7 +189,7 @@ public class PolarCoordinates implements Spatial, 
Serializable {
     /** {@inheritDoc} */
     @Override
     public String toString() {
-        return PARSER.format(this);
+        return SimpleCoordinateFormat.getPointFormat().format(radius, azimuth);
     }
 
     /** Return a new polar coordinate instance with the given values.
@@ -191,7 +197,7 @@ public class PolarCoordinates implements Spatial, 
Serializable {
      * and azimuth in the range {@code (-pi, pi]}.
      * @param radius Radius value.
      * @param azimuth Azimuth angle in radians.
-     * @return
+     * @return new {@link PolarCoordinates} instance
      */
     public static PolarCoordinates of(double radius, double azimuth) {
         return new PolarCoordinates(radius, azimuth);
@@ -210,15 +216,14 @@ public class PolarCoordinates implements Spatial, 
Serializable {
     }
 
     /** Parse the given string and return a new polar coordinates instance. 
The parsed
-     * coordinates are normalized so that radius is within the range {@code 
[0, +infinity)}
-     * and azimuth is within the range {@code (-pi, pi]}. The expected string
+     * coordinates are normalized as in the {@link #of(double, double)} 
method. The expected string
      * format is the same as that returned by {@link #toString()}.
      * @param input the string to parse
      * @return new {@link PolarCoordinates} instance
      * @throws IllegalArgumentException if the string format is invalid.
      */
     public static PolarCoordinates parse(String input) {
-        return PARSER.parse(input);
+        return SimpleCoordinateFormat.getPointFormat().parse(input, FACTORY);
     }
 
     /** Convert the given set of polar coordinates to Cartesian coordinates.
@@ -238,65 +243,10 @@ public class PolarCoordinates implements Spatial, 
Serializable {
         return factory.create(x, y);
     }
 
-    /** Parser and formatter class for polar coordinates. */
-    private static class PolarCoordinatesParser extends 
AbstractCoordinateParser {
-
-        /** String prefix for the radius value. */
-        private static final String RADIUS_PREFIX = "r=";
-
-        /** String prefix for the azimuth value. */
-        private static final String AZIMUTH_PREFIX = "az=";
-
-        /** Simple constructor. */
-        private PolarCoordinatesParser() {
-            super(",", "(", ")");
-        }
-
-        /** Return a standardized string representation of the given set of 
polar
-         * coordinates.
-         * @param polar coordinates to format
-         * @return a standard string representation of the polar coordinates
-         */
-        public String format(PolarCoordinates polar) {
-            final StringBuilder sb = new StringBuilder();
-
-            sb.append(getPrefix());
-
-            sb.append(RADIUS_PREFIX);
-            sb.append(polar.getRadius());
-
-            sb.append(getSeparator());
-            sb.append(" ");
-
-            sb.append(AZIMUTH_PREFIX);
-            sb.append(polar.getAzimuth());
-
-            sb.append(getSuffix());
-
-            return sb.toString();
-        }
-
-        /** Parse the given string and return a set of standardized polar 
coordinates.
-         * @param str the string to parse
-         * @return polar coordinates
-         */
-        public PolarCoordinates parse(String str) {
-            final ParsePosition pos = new ParsePosition(0);
-
-            readPrefix(str, pos);
-
-            consumeWhitespace(str, pos);
-            readSequence(str, RADIUS_PREFIX, pos);
-            final double radius = readCoordinateValue(str, pos);
-
-            consumeWhitespace(str, pos);
-            readSequence(str, AZIMUTH_PREFIX, pos);
-            final double azimuth = readCoordinateValue(str, pos);
-
-            readSuffix(str, pos);
-            endParse(str, pos);
-
-            return new PolarCoordinates(radius, azimuth);
-        }
+    /** Return a factory object for creating new {@link PolarCoordinates} 
instances.
+     * @return factory object for creating new instances.
+     */
+    public static Coordinates.Factory2D<PolarCoordinates> getFactory() {
+        return FACTORY;
     }
 }
diff --git 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
index 8b1f51a..d1d9f01 100644
--- 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
+++ 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
@@ -4,7 +4,6 @@ import java.util.regex.Pattern;
 
 import org.apache.commons.geometry.core.Geometry;
 import org.apache.commons.geometry.core.util.Coordinates;
-import org.apache.commons.geometry.euclidean.twod.PolarCoordinates;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -328,8 +327,8 @@ public class SphericalCoordinatesTest {
     @Test
     public void testParse() {
         // act/assert
-        checkSpherical(SphericalCoordinates.parse("(1, 2, -3)"), 1, 2, 3);
-        checkSpherical(SphericalCoordinates.parse("(  2e0 , 5 , -0.000 )"), 2, 
5 - Geometry.TWO_PI, 0);
+        checkSpherical(SphericalCoordinates.parse("(1, 2, 3)"), 1, 2, 3);
+        checkSpherical(SphericalCoordinates.parse("(  -2.0 , 1 , -5e-1)"), 2, 
1 - Geometry.PI, Geometry.PI - 0.5);
         checkSpherical(SphericalCoordinates.parse("(NaN,Infinity,-Infinity)"), 
Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
     }
 
diff --git 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
index db5da3b..2b28c19 100644
--- 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
+++ 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
@@ -319,7 +319,7 @@ public class PolarCoordinatesTest {
     public void testToString() {
         // arrange
         PolarCoordinates polar = PolarCoordinates.of(1, 2);
-        Pattern pattern = Pattern.compile("\\(r=1.{0,2}, az=2.{0,2}\\)");
+        Pattern pattern = Pattern.compile("\\(1.{0,2}, 2.{0,2}\\)");
 
         // act
         String str = polar.toString();;
@@ -332,9 +332,9 @@ public class PolarCoordinatesTest {
     @Test
     public void testParse() {
         // act/assert
-        checkPolar(PolarCoordinates.parse("(r=1, az=2)"), 1, 2);
-        checkPolar(PolarCoordinates.parse("( r= -1, az= 0.5 )"), 1, 0.5 - 
Geometry.PI);
-        checkPolar(PolarCoordinates.parse("( r=NaN, az= -Infinity )"), 
Double.NaN, Double.NEGATIVE_INFINITY);
+        checkPolar(PolarCoordinates.parse("(1, 2)"), 1, 2);
+        checkPolar(PolarCoordinates.parse("( -1 , 0.5 )"), 1, 0.5 - 
Geometry.PI);
+        checkPolar(PolarCoordinates.parse("(NaN,-Infinity)"), Double.NaN, 
Double.NEGATIVE_INFINITY);
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -343,6 +343,15 @@ public class PolarCoordinatesTest {
         PolarCoordinates.parse("abc");
     }
 
+    @Test
+    public void testGetFactory() {
+        // act
+        Coordinates.Factory2D<PolarCoordinates> factory = 
PolarCoordinates.getFactory();
+
+        // assert
+        checkPolar(factory.create(-1, Geometry.HALF_PI), 1, 
Geometry.MINUS_HALF_PI);
+    }
+
     private void checkPolar(PolarCoordinates polar, double radius, double 
azimuth) {
         Assert.assertEquals(radius, polar.getRadius(), EPS);
         Assert.assertEquals(azimuth, polar.getAzimuth(), EPS);

Reply via email to