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 3e65774f3278ca48168a45d55720325615654ff2 Author: Bertil Chapuis <[email protected]> AuthorDate: Tue Nov 22 09:30:23 2022 +0100 Remove unnecessary classes --- .../benchmarks/OpenStreetMapBenchmark.java | 23 +++-- .../OpenStreetMapGeometriesBenchmark.java | 28 +++--- .../org/apache/baremaps/database/DiffService.java | 43 ++++------ .../apache/baremaps/database/ImportService.java | 4 +- .../baremaps/database/SaveBlockConsumer.java | 37 ++++---- .../baremaps/database/SaveChangeConsumer.java | 56 ++++++------ .../apache/baremaps/database/UpdateService.java | 4 +- .../openstreetmap/function/BlockConsumer.java | 57 ------------- .../function/BlockConsumerAdapter.java | 28 ------ ...ityConsumer.java => BlockEntitiesConsumer.java} | 30 ++++--- .../openstreetmap/function/BlockFunction.java | 62 -------------- .../openstreetmap/function/ChangeConsumer.java | 43 ---------- ...tyConsumer.java => ChangeEntitiesConsumer.java} | 6 +- .../openstreetmap/function/ChangeFunction.java | 48 ----------- .../function/CreateGeometryConsumer.java | 1 - .../openstreetmap/function/EntityFunction.java | 99 ---------------------- .../function/ExtractGeometryFunction.java | 55 +++++------- .../function/ReprojectEntityConsumer.java | 3 +- .../baremaps/openstreetmap/model/Change.java | 12 --- .../baremaps/openstreetmap/model/Element.java | 1 + .../baremaps/openstreetmap/pbf/PbfBlockReader.java | 4 +- .../openstreetmap/store/DataStoreConsumer.java | 24 ++++-- .../baremaps/openstreetmap/OpenStreetMapTest.java | 25 ++---- 23 files changed, 163 insertions(+), 530 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 9fe2cfb4..cb3f9c7f 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,6 +13,7 @@ package org.apache.baremaps.benchmarks; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -23,7 +24,6 @@ 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.model.Node; import org.apache.baremaps.openstreetmap.model.Relation; import org.apache.baremaps.openstreetmap.model.Way; @@ -71,22 +71,21 @@ public class OpenStreetMapBenchmark { AtomicLong relations = new AtomicLong(0); try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(path))) { - new PbfEntityReader(new PbfBlockReader()).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(); - } - }); + new PbfEntityReader(new PbfBlockReader()).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(); + } + }); } } 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 f383da4a..42c5d273 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,6 +13,7 @@ package org.apache.baremaps.benchmarks; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -24,7 +25,6 @@ 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; @@ -78,31 +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(entity -> { - if (entity instanceof Node node) { - nodes.incrementAndGet(); - } else if (entity instanceof Way way) { - ways.incrementAndGet(); - } else if (entity instanceof 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/DiffService.java b/baremaps-core/src/main/java/org/apache/baremaps/database/DiffService.java index 8862b3cf..14056fc3 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/database/DiffService.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/database/DiffService.java @@ -32,16 +32,11 @@ import org.apache.baremaps.database.repository.HeaderRepository; import org.apache.baremaps.database.repository.Repository; import org.apache.baremaps.database.tile.Tile; import org.apache.baremaps.openstreetmap.function.CreateGeometryConsumer; -import org.apache.baremaps.openstreetmap.function.EntityFunction; import org.apache.baremaps.openstreetmap.function.ExtractGeometryFunction; import org.apache.baremaps.openstreetmap.geometry.ProjectionTransformer; -import org.apache.baremaps.openstreetmap.model.Bound; -import org.apache.baremaps.openstreetmap.model.Change; -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.openstreetmap.model.*; import org.apache.baremaps.openstreetmap.xml.XmlChangeReader; +import org.apache.baremaps.stream.StreamException; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.slf4j.Logger; @@ -114,35 +109,27 @@ public class DiffService implements Callable<List<Tile>> { } private Stream<Geometry> geometriesForPreviousVersion(Change change) { - return change.getEntities().stream().map(new EntityFunction<Optional<Geometry>>() { - @Override - public Optional<Geometry> match(Header header) { - return Optional.empty(); - } - - @Override - public Optional<Geometry> match(Bound bound) { - return Optional.empty(); - } + return change.getEntities().stream().map(this::geometriesForPreviousVersion) + .flatMap(Optional::stream); + } - @Override - public Optional<Geometry> match(Node node) throws Exception { + private Optional<Geometry> geometriesForPreviousVersion(Entity entity) { + try { + if (entity instanceof Node node) { Node previousNode = nodeRepository.get(node.getId()); return Optional.ofNullable(previousNode).map(Node::getGeometry); - } - - @Override - public Optional<Geometry> match(Way way) throws Exception { + } else if (entity instanceof Way way) { Way previousWay = wayRepository.get(way.getId()); return Optional.ofNullable(previousWay).map(Way::getGeometry); - } - - @Override - public Optional<Geometry> match(Relation relation) throws Exception { + } else if (entity instanceof Relation relation) { Relation previousRelation = relationRepository.get(relation.getId()); return Optional.ofNullable(previousRelation).map(Relation::getGeometry); + } else { + return Optional.empty(); } - }).flatMap(Optional::stream); + } catch (Exception e) { + throw new StreamException(e); + } } private Stream<Geometry> geometriesForNextVersion(Change change) { 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 5e95bc9a..1320aa61 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 @@ -25,7 +25,7 @@ import java.util.function.Function; import org.apache.baremaps.collection.LongDataMap; 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.BlockEntitiesConsumer; import org.apache.baremaps.openstreetmap.function.CreateGeometryConsumer; import org.apache.baremaps.openstreetmap.function.ReprojectEntityConsumer; import org.apache.baremaps.openstreetmap.model.Block; @@ -68,7 +68,7 @@ public class ImportService implements Callable<Void> { Consumer<Entity> createGeometry = new CreateGeometryConsumer(coordinates, references); Consumer<Entity> reprojectGeometry = new ReprojectEntityConsumer(4326, databaseSrid); Consumer<Block> prepareGeometries = - new BlockEntityConsumer(createGeometry.andThen(reprojectGeometry)); + new BlockEntitiesConsumer(createGeometry.andThen(reprojectGeometry)); Function<Block, Block> prepareBlock = consumeThenReturn(cacheBlock.andThen(prepareGeometries)); Consumer<Block> saveBlock = new SaveBlockConsumer(headerRepository, nodeRepository, wayRepository, relationRepository); diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/SaveBlockConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/database/SaveBlockConsumer.java index 5e5c3489..eb40a00a 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/database/SaveBlockConsumer.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/database/SaveBlockConsumer.java @@ -14,17 +14,13 @@ package org.apache.baremaps.database; +import java.util.function.Consumer; import org.apache.baremaps.database.repository.Repository; -import org.apache.baremaps.openstreetmap.function.BlockConsumerAdapter; -import org.apache.baremaps.openstreetmap.model.DataBlock; -import org.apache.baremaps.openstreetmap.model.Header; -import org.apache.baremaps.openstreetmap.model.HeaderBlock; -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; /** A consumer for saving OpenStreetMap blocks in a database. */ -public class SaveBlockConsumer implements BlockConsumerAdapter { +public class SaveBlockConsumer implements Consumer<Block> { private final Repository<Long, Header> headerRepository; private final Repository<Long, Node> nodeRepository; @@ -48,18 +44,19 @@ public class SaveBlockConsumer implements BlockConsumerAdapter { this.relationRepository = relationRepository; } - /** {@inheritDoc} */ @Override - public void match(HeaderBlock headerBlock) throws Exception { - headerRepository.put(headerBlock.getHeader()); - } - - /** {@inheritDoc} */ - @Override - public void match(DataBlock dataBlock) throws Exception { - nodeRepository.copy(dataBlock.getDenseNodes()); - nodeRepository.copy(dataBlock.getNodes()); - wayRepository.copy(dataBlock.getWays()); - relationRepository.copy(dataBlock.getRelations()); + public void accept(Block block) { + try { + if (block instanceof HeaderBlock headerBlock) { + headerRepository.put(headerBlock.getHeader()); + } else if (block instanceof DataBlock dataBlock) { + nodeRepository.copy(dataBlock.getDenseNodes()); + nodeRepository.copy(dataBlock.getNodes()); + wayRepository.copy(dataBlock.getWays()); + relationRepository.copy(dataBlock.getRelations()); + } + } catch (Exception e) { + throw new StreamException(e); + } } } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/SaveChangeConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/database/SaveChangeConsumer.java index d8146b58..59e4a805 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/database/SaveChangeConsumer.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/database/SaveChangeConsumer.java @@ -14,16 +14,18 @@ package org.apache.baremaps.database; +import java.util.function.Consumer; import org.apache.baremaps.database.repository.Repository; -import org.apache.baremaps.openstreetmap.function.ChangeConsumer; +import org.apache.baremaps.database.repository.RepositoryException; import org.apache.baremaps.openstreetmap.model.Change; import org.apache.baremaps.openstreetmap.model.Entity; 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; /** A consumer for saving OpenStreetMap changes in a database. */ -public class SaveChangeConsumer implements ChangeConsumer { +public class SaveChangeConsumer implements Consumer<Change> { private final Repository<Long, Node> nodeRepository; private final Repository<Long, Way> wayRepository; @@ -45,29 +47,35 @@ public class SaveChangeConsumer implements ChangeConsumer { /** {@inheritDoc} */ @Override - public void match(Change change) throws Exception { - for (Entity entity : change.getEntities()) { - switch (change.getType()) { - case CREATE: - case MODIFY: - if (entity instanceof Node node) { - nodeRepository.put(node); - } else if (entity instanceof Way way) { - wayRepository.put(way); - } else if (entity instanceof Relation relation) { - relationRepository.put(relation); - } - break; - case DELETE: - if (entity instanceof Node node) { - nodeRepository.delete(node.getId()); - } else if (entity instanceof Way way) { - wayRepository.delete(way.getId()); - } else if (entity instanceof Relation relation) { - relationRepository.delete(relation.getId()); - } - break; + public void accept(Change change) { + try { + for (Entity entity : change.getEntities()) { + switch (change.getType()) { + case CREATE: + case MODIFY: + if (entity instanceof Node node) { + nodeRepository.put(node); + } else if (entity instanceof Way way) { + wayRepository.put(way); + } else if (entity instanceof Relation relation) { + relationRepository.put(relation); + } + break; + case DELETE: + if (entity instanceof Node node) { + nodeRepository.delete(node.getId()); + } else if (entity instanceof Way way) { + wayRepository.delete(way.getId()); + } else if (entity instanceof Relation relation) { + relationRepository.delete(relation.getId()); + } + break; + } } + } catch (RepositoryException e) { + throw new StreamException(e); } } + + } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/UpdateService.java b/baremaps-core/src/main/java/org/apache/baremaps/database/UpdateService.java index 6b45ed69..5d2060e0 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/database/UpdateService.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/database/UpdateService.java @@ -27,7 +27,7 @@ import java.util.zip.GZIPInputStream; import org.apache.baremaps.collection.LongDataMap; import org.apache.baremaps.database.repository.HeaderRepository; import org.apache.baremaps.database.repository.Repository; -import org.apache.baremaps.openstreetmap.function.ChangeEntityConsumer; +import org.apache.baremaps.openstreetmap.function.ChangeEntitiesConsumer; import org.apache.baremaps.openstreetmap.function.CreateGeometryConsumer; import org.apache.baremaps.openstreetmap.function.ReprojectEntityConsumer; import org.apache.baremaps.openstreetmap.model.Change; @@ -73,7 +73,7 @@ public class UpdateService implements Callable<Void> { Consumer<Entity> createGeometry = new CreateGeometryConsumer(coordinates, references); Consumer<Entity> reprojectGeometry = new ReprojectEntityConsumer(4326, srid); Consumer<Change> prepareGeometries = - new ChangeEntityConsumer(createGeometry.andThen(reprojectGeometry)); + new ChangeEntitiesConsumer(createGeometry.andThen(reprojectGeometry)); Function<Change, Change> prepareChange = consumeThenReturn(prepareGeometries); Consumer<Change> saveChange = new SaveChangeConsumer(nodeRepository, wayRepository, relationRepository); diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumer.java deleted file mode 100644 index 70b6a8cd..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumer.java +++ /dev/null @@ -1,57 +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.Block; -import org.apache.baremaps.openstreetmap.model.DataBlock; -import org.apache.baremaps.openstreetmap.model.HeaderBlock; -import org.apache.baremaps.stream.StreamException; - -/** Represents an operation on blocks of different types. */ -public interface BlockConsumer extends Consumer<Block> { - - /** {@inheritDoc} */ - @Override - default void accept(Block block) { - try { - if (block instanceof HeaderBlock headerBlock) { - match(headerBlock); - } else if (block instanceof DataBlock dataBlock) { - match(dataBlock); - } else { - throw new StreamException("Unknown block type."); - } - } catch (Exception e) { - throw new StreamException(e); - } - } - - /** - * Matches an operation on a {@code HeaderBlock}. - * - * @param headerBlock the header block - * @throws Exception - */ - void match(HeaderBlock headerBlock) throws Exception; - - /** - * Matches an operation on a {@code DataBlock}. - * - * @param dataBlock the data block - * @throws Exception - */ - void match(DataBlock dataBlock) throws Exception; -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumerAdapter.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumerAdapter.java deleted file mode 100644 index 3879d376..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockConsumerAdapter.java +++ /dev/null @@ -1,28 +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.DataBlock; -import org.apache.baremaps.openstreetmap.model.HeaderBlock; - -/** {@inheritDoc} */ -public interface BlockConsumerAdapter extends BlockConsumer { - - /** {@inheritDoc} */ - default void match(HeaderBlock headerBlock) throws Exception {} - - /** {@inheritDoc} */ - default void match(DataBlock dataBlock) throws Exception {} -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntityConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntitiesConsumer.java similarity index 60% rename from baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntityConsumer.java rename to baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntitiesConsumer.java index f0b87c34..1863f7b9 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntityConsumer.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockEntitiesConsumer.java @@ -15,12 +15,14 @@ package org.apache.baremaps.openstreetmap.function; import java.util.function.Consumer; +import org.apache.baremaps.openstreetmap.model.Block; import org.apache.baremaps.openstreetmap.model.DataBlock; import org.apache.baremaps.openstreetmap.model.Entity; import org.apache.baremaps.openstreetmap.model.HeaderBlock; +import org.apache.baremaps.stream.StreamException; /** Represents an operation on the entities of blocks of different types. */ -public class BlockEntityConsumer implements BlockConsumer { +public class BlockEntitiesConsumer implements Consumer<Block> { private final Consumer<Entity> consumer; @@ -29,23 +31,23 @@ public class BlockEntityConsumer implements BlockConsumer { * * @param consumer the entity consumer */ - public BlockEntityConsumer(Consumer<Entity> consumer) { + public BlockEntitiesConsumer(Consumer<Entity> consumer) { this.consumer = consumer; } - /** {@inheritDoc} */ @Override - public void match(HeaderBlock headerBlock) throws Exception { - consumer.accept(headerBlock.getHeader()); - consumer.accept(headerBlock.getBound()); + public void accept(Block block) { + if (block instanceof HeaderBlock headerBlock) { + consumer.accept(headerBlock.getHeader()); + consumer.accept(headerBlock.getBound()); + } else if (block instanceof DataBlock dataBlock) { + dataBlock.getDenseNodes().forEach(consumer); + dataBlock.getNodes().forEach(consumer); + dataBlock.getWays().forEach(consumer); + dataBlock.getRelations().forEach(consumer); + } else { + throw new StreamException("Unknown block type."); + } } - /** {@inheritDoc} */ - @Override - public void match(DataBlock dataBlock) throws Exception { - dataBlock.getDenseNodes().forEach(consumer); - dataBlock.getNodes().forEach(consumer); - dataBlock.getWays().forEach(consumer); - dataBlock.getRelations().forEach(consumer); - } } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockFunction.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockFunction.java deleted file mode 100644 index 17303b4c..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/BlockFunction.java +++ /dev/null @@ -1,62 +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.Function; -import org.apache.baremaps.openstreetmap.model.Block; -import org.apache.baremaps.openstreetmap.model.DataBlock; -import org.apache.baremaps.openstreetmap.model.HeaderBlock; -import org.apache.baremaps.stream.StreamException; - -/** - * Represents an function on blocks of different types. - * - * @param <T> - */ -public interface BlockFunction<T> extends Function<Block, T> { - - /** {@inheritDoc} */ - @Override - default T apply(Block block) { - try { - if (block instanceof HeaderBlock headerBlock) { - return match(headerBlock); - } else if (block instanceof DataBlock dataBlock) { - return match(dataBlock); - } else { - throw new StreamException("Unknown block type."); - } - } catch (Exception e) { - throw new StreamException(e); - } - } - - /** - * Applies a function on a {@code HeaderBlock}. - * - * @param headerBlock the header block - * @throws Exception - */ - T match(HeaderBlock headerBlock) throws Exception; - - /** - * Applies a function on a {@code DataBlock}. - * - * @param dataBlock - * @return the function result - * @throws Exception - */ - T match(DataBlock dataBlock) throws Exception; -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeConsumer.java deleted file mode 100644 index a9507c2e..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeConsumer.java +++ /dev/null @@ -1,43 +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.Change; -import org.apache.baremaps.stream.StreamException; - -/** Represents an operation on changes of different types. */ -public interface ChangeConsumer extends Consumer<Change> { - - /** {@inheritDoc} */ - @Override - default void accept(Change change) { - try { - change.visit(this); - } catch (StreamException e) { - throw e; - } catch (Exception e) { - throw new StreamException(e); - } - } - - /** - * Matches an operation on a {@code Change}. - * - * @param change the change - * @throws Exception - */ - void match(Change change) throws Exception; -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntityConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntitiesConsumer.java similarity index 86% rename from baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntityConsumer.java rename to baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntitiesConsumer.java index 1662e6f6..85928875 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntityConsumer.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeEntitiesConsumer.java @@ -19,7 +19,7 @@ import org.apache.baremaps.openstreetmap.model.Change; import org.apache.baremaps.openstreetmap.model.Entity; /** Represents an operation on the entities of changes of different types. */ -public class ChangeEntityConsumer implements ChangeConsumer { +public class ChangeEntitiesConsumer implements Consumer<Change> { private final Consumer<Entity> consumer; @@ -29,13 +29,13 @@ public class ChangeEntityConsumer implements ChangeConsumer { * * @param consumer */ - public ChangeEntityConsumer(Consumer<Entity> consumer) { + public ChangeEntitiesConsumer(Consumer<Entity> consumer) { this.consumer = consumer; } /** {@inheritDoc} */ @Override - public void match(Change change) throws Exception { + public void accept(Change change) { change.getEntities().forEach(consumer); } } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeFunction.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeFunction.java deleted file mode 100644 index 8f9510bb..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ChangeFunction.java +++ /dev/null @@ -1,48 +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.Function; -import org.apache.baremaps.openstreetmap.model.Change; -import org.apache.baremaps.stream.StreamException; - -/** - * Represents a function that transforms entities of different types. - * - * @param <T> - */ -public interface ChangeFunction<T> extends Function<Change, T> { - - /** {@inheritDoc} */ - @Override - default T apply(Change change) { - try { - return change.visit(this); - } catch (StreamException e) { - throw e; - } catch (Exception e) { - throw new StreamException(e); - } - } - - /** - * Applies a function on a {@code Change}. - * - * @param change the change - * @return the function result - * @throws Exception - */ - T match(Change change) throws Exception; -} 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 22976987..4d1f0595 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 @@ -21,7 +21,6 @@ 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.*; import org.apache.baremaps.stream.StreamException; diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityFunction.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityFunction.java deleted file mode 100644 index a2ec7913..00000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/EntityFunction.java +++ /dev/null @@ -1,99 +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.Function; -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 a function that transforms entities of different types. - * - * @param <T> - */ -public interface EntityFunction<T> extends Function<Entity, T> { - - /** {@inheritDoc} */ - @Override - default T apply(Entity entity) { - try { - if (entity instanceof Node node) { - return match(node); - } else if (entity instanceof Way way) { - return match(way); - } else if (entity instanceof Relation relation) { - return match(relation); - } else if (entity instanceof Header header) { - return match(header); - } else if (entity instanceof Bound bound) { - return match(bound); - } else { - throw new StreamException("Unknown entity type."); - } - } catch (Exception e) { - throw new StreamException(e); - } - } - - /** - * Applies a function on a {@code Header}. - * - * @param header the header - * @return the function result - * @throws Exception - */ - T match(Header header) throws Exception; - - /** - * Applies a function on a {@code Bound}. - * - * @param bound the bound - * @return the function result - * @throws Exception - */ - T match(Bound bound) throws Exception; - - /** - * Applies a function on a {@code Node}. - * - * @param node the node - * @return the function result - * @throws Exception - */ - T match(Node node) throws Exception; - - /** - * Applies a function on a {@code Way}. - * - * @param way the way - * @return the function result - * @throws Exception - */ - T match(Way way) throws Exception; - - /** - * Applies a function on a {@code Relation}. - * - * @param relation the relation - * @return the function result - * @throws Exception - */ - T match(Relation relation) throws Exception; -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ExtractGeometryFunction.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ExtractGeometryFunction.java index 1552e354..f874135b 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ExtractGeometryFunction.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ExtractGeometryFunction.java @@ -15,43 +15,32 @@ package org.apache.baremaps.openstreetmap.function; import java.util.Optional; -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; +import java.util.function.Function; +import org.apache.baremaps.openstreetmap.model.*; +import org.apache.baremaps.stream.StreamException; import org.locationtech.jts.geom.Geometry; /** A function that maps an {@code Entity} to its {@code Geometry}. */ -public class ExtractGeometryFunction implements EntityFunction<Optional<Geometry>> { +public class ExtractGeometryFunction implements Function<Entity, Optional<Geometry>> { - /** {@inheritDoc} */ @Override - public Optional<Geometry> match(Header header) throws Exception { - return Optional.empty(); - } - - /** {@inheritDoc} */ - @Override - public Optional<Geometry> match(Bound bound) throws Exception { - return Optional.empty(); - } - - /** {@inheritDoc} */ - @Override - public Optional<Geometry> match(Node node) throws Exception { - return Optional.ofNullable(node.getGeometry()); - } - - /** {@inheritDoc} */ - @Override - public Optional<Geometry> match(Way way) throws Exception { - return Optional.ofNullable(way.getGeometry()); - } - - /** {@inheritDoc} */ - @Override - public Optional<Geometry> match(Relation relation) throws Exception { - return Optional.ofNullable(relation.getGeometry()); + public Optional<Geometry> apply(Entity entity) { + try { + if (entity instanceof Node node) { + return Optional.ofNullable(node.getGeometry()); + } else if (entity instanceof Way way) { + return Optional.ofNullable(way.getGeometry()); + } else if (entity instanceof Relation relation) { + return Optional.ofNullable(relation.getGeometry()); + } else if (entity instanceof Header header) { + return Optional.empty(); + } else if (entity instanceof Bound bound) { + return Optional.empty(); + } else { + throw new StreamException("Unknown entity type."); + } + } catch (Exception e) { + throw new StreamException(e); + } } } 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 40c2059c..41e9fd46 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 @@ -14,12 +14,11 @@ package org.apache.baremaps.openstreetmap.function; +import java.util.function.Consumer; import org.apache.baremaps.openstreetmap.geometry.ProjectionTransformer; 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 Consumer<Entity> { diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Change.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Change.java index 5cfd942e..c8c8d354 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Change.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Change.java @@ -16,8 +16,6 @@ package org.apache.baremaps.openstreetmap.model; import java.util.List; import java.util.StringJoiner; -import org.apache.baremaps.openstreetmap.function.ChangeConsumer; -import org.apache.baremaps.openstreetmap.function.ChangeFunction; /** Represents a change in an OpenStreetMap dataset. */ public final class Change { @@ -59,16 +57,6 @@ public final class Change { return entities; } - /** Visits the entity with the provided entity consumer. */ - public void visit(ChangeConsumer consumer) throws Exception { - consumer.match(this); - } - - /** Visits the entity with the provided entity function. */ - public <T> T visit(ChangeFunction<T> function) throws Exception { - return function.match(this); - } - /** {@inheritDoc} */ @Override public String toString() { diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Element.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Element.java index d8b3b5e4..52200f87 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Element.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/model/Element.java @@ -24,6 +24,7 @@ import org.locationtech.jts.geom.Geometry; * world. */ public sealed + abstract class Element implements Entity permits Node, Way, Relation { diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/pbf/PbfBlockReader.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/pbf/PbfBlockReader.java index 68f1367a..25e822dc 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/pbf/PbfBlockReader.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/pbf/PbfBlockReader.java @@ -21,7 +21,7 @@ import java.util.function.Function; import java.util.stream.Stream; import org.apache.baremaps.collection.LongDataMap; import org.apache.baremaps.openstreetmap.OsmReader; -import org.apache.baremaps.openstreetmap.function.BlockEntityConsumer; +import org.apache.baremaps.openstreetmap.function.BlockEntitiesConsumer; import org.apache.baremaps.openstreetmap.function.CreateGeometryConsumer; import org.apache.baremaps.openstreetmap.function.ReprojectEntityConsumer; import org.apache.baremaps.openstreetmap.model.Blob; @@ -161,7 +161,7 @@ public class PbfBlockReader implements OsmReader<Block> { Consumer<Entity> reprojectGeometry = new ReprojectEntityConsumer(4326, srid); createGeometry = createGeometry.andThen(reprojectGeometry); } - Consumer<Block> prepareGeometries = new BlockEntityConsumer(createGeometry); + Consumer<Block> prepareGeometries = new BlockEntitiesConsumer(createGeometry); Function<Block, Block> prepareBlock = consumeThenReturn(cacheBlock.andThen(prepareGeometries)); blocks = blocks.map(prepareBlock); diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/store/DataStoreConsumer.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/store/DataStoreConsumer.java index 5121f7b6..11e5f52f 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/store/DataStoreConsumer.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/store/DataStoreConsumer.java @@ -15,13 +15,15 @@ package org.apache.baremaps.openstreetmap.store; import java.util.List; +import java.util.function.Consumer; import org.apache.baremaps.collection.LongDataMap; -import org.apache.baremaps.openstreetmap.function.BlockConsumerAdapter; +import org.apache.baremaps.openstreetmap.model.Block; import org.apache.baremaps.openstreetmap.model.DataBlock; +import org.apache.baremaps.stream.StreamException; import org.locationtech.jts.geom.Coordinate; /** A consumer that stores osm nodes and ways in the provided caches. */ -public class DataStoreConsumer implements BlockConsumerAdapter { +public class DataStoreConsumer implements Consumer<Block> { private final LongDataMap<Coordinate> coordinates; private final LongDataMap<List<Long>> references; @@ -40,11 +42,17 @@ public class DataStoreConsumer implements BlockConsumerAdapter { /** {@inheritDoc} */ @Override - public void match(DataBlock dataBlock) throws Exception { - dataBlock.getDenseNodes().stream().forEach( - node -> coordinates.put(node.getId(), new Coordinate(node.getLon(), node.getLat()))); - dataBlock.getNodes().stream().forEach( - node -> coordinates.put(node.getId(), new Coordinate(node.getLon(), node.getLat()))); - dataBlock.getWays().stream().forEach(way -> references.put(way.getId(), way.getNodes())); + public void accept(Block block) { + try { + if (block instanceof DataBlock dataBlock) { + dataBlock.getDenseNodes().stream().forEach( + node -> coordinates.put(node.getId(), new Coordinate(node.getLon(), node.getLat()))); + dataBlock.getNodes().stream().forEach( + node -> coordinates.put(node.getId(), new Coordinate(node.getLon(), node.getLat()))); + dataBlock.getWays().stream().forEach(way -> references.put(way.getId(), way.getNodes())); + } + } catch (Exception e) { + throw new StreamException(e); + } } } 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 6c523317..441b29a1 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,6 @@ 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.model.Bound; import org.apache.baremaps.openstreetmap.model.Entity; import org.apache.baremaps.openstreetmap.model.Header; @@ -74,10 +73,8 @@ 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()); } } @@ -99,7 +96,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()); } } @@ -107,7 +104,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()); } } @@ -115,7 +112,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()); } } @@ -138,19 +135,15 @@ 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);
