From 5cc3a4b31b2ff746aa292167c9bf88002d27fbb5 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Thu, 11 Apr 2024 11:17:56 -0400 Subject: [PATCH] refactor(MobilityProfileParser): Use EnumMap --- .../ext/mobilityprofile/MobilityProfileRoutingTest.java | 4 ++-- .../ext/mobilityprofile/MobilityProfileParser.java | 5 ++--- .../ext/mobilityprofile/MobilityProfileRouting.java | 3 +-- .../routing/graph/kryosupport/KryoBuilder.java | 4 ++++ .../opentripplanner/street/model/edge/StreetEdgeBuilder.java | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ext-test/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRoutingTest.java b/src/ext-test/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRoutingTest.java index 62ba94ca3af..d72e2ecd66b 100644 --- a/src/ext-test/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRoutingTest.java +++ b/src/ext-test/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRoutingTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.HashMap; +import java.util.EnumMap; import java.util.Map; import org.junit.jupiter.api.Test; import org.opentripplanner.openstreetmap.model.OSMWay; @@ -67,7 +67,7 @@ void canPreserveWalkPermissionOnFootway() { @Test void canProRateProfileCosts() { - Map profileCost = new HashMap<>(); + Map profileCost = new EnumMap<>(MobilityProfile.class); profileCost.put(MobilityProfile.NONE, 10.0f); profileCost.put(MobilityProfile.DEVICE, 100.0f); diff --git a/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileParser.java b/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileParser.java index 4b2f88f4315..f0359e99172 100644 --- a/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileParser.java +++ b/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileParser.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; @@ -67,9 +68,7 @@ private static void parseRow( String key = getKey(id, fromNode, toNode); float lengthMeters = ONE_MILE_IN_METERS * Float.parseFloat(reader.get("Link Length")); - // The weight map has to be a HashMap instead of an EnumMap so that it is correctly - // persisted in the graph. - var weightMap = new HashMap(); + var weightMap = new EnumMap(MobilityProfile.class); for (var profile : MobilityProfile.values()) { currentColumnHeader = profile.getText(); try { diff --git a/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRouting.java b/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRouting.java index db37a85d4ac..5257221e1aa 100644 --- a/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRouting.java +++ b/src/ext/java/org/opentripplanner/ext/mobilityprofile/MobilityProfileRouting.java @@ -3,7 +3,6 @@ import static java.util.Map.entry; import java.util.EnumMap; -import java.util.HashMap; import java.util.Map; import org.opentripplanner.openstreetmap.model.OSMWay; import org.opentripplanner.street.model.StreetTraversalPermission; @@ -85,7 +84,7 @@ public static Map getProRatedProfileCosts( float ratio ) { // Has to be a HashMap for graph serialization - Map result = new HashMap<>(); + Map result = new EnumMap<>(MobilityProfile.class); cost.forEach((k, v) -> result.put(k, v * ratio)); return result; } diff --git a/src/main/java/org/opentripplanner/routing/graph/kryosupport/KryoBuilder.java b/src/main/java/org/opentripplanner/routing/graph/kryosupport/KryoBuilder.java index d094f8c945f..3836b8aa6e0 100644 --- a/src/main/java/org/opentripplanner/routing/graph/kryosupport/KryoBuilder.java +++ b/src/main/java/org/opentripplanner/routing/graph/kryosupport/KryoBuilder.java @@ -3,6 +3,7 @@ import com.conveyal.kryo.TIntArrayListSerializer; import com.conveyal.kryo.TIntIntHashMapSerializer; import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.serializers.EnumMapSerializer; import com.esotericsoftware.kryo.serializers.ExternalizableSerializer; import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy; import com.google.common.collect.ArrayListMultimap; @@ -12,11 +13,13 @@ import gnu.trove.impl.hash.TPrimitiveHash; import gnu.trove.list.array.TIntArrayList; import gnu.trove.map.hash.TIntIntHashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import org.objenesis.strategy.SerializingInstantiatorStrategy; +import org.opentripplanner.ext.mobilityprofile.MobilityProfile; import org.opentripplanner.kryo.BuildConfigSerializer; import org.opentripplanner.kryo.RouterConfigSerializer; import org.opentripplanner.kryo.UnmodifiableCollectionsSerializer; @@ -65,6 +68,7 @@ public static Kryo create() { kryo.register(RouterConfig.class, new RouterConfigSerializer()); kryo.register(BuildConfig.class, new BuildConfigSerializer()); kryo.register(AtomicInteger.class, new AtomicIntegerSerializer()); + kryo.register(EnumMap.class, new EnumMapSerializer(MobilityProfile.class)); UnmodifiableCollectionsSerializer.registerSerializers(kryo); // Instantiation strategy: how should Kryo make new instances of objects when they are deserialized? diff --git a/src/main/java/org/opentripplanner/street/model/edge/StreetEdgeBuilder.java b/src/main/java/org/opentripplanner/street/model/edge/StreetEdgeBuilder.java index c7d20dd3c70..f49a37a5d6b 100644 --- a/src/main/java/org/opentripplanner/street/model/edge/StreetEdgeBuilder.java +++ b/src/main/java/org/opentripplanner/street/model/edge/StreetEdgeBuilder.java @@ -42,7 +42,7 @@ public class StreetEdgeBuilder> { private float bicycleSafetyFactor; private short flags; private StreetElevationExtension streetElevationExtension; - private Map profileCosts = new HashMap<>(); // new EnumMap<>(MobilityProfile.class); + private Map profileCosts = new EnumMap<>(MobilityProfile.class); public StreetEdgeBuilder() { this.defaultLength = true;