You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2022/11/22 15:22:54 UTC

[incubator-baremaps] 02/07: Delete EntityConsumer

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

bchapuis pushed a commit to branch instanceof
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit 9f1b9e95436e5f604176c7df640000b3b9481995
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Tue Nov 22 01:15:47 2022 +0100

    Delete EntityConsumer
---
 .../benchmarks/OpenStreetMapBenchmark.java         | 30 +++-----
 .../OpenStreetMapGeometriesBenchmark.java          | 36 ++++-----
 .../apache/baremaps/database/ImportService.java    |  1 -
 .../function/CreateGeometryConsumer.java           | 29 ++++---
 .../openstreetmap/function/EntityConsumer.java     | 90 ----------------------
 .../function/EntityConsumerAdapter.java            | 40 ----------
 .../function/ReprojectEntityConsumer.java          | 30 ++------
 .../baremaps/openstreetmap/OpenStreetMapTest.java  | 51 ++++++------
 8 files changed, 73 insertions(+), 234 deletions(-)

diff --git a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapBenchmark.java b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapBenchmark.java
index bf19ac36..9fe2cfb4 100644
--- a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapBenchmark.java
+++ b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapBenchmark.java
@@ -13,7 +13,6 @@
 package org.apache.baremaps.benchmarks;
 
 
-
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -24,7 +23,7 @@ import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
-import org.apache.baremaps.openstreetmap.function.EntityConsumerAdapter;
+
 import org.apache.baremaps.openstreetmap.model.Node;
 import org.apache.baremaps.openstreetmap.model.Relation;
 import org.apache.baremaps.openstreetmap.model.Way;
@@ -73,28 +72,21 @@ public class OpenStreetMapBenchmark {
 
     try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(path))) {
       new PbfEntityReader(new PbfBlockReader()).stream(inputStream)
-          .forEach(new EntityConsumerAdapter() {
-            @Override
-            public void match(Node node) {
-              nodes.incrementAndGet();
-            }
-
-            @Override
-            public void match(Way way) {
-              ways.incrementAndGet();
-            }
-
-            @Override
-            public void match(Relation relation) {
-              relations.incrementAndGet();
-            }
-          });
+        .forEach(entity -> {
+          if (entity instanceof Node node) {
+            nodes.incrementAndGet();
+          } else if (entity instanceof Way way) {
+            ways.incrementAndGet();
+          } else if (entity instanceof Relation) {
+            relations.incrementAndGet();
+          }
+        });
     }
   }
 
   public static void main(String[] args) throws RunnerException {
     Options opt =
-        new OptionsBuilder().include(OpenStreetMapBenchmark.class.getSimpleName()).forks(1).build();
+      new OptionsBuilder().include(OpenStreetMapBenchmark.class.getSimpleName()).forks(1).build();
     new Runner(opt).run();
   }
 }
diff --git a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
index 3d6e4faa..f383da4a 100644
--- a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
+++ b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
@@ -13,7 +13,6 @@
 package org.apache.baremaps.benchmarks;
 
 
-
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -25,6 +24,7 @@ import java.nio.file.StandardCopyOption;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
+
 import org.apache.baremaps.collection.DataStore;
 import org.apache.baremaps.collection.LongDataMap;
 import org.apache.baremaps.collection.LongDataOpenHashMap;
@@ -33,7 +33,6 @@ import org.apache.baremaps.collection.memory.OnHeapMemory;
 import org.apache.baremaps.collection.type.CoordinateDataType;
 import org.apache.baremaps.collection.type.LongListDataType;
 import org.apache.baremaps.collection.utils.FileUtils;
-import org.apache.baremaps.openstreetmap.function.EntityConsumerAdapter;
 import org.apache.baremaps.openstreetmap.model.Node;
 import org.apache.baremaps.openstreetmap.model.Relation;
 import org.apache.baremaps.openstreetmap.model.Way;
@@ -79,38 +78,31 @@ public class OpenStreetMapGeometriesBenchmark {
   public void store() throws IOException {
     Path directory = Files.createTempDirectory(Paths.get("."), "baremaps_");
     LongDataMap<Coordinate> coordinates = new LongDataOpenHashMap<>(
-        new DataStore<>(new CoordinateDataType(), new OnDiskDirectoryMemory(directory)));
+      new DataStore<>(new CoordinateDataType(), new OnDiskDirectoryMemory(directory)));
     LongDataMap<List<Long>> references =
-        new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
+      new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
     AtomicLong nodes = new AtomicLong(0);
     AtomicLong ways = new AtomicLong(0);
     AtomicLong relations = new AtomicLong(0);
     try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(path))) {
       new PbfEntityReader(
-          new PbfBlockReader().coordinates(coordinates).references(references).projection(4326))
-              .stream(inputStream).forEach(new EntityConsumerAdapter() {
-                @Override
-                public void match(Node node) {
-                  nodes.incrementAndGet();
-                }
-
-                @Override
-                public void match(Way way) {
-                  ways.incrementAndGet();
-                }
-
-                @Override
-                public void match(Relation relation) {
-                  relations.incrementAndGet();
-                }
-              });
+        new PbfBlockReader().coordinates(coordinates).references(references).projection(4326))
+        .stream(inputStream).forEach(entity -> {
+          if (entity instanceof Node node) {
+            nodes.incrementAndGet();
+          } else if (entity instanceof Way way) {
+            ways.incrementAndGet();
+          } else if (entity instanceof Relation) {
+            relations.incrementAndGet();
+          }
+        });
     }
     FileUtils.deleteRecursively(directory);
   }
 
   public static void main(String[] args) throws RunnerException {
     Options opt = new OptionsBuilder()
-        .include(OpenStreetMapGeometriesBenchmark.class.getSimpleName()).forks(1).build();
+      .include(OpenStreetMapGeometriesBenchmark.class.getSimpleName()).forks(1).build();
     new Runner(opt).run();
   }
 }
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/ImportService.java b/baremaps-core/src/main/java/org/apache/baremaps/database/ImportService.java
index 1710f8d1..5e95bc9a 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/ImportService.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/ImportService.java
@@ -27,7 +27,6 @@ import org.apache.baremaps.database.repository.HeaderRepository;
 import org.apache.baremaps.database.repository.Repository;
 import org.apache.baremaps.openstreetmap.function.BlockEntityConsumer;
 import org.apache.baremaps.openstreetmap.function.CreateGeometryConsumer;
-import org.apache.baremaps.openstreetmap.function.EntityConsumer;
 import org.apache.baremaps.openstreetmap.function.ReprojectEntityConsumer;
 import org.apache.baremaps.openstreetmap.model.Block;
 import org.apache.baremaps.openstreetmap.model.Entity;
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CreateGeometryConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CreateGeometryConsumer.java
index 22857e08..22976987 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CreateGeometryConsumer.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CreateGeometryConsumer.java
@@ -20,11 +20,10 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Consumer;
+
 import org.apache.baremaps.collection.LongDataMap;
-import org.apache.baremaps.openstreetmap.model.Member;
-import org.apache.baremaps.openstreetmap.model.Node;
-import org.apache.baremaps.openstreetmap.model.Relation;
-import org.apache.baremaps.openstreetmap.model.Way;
+import org.apache.baremaps.openstreetmap.model.*;
 import org.apache.baremaps.stream.StreamException;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Geometry;
@@ -43,8 +42,8 @@ import org.locationtech.jts.operation.union.CascadedPolygonUnion;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/** A consumer that creates and sets the geometry of OpenStreetMap entities via side-effects. */
-public class CreateGeometryConsumer implements EntityConsumerAdapter {
+/** A consumer that creates and sets the geometry of OpenStreetMap entities via side effects. */
+public class CreateGeometryConsumer implements Consumer<Entity> {
 
   private static final Logger logger = LoggerFactory.getLogger(CreateGeometryConsumer.class);
 
@@ -65,15 +64,26 @@ public class CreateGeometryConsumer implements EntityConsumerAdapter {
     this.references = references;
   }
 
-  /** {@inheritDoc} */
   @Override
+  public void accept(Entity entity) {
+    if (entity instanceof Node node) {
+      match(node);
+    } else if (entity instanceof Way way) {
+      match(way);
+    } else if (entity instanceof Relation relation) {
+      match(relation);
+    } else {
+      // do nothing
+    }
+  }
+
+  /** {@inheritDoc} */
   public void match(Node node) {
     Point point = geometryFactory.createPoint(new Coordinate(node.getLon(), node.getLat()));
     node.setGeometry(point);
   }
 
   /** {@inheritDoc} */
-  @Override
   public void match(Way way) {
     try {
       List<Coordinate> list = way.getNodes().stream().map(coordinates::get).toList();
@@ -93,7 +103,6 @@ public class CreateGeometryConsumer implements EntityConsumerAdapter {
   }
 
   /** {@inheritDoc} */
-  @Override
   public void match(Relation relation) {
     try {
       Map<String, String> tags = relation.getTags();
@@ -208,4 +217,6 @@ public class CreateGeometryConsumer implements EntityConsumerAdapter {
       throw new StreamException(e);
     }
   }
+
+
 }
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumer.java
deleted file mode 100644
index 1ba0eb3d..00000000
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumer.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package org.apache.baremaps.openstreetmap.function;
-
-
-
-import java.util.function.Consumer;
-import org.apache.baremaps.openstreetmap.model.Bound;
-import org.apache.baremaps.openstreetmap.model.Entity;
-import org.apache.baremaps.openstreetmap.model.Header;
-import org.apache.baremaps.openstreetmap.model.Node;
-import org.apache.baremaps.openstreetmap.model.Relation;
-import org.apache.baremaps.openstreetmap.model.Way;
-import org.apache.baremaps.stream.StreamException;
-
-/** Represents an operation on entities of different types. */
-public interface EntityConsumer extends Consumer<Entity> {
-
-  /** {@inheritDoc} */
-  @Override
-  default void accept(Entity entity) {
-    try {
-      if (entity instanceof Node node) {
-        match(node);
-      } else if (entity instanceof Way way) {
-        match(way);
-      } else if (entity instanceof Relation relation) {
-        match(relation);
-      } else if (entity instanceof Header header) {
-        match(header);
-      } else if (entity instanceof Bound bound) {
-        match(bound);
-      } else {
-        throw new StreamException("Unknown entity type.");
-      }
-    } catch (Exception e) {
-      throw new StreamException(e);
-    }
-  }
-
-  /**
-   * Matches an operation on a {@code Header}.
-   *
-   * @param header the header
-   * @throws Exception
-   */
-  void match(Header header) throws Exception;
-
-  /**
-   * Matches an operation on a {@code Bound}.
-   *
-   * @param bound the bound
-   * @throws Exception
-   */
-  void match(Bound bound) throws Exception;
-
-  /**
-   * Matches an operation on a {@code Node}.
-   *
-   * @param node the node
-   * @throws Exception
-   */
-  void match(Node node) throws Exception;
-
-  /**
-   * Matches an operation on a {@code Way}.
-   *
-   * @param way the way
-   * @throws Exception
-   */
-  void match(Way way) throws Exception;
-
-  /**
-   * Matches an operation on a {@code Relation}.
-   *
-   * @param relation the relation
-   * @throws Exception
-   */
-  void match(Relation relation) throws Exception;
-}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumerAdapter.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumerAdapter.java
deleted file mode 100644
index 3f857874..00000000
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityConsumerAdapter.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package org.apache.baremaps.openstreetmap.function;
-
-
-
-import org.apache.baremaps.openstreetmap.model.Bound;
-import org.apache.baremaps.openstreetmap.model.Header;
-import org.apache.baremaps.openstreetmap.model.Node;
-import org.apache.baremaps.openstreetmap.model.Relation;
-import org.apache.baremaps.openstreetmap.model.Way;
-
-/** {@inheritDoc} */
-public interface EntityConsumerAdapter extends EntityConsumer {
-
-  /** {@inheritDoc} */
-  default void match(Header header) throws Exception {}
-
-  /** {@inheritDoc} */
-  default void match(Bound bound) throws Exception {}
-
-  /** {@inheritDoc} */
-  default void match(Node node) throws Exception {}
-
-  /** {@inheritDoc} */
-  default void match(Way way) throws Exception {}
-
-  /** {@inheritDoc} */
-  default void match(Relation relation) throws Exception {}
-}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReprojectEntityConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReprojectEntityConsumer.java
index 61b5ce31..40c2059c 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReprojectEntityConsumer.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReprojectEntityConsumer.java
@@ -15,14 +15,13 @@ package org.apache.baremaps.openstreetmap.function;
 
 
 import org.apache.baremaps.openstreetmap.geometry.ProjectionTransformer;
-import org.apache.baremaps.openstreetmap.model.Element;
-import org.apache.baremaps.openstreetmap.model.Node;
-import org.apache.baremaps.openstreetmap.model.Relation;
-import org.apache.baremaps.openstreetmap.model.Way;
+import org.apache.baremaps.openstreetmap.model.*;
 import org.locationtech.jts.geom.Geometry;
 
+import java.util.function.Consumer;
+
 /** Changes the projection of the geometry of an entity via side-effects. */
-public class ReprojectEntityConsumer implements EntityConsumerAdapter {
+public class ReprojectEntityConsumer implements Consumer<Entity> {
 
   private final ProjectionTransformer projectionTransformer;
 
@@ -38,25 +37,8 @@ public class ReprojectEntityConsumer implements EntityConsumerAdapter {
 
   /** {@inheritDoc} */
   @Override
-  public void match(Node node) {
-    handleElement(node);
-  }
-
-  /** {@inheritDoc} */
-  @Override
-  public void match(Way way) {
-    handleElement(way);
-  }
-
-  /** {@inheritDoc} */
-  @Override
-  public void match(Relation relation) {
-    handleElement(relation);
-  }
-
-  /** {@inheritDoc} */
-  private void handleElement(Element element) {
-    if (element.getGeometry() != null) {
+  public void accept(Entity entity) {
+    if (entity instanceof Element element && element.getGeometry() != null) {
       Geometry geometry = projectionTransformer.transform(element.getGeometry());
       element.setGeometry(geometry);
     }
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OpenStreetMapTest.java b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OpenStreetMapTest.java
index 5a303624..6c523317 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OpenStreetMapTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OpenStreetMapTest.java
@@ -32,7 +32,7 @@ import java.time.LocalDateTime;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
-import org.apache.baremaps.openstreetmap.function.EntityConsumer;
+
 import org.apache.baremaps.openstreetmap.model.Bound;
 import org.apache.baremaps.openstreetmap.model.Entity;
 import org.apache.baremaps.openstreetmap.model.Header;
@@ -74,8 +74,10 @@ class OpenStreetMapTest {
   @Test
   void dataOsmXmlRelations() throws IOException {
     try (InputStream input = Files.newInputStream(DATA_OSM_XML)) {
-      assertEquals(1,
-          new XmlEntityReader().stream(input).filter(e -> e instanceof Relation).count());
+      assertEquals(
+        1,
+        new XmlEntityReader().stream(input).filter(e -> e instanceof Relation).count()
+      );
     }
   }
 
@@ -97,7 +99,7 @@ class OpenStreetMapTest {
   void denseNodesOsmPbf() throws IOException {
     try (InputStream input = Files.newInputStream(DENSE_NODES_OSM_PBF)) {
       assertEquals(8000, new PbfEntityReader(new PbfBlockReader()).stream(input)
-          .filter(e -> e instanceof Node).count());
+        .filter(e -> e instanceof Node).count());
     }
   }
 
@@ -105,7 +107,7 @@ class OpenStreetMapTest {
   void waysOsmPbf() throws IOException {
     try (InputStream input = Files.newInputStream(WAYS_OSM_PBF)) {
       assertEquals(8000, new PbfEntityReader(new PbfBlockReader()).stream(input)
-          .filter(e -> e instanceof Way).count());
+        .filter(e -> e instanceof Way).count());
     }
   }
 
@@ -113,7 +115,7 @@ class OpenStreetMapTest {
   void relationsOsmPbf() throws IOException {
     try (InputStream input = Files.newInputStream(RELATIONS_OSM_PBF)) {
       assertEquals(8000, new PbfEntityReader(new PbfBlockReader()).stream(input)
-          .filter(e -> e instanceof Relation).count());
+        .filter(e -> e instanceof Relation).count());
     }
   }
 
@@ -136,52 +138,43 @@ class OpenStreetMapTest {
 
   @Test
   void monacoOsmBz2() throws IOException, URISyntaxException {
-    try (InputStream inputStream =
-        new BZip2CompressorInputStream(Files.newInputStream(MONACO_OSM_BZ2))) {
+    try (
+      InputStream inputStream =
+        new BZip2CompressorInputStream(Files.newInputStream(MONACO_OSM_BZ2))
+    ) {
       Stream<Entity> stream = new XmlEntityReader().stream(inputStream);
       process(stream, 1, 1, 24951, 4015, 243);
     }
   }
 
-  void process(Stream<Entity> stream, long headerCount, long boundCount, long nodeCount,
-      long wayCount, long relationCount) {
+  void process(
+    Stream<Entity> stream, long headerCount, long boundCount, long nodeCount,
+    long wayCount, long relationCount
+  ) {
     AtomicLong headers = new AtomicLong(0);
     AtomicLong bounds = new AtomicLong(0);
     AtomicLong nodes = new AtomicLong(0);
     AtomicLong ways = new AtomicLong(0);
     AtomicLong relations = new AtomicLong(0);
-    stream.forEach(new EntityConsumer() {
-      @Override
-      public void match(Header header) {
+    stream.forEach(entity -> {
+      if (entity instanceof Header header) {
         assertNotNull(header);
         assertEquals("osmium/1.8.0", header.getWritingProgram());
         headers.incrementAndGet();
-      }
-
-      @Override
-      public void match(Bound bound) {
+      } else if (entity instanceof Bound bound) {
         assertNotNull(bound);
         assertEquals(43.75169, bound.getMaxLat(), 0.000001);
         assertEquals(7.448637, bound.getMaxLon(), 0.000001);
         assertEquals(43.72335, bound.getMinLat(), 0.000001);
         assertEquals(7.409205, bound.getMinLon(), 0.000001);
         bounds.incrementAndGet();
-      }
-
-      @Override
-      public void match(Node node) {
+      } else if (entity instanceof Node node) {
         assertNotNull(node);
         nodes.incrementAndGet();
-      }
-
-      @Override
-      public void match(Way way) {
+      } else if (entity instanceof Way way) {
         assertNotNull(way);
         ways.incrementAndGet();
-      }
-
-      @Override
-      public void match(Relation relation) {
+      } else if (entity instanceof Relation relation) {
         assertNotNull(relation);
         relations.incrementAndGet();
       }