diff --git a/pom.xml b/pom.xml index c247584..480de3f 100755 --- a/pom.xml +++ b/pom.xml @@ -4,15 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - org.sonatype.oss - oss-parent - 7 - - com.goebl simplify - 1.0.1-SNAPSHOT + ${revision} jar simplify-java @@ -20,14 +14,17 @@ https://github.com/hgoebl/simplify-java + 1.0.1-SNAPSHOT UTF-8 + 17 + additional-deployment-repository junit junit - 4.11 + 4.13.2 test @@ -56,14 +53,12 @@ - sonatype-nexus-snapshots - Sonatype Nexus snapshot repository - https://oss.sonatype.org/content/repositories/snapshots + ias-snapshots + https://maven.mangoautomation.net/repository/ias-snapshot/ - sonatype-nexus-staging - Sonatype Nexus release repository - https://oss.sonatype.org/service/local/staging/deploy/maven2 + ias-releases + https://maven.mangoautomation.net/repository/ias-release/ @@ -77,16 +72,17 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 - - 1.5 - 1.5 - + 3.14.0 + + + org.apache.maven.plugins + maven-deploy-plugin + 3.1.4 org.apache.maven.plugins maven-source-plugin - 2.1.2 + 3.3.1 attach-sources @@ -99,8 +95,9 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.9.1 + 3.11.2 + all,-missing true true true @@ -115,6 +112,32 @@ + + org.codehaus.mojo + flatten-maven-plugin + 1.7.0 + + true + resolveCiFriendliesOnly + ${project.build.directory} + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + + @@ -131,7 +154,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.4 + 3.2.7 sign-artifacts @@ -145,6 +168,33 @@ + + additional-deployment-repository + + + additionalDeploymentRepositoryUrl + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + + additional-deployment-repository + + deploy + + + ${additionalDeploymentRepositoryId}::${additionalDeploymentRepositoryUrl} + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/goebl/simplify/AbstractSimplify.java b/src/main/java/com/goebl/simplify/AbstractSimplify.java index 78c53b6..56d39cb 100755 --- a/src/main/java/com/goebl/simplify/AbstractSimplify.java +++ b/src/main/java/com/goebl/simplify/AbstractSimplify.java @@ -12,27 +12,27 @@ */ abstract class AbstractSimplify { - private T[] sampleArray; - - protected AbstractSimplify(T[] sampleArray) { - this.sampleArray = sampleArray; + protected AbstractSimplify() { } /** * Simplifies a list of points to a shorter list of points. * @param points original list of points * @param tolerance tolerance in the same measurement as the point coordinates - * @param highestQuality true for using Douglas-Peucker only, - * false for using Radial-Distance algorithm before + * @param highestQuality true for using Douglas-Peucker only, + * false for using Radial-Distance algorithm before * applying Douglas-Peucker (should be a bit faster) * @return simplified list of points */ - public T[] simplify(T[] points, + public List simplify(List points, double tolerance, boolean highestQuality) { - if (points == null || points.length <= 2) { - return points; + if (points == null) { + throw new IllegalArgumentException(); + } + if (points.size() <= 2) { + return new ArrayList<>(points); } double sqTolerance = tolerance * tolerance; @@ -46,15 +46,15 @@ public T[] simplify(T[] points, return points; } - T[] simplifyRadialDistance(T[] points, double sqTolerance) { + List simplifyRadialDistance(List points, double sqTolerance) { T point = null; - T prevPoint = points[0]; + T prevPoint = points.get(0); - List newPoints = new ArrayList(); + List newPoints = new ArrayList<>(); newPoints.add(prevPoint); - for (int i = 1; i < points.length; ++i) { - point = points[i]; + for (int i = 1; i < points.size(); ++i) { + point = points.get(i); if (getSquareDistance(point, prevPoint) > sqTolerance) { newPoints.add(point); @@ -66,7 +66,7 @@ T[] simplifyRadialDistance(T[] points, double sqTolerance) { newPoints.add(point); } - return newPoints.toArray(sampleArray); + return newPoints; } private static class Range { @@ -79,14 +79,14 @@ private Range(int first, int last) { int last; } - T[] simplifyDouglasPeucker(T[] points, double sqTolerance) { + List simplifyDouglasPeucker(List points, double sqTolerance) { - BitSet bitSet = new BitSet(points.length); + BitSet bitSet = new BitSet(points.size()); bitSet.set(0); - bitSet.set(points.length - 1); + bitSet.set(points.size() - 1); - List stack = new ArrayList(); - stack.add(new Range(0, points.length - 1)); + List stack = new ArrayList<>(); + stack.add(new Range(0, points.size() - 1)); while (!stack.isEmpty()) { Range range = stack.remove(stack.size() - 1); @@ -96,7 +96,7 @@ T[] simplifyDouglasPeucker(T[] points, double sqTolerance) { // find index of point with maximum square distance from first and last point for (int i = range.first + 1; i < range.last; ++i) { - double sqDist = getSquareSegmentDistance(points[i], points[range.first], points[range.last]); + double sqDist = getSquareSegmentDistance(points.get(i), points.get(range.first), points.get(range.last)); if (sqDist > maxSqDist) { index = i; @@ -112,12 +112,12 @@ T[] simplifyDouglasPeucker(T[] points, double sqTolerance) { } } - List newPoints = new ArrayList(bitSet.cardinality()); + List newPoints = new ArrayList<>(bitSet.cardinality()); for (int index = bitSet.nextSetBit(0); index >= 0; index = bitSet.nextSetBit(index + 1)) { - newPoints.add(points[index]); + newPoints.add(points.get(index)); } - return newPoints.toArray(sampleArray); + return newPoints; } diff --git a/src/main/java/com/goebl/simplify/Simplify.java b/src/main/java/com/goebl/simplify/Simplify.java index bfff4ad..435820c 100755 --- a/src/main/java/com/goebl/simplify/Simplify.java +++ b/src/main/java/com/goebl/simplify/Simplify.java @@ -8,20 +8,19 @@ */ public class Simplify extends AbstractSimplify { - private final PointExtractor pointExtractor; + private final PointExtractor pointExtractor; /** * Simple constructor for 2D-Simplifier. *
* With this simple constructor your array elements must implement {@link Point}.
- * If you have coordinate classes which cannot be changed to implement Point, use - * {@link #Simplify(Object[], PointExtractor)} constructor! + * If you have coordinate classes which cannot be changed to implement Point, use + * {@link #Simplify(PointExtractor)} constructor! * - * @param sampleArray pass just an empty array (new MyPoint[0]) - necessary for type consistency. */ - public Simplify(T[] sampleArray) { - super(sampleArray); - this.pointExtractor = new PointExtractor() { + public Simplify() { + super(); + this.pointExtractor = new PointExtractor<>() { @Override public double getX(T point) { return ((Point) point).getX(); @@ -38,13 +37,12 @@ public double getY(T point) { * Alternative constructor for 2D-Simplifier. *
* With this constructor your array elements do not have to implement a special interface like {@link Point}.
- * Implement a {@link PointExtractor} to give Simplify access to your coordinates. + * Implement a {@link PointExtractor} to give Simplify access to your coordinates. * - * @param sampleArray pass just an empty array (new MyPoint[0]) - necessary for type consistency. * @param pointExtractor your implementation to extract X and Y coordinates from you array elements. */ - public Simplify(T[] sampleArray, PointExtractor pointExtractor) { - super(sampleArray); + public Simplify(PointExtractor pointExtractor) { + super(); this.pointExtractor = pointExtractor; } diff --git a/src/main/java/com/goebl/simplify/Simplify3D.java b/src/main/java/com/goebl/simplify/Simplify3D.java index a6e13dd..d2bcd45 100755 --- a/src/main/java/com/goebl/simplify/Simplify3D.java +++ b/src/main/java/com/goebl/simplify/Simplify3D.java @@ -8,20 +8,18 @@ */ public class Simplify3D extends AbstractSimplify { - private final Point3DExtractor pointExtractor; + private final Point3DExtractor pointExtractor; /** * Simple constructor for 3D-Simplifier. *
* With this simple constructor your array elements must implement {@link Point3D}.
- * If you have coordinate classes which cannot be changed to implement Point3D, use - * {@link #Simplify3D(Object[], Point3DExtractor)} constructor! - * - * @param sampleArray pass just an empty array (new MyPoint[0]) - necessary for type consistency. + * If you have coordinate classes which cannot be changed to implement Point3D, use + * {@link #Simplify3D(Point3DExtractor)} constructor! */ - public Simplify3D(T[] sampleArray) { - super(sampleArray); - this.pointExtractor = new Point3DExtractor() { + public Simplify3D() { + super(); + this.pointExtractor = new Point3DExtractor<>() { @Override public double getX(T point) { return ((Point) point).getX(); @@ -43,13 +41,12 @@ public double getZ(T point) { * Alternative constructor for 3D-Simplifier. *
* With this constructor your array elements do not have to implement a special interface like {@link Point3D}.
- * Implement a {@link Point3DExtractor} to give Simplify3D access to your coordinates. + * Implement a {@link Point3DExtractor} to give Simplify3D access to your coordinates. * - * @param sampleArray pass just an empty array (new MyPoint[0]) - necessary for type consistency. * @param pointExtractor your implementation to extract X, Y and Z coordinates from you array elements. */ - public Simplify3D(T[] sampleArray, Point3DExtractor pointExtractor) { - super(sampleArray); + public Simplify3D(Point3DExtractor pointExtractor) { + super(); this.pointExtractor = pointExtractor; } diff --git a/src/test/java/com/goebl/simplify/GpsLocationTest.java b/src/test/java/com/goebl/simplify/GpsLocationTest.java index c9e1e9e..eed7325 100644 --- a/src/test/java/com/goebl/simplify/GpsLocationTest.java +++ b/src/test/java/com/goebl/simplify/GpsLocationTest.java @@ -11,7 +11,7 @@ * Test class for {@link Simplify} in context of typical Gps-Tracks. *
* This is more of a demonstration than a test. - * See http://stackoverflow.com/q/34010298/2176962 + * See http://stackoverflow.com/q/34010298/2176962 * * @author goebl * @since 01.12.15 @@ -36,9 +36,9 @@ public double getLng() { } } - private static LatLng[] coords; + private static List coords; - private static PointExtractor latLngPointExtractor = new PointExtractor() { + private static final PointExtractor latLngPointExtractor = new PointExtractor<>() { @Override public double getX(LatLng point) { return point.getLat() * 1000000; @@ -53,25 +53,25 @@ public double getY(LatLng point) { @BeforeClass public static void readPoints() throws Exception { Point[] allPoints = SimplifyTest.readPoints("gps-track.txt"); - List coordsList = new ArrayList(allPoints.length); + List coordsList = new ArrayList<>(allPoints.length); for (Point point:allPoints) { coordsList.add(new LatLng(point.getX(), point.getY())); } - coords = coordsList.toArray(new LatLng[coordsList.size()]); + coords = coordsList; } @Test - public void testSimplifyGpsTrack() throws Exception { - Simplify simplify = new Simplify(new LatLng[0], latLngPointExtractor); + public void testSimplifyGpsTrack() { + Simplify simplify = new Simplify<>(latLngPointExtractor); - LatLng[] simplified = simplify.simplify(coords, 20f, false); - System.out.println("coords:" + coords.length + " simplified:" + simplified.length); - Assert.assertTrue("should be simplified to less than 33%", simplified.length < (coords.length / 3)); + List simplified = simplify.simplify(coords, 20f, false); + System.out.println("coords:" + coords.size() + " simplified:" + simplified.size()); + Assert.assertTrue("should be simplified to less than 33%", simplified.size() < (coords.size() / 3)); simplified = simplify.simplify(coords, 50f, true); - System.out.println("coords:" + coords.length + " simplified:" + simplified.length); - Assert.assertTrue("should be simplified to less than 20%", simplified.length < (coords.length * 0.2)); + System.out.println("coords:" + coords.size() + " simplified:" + simplified.size()); + Assert.assertTrue("should be simplified to less than 20%", simplified.size() < (coords.size() * 0.2)); } } diff --git a/src/test/java/com/goebl/simplify/Simplify3DTest.java b/src/test/java/com/goebl/simplify/Simplify3DTest.java index 2e5e961..a02977b 100755 --- a/src/test/java/com/goebl/simplify/Simplify3DTest.java +++ b/src/test/java/com/goebl/simplify/Simplify3DTest.java @@ -3,6 +3,10 @@ import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * Test class for {@link com.goebl.simplify.Simplify}. * @@ -17,7 +21,7 @@ public class Simplify3DTest { @Test public void testCustomPoint3DExtractor() { - Point3DExtractor pointExtractor = new Point3DExtractor() { + Point3DExtractor pointExtractor = new Point3DExtractor<>() { @Override public double getX(float[] point) { return point[0]; @@ -34,29 +38,29 @@ public double getZ(float[] point) { } }; - Simplify3D simplify = new Simplify3D(new float[0][0], pointExtractor); + Simplify3D simplify = new Simplify3D<>(pointExtractor); - float[][] simplified = simplify.simplify(POINTS_3D, 5.0f, false); - Assert.assertEquals("array should be simplified", 2, simplified.length); + List simplified = simplify.simplify(Arrays.stream(POINTS_3D).toList(), 5.0f, false); + Assert.assertEquals("array should be simplified", 2, simplified.size()); - simplified = simplify.simplify(POINTS_3D, 5.0f, true); - Assert.assertEquals("array should be simplified", 2, simplified.length); + simplified = simplify.simplify(Arrays.stream(POINTS_3D).toList(), 5.0f, true); + Assert.assertEquals("array should be simplified", 2, simplified.size()); } @Test public void testDefaultPointExtractor() { - Point3D[] points = new MyPoint[POINTS_3D.length]; - for (int i = 0; i < POINTS_3D.length; ++i) { - points[i] = new MyPoint(POINTS_3D[i][0], POINTS_3D[i][1], POINTS_3D[i][2]); + List points = new ArrayList<>(); + for (float[] floats : POINTS_3D) { + points.add(new MyPoint(floats[0], floats[1], floats[2])); } - Simplify3D simplify3D = new Simplify3D(new MyPoint[0]); + Simplify3D simplify3D = new Simplify3D<>(); - Point3D[] simplified = simplify3D.simplify(points, 5.0d, false); - Assert.assertEquals("array should be simplified", 2, simplified.length); + List simplified = simplify3D.simplify(points, 5.0d, false); + Assert.assertEquals("array should be simplified", 2, simplified.size()); simplified = simplify3D.simplify(points, 5.0d, true); - Assert.assertEquals("array should be simplified", 2, simplified.length); + Assert.assertEquals("array should be simplified", 2, simplified.size()); } private static class MyPoint implements Point3D { @@ -99,9 +103,7 @@ public boolean equals(Object o) { if (Double.compare(myPoint.x, x) != 0) return false; if (Double.compare(myPoint.y, y) != 0) return false; - if (Double.compare(myPoint.z, z) != 0) return false; - - return true; + return Double.compare(myPoint.z, z) == 0; } } diff --git a/src/test/java/com/goebl/simplify/SimplifyTest.java b/src/test/java/com/goebl/simplify/SimplifyTest.java index 1fd816e..771237c 100755 --- a/src/test/java/com/goebl/simplify/SimplifyTest.java +++ b/src/test/java/com/goebl/simplify/SimplifyTest.java @@ -9,7 +9,9 @@ import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -51,7 +53,7 @@ public void testHighQuality() throws Exception { @Test public void testCustomPointExtractor() { - PointExtractor pointExtractor = new PointExtractor() { + PointExtractor pointExtractor = new PointExtractor<>() { @Override public double getX(float[] point) { return point[0]; @@ -63,34 +65,35 @@ public double getY(float[] point) { } }; - Simplify simplify = new Simplify(new float[0][0], pointExtractor); + Simplify simplify = new Simplify<>(pointExtractor); - float[][] simplified = simplify.simplify(POINTS_2D, 5.0f, false); - Assert.assertEquals("array should be simplified", 2, simplified.length); + List simplified = simplify.simplify(Arrays.stream(POINTS_2D).toList(), 5.0f, false); + Assert.assertEquals("array should be simplified", 2, simplified.size()); - simplified = simplify.simplify(POINTS_2D, 5.0f, true); - Assert.assertEquals("array should be simplified", 2, simplified.length); + simplified = simplify.simplify(Arrays.stream(POINTS_2D).toList(), 5.0f, true); + Assert.assertEquals("array should be simplified", 2, simplified.size()); } @Test public void testInvalidPointsParam() { - Simplify aut = new Simplify(new MyPoint[0]); - Assert.assertNull("return null when point-array is null", aut.simplify(null, 1f, false)); + Simplify aut = new Simplify<>(); + Assert.assertThrows("throws when point-array is null", IllegalArgumentException.class, + () -> aut.simplify(null, 1f, false)); - Point[] only2 = new Point[2]; - only2[0] = new MyPoint(1, 2); - only2[1] = new MyPoint(2, 3); + List only2 = new ArrayList<>(); + only2.add(new MyPoint(1, 2)); + only2.add(new MyPoint(2, 3)); - Assert.assertTrue("return identical array when less than 3 points", - only2 == aut.simplify(only2, 1f, false)); + Assert.assertEquals("return identical array when less than 3 points", + only2, aut.simplify(only2, 1f, false)); } private void assertPointsEqual(float tolerance, boolean highQuality) throws Exception { Point[] pointsExpected = readPoints(tolerance, highQuality); long start = System.nanoTime(); - Simplify aut = new Simplify(new MyPoint[0]); - Point[] pointsActual = aut.simplify(allPoints, tolerance, highQuality); + Simplify aut = new Simplify<>(); + List pointsActual = aut.simplify(Arrays.stream(allPoints).toList(), tolerance, highQuality); long end = System.nanoTime(); System.out.println("tolerance=" + tolerance + " hq=" + highQuality + " nanos=" + (end - start)); @@ -98,8 +101,8 @@ private void assertPointsEqual(float tolerance, boolean highQuality) throws Exce Assert.assertNotNull("wrong test setup", pointsExpected); Assert.assertNotNull("simplify must return Point[]", pointsActual); - Assert.assertArrayEquals("tolerance=" + tolerance + " hq=" + highQuality, - pointsExpected, pointsActual); + Assert.assertEquals("tolerance=" + tolerance + " hq=" + highQuality, + Arrays.stream(pointsExpected).toList(), pointsActual); } private static Point[] readPoints(float tolerance, boolean highQuality) throws Exception { @@ -108,15 +111,13 @@ private static Point[] readPoints(float tolerance, boolean highQuality) throws E } static Point[] readPoints(String fileName) throws Exception { - List pointList = new ArrayList(); + List pointList = new ArrayList<>(); File file = new File("src/test/resources", fileName); - InputStream is = null; - try { - is = new FileInputStream(file); - BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + try (InputStream is = new FileInputStream(file)) { + BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); String line; while ((line = reader.readLine()) != null) { - if (line.trim().length() == 0) { + if (line.trim().isEmpty()) { continue; } String[] xy = line.split(","); @@ -124,12 +125,8 @@ static Point[] readPoints(String fileName) throws Exception { double y = Double.parseDouble(xy[1]); pointList.add(new MyPoint(x, y)); } - } finally { - if (is != null) { - is.close(); - } } - return pointList.toArray(new MyPoint[pointList.size()]); + return pointList.toArray(new MyPoint[0]); } private static class MyPoint implements Point { @@ -164,9 +161,7 @@ public boolean equals(Object o) { MyPoint myPoint = (MyPoint) o; if (Double.compare(myPoint.x, x) != 0) return false; - if (Double.compare(myPoint.y, y) != 0) return false; - - return true; + return Double.compare(myPoint.y, y) == 0; } }