Skip to content

Commit

Permalink
refactor(MobilityProfileParser): Use EnumMap
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Apr 11, 2024
1 parent 00c0402 commit 5cc3a4b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +67,7 @@ void canPreserveWalkPermissionOnFootway() {

@Test
void canProRateProfileCosts() {
Map<MobilityProfile, Float> profileCost = new HashMap<>();
Map<MobilityProfile, Float> profileCost = new EnumMap<>(MobilityProfile.class);
profileCost.put(MobilityProfile.NONE, 10.0f);
profileCost.put(MobilityProfile.DEVICE, 100.0f);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MobilityProfile, Float>();
var weightMap = new EnumMap<MobilityProfile, Float>(MobilityProfile.class);
for (var profile : MobilityProfile.values()) {
currentColumnHeader = profile.getText();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +84,7 @@ public static Map<MobilityProfile, Float> getProRatedProfileCosts(
float ratio
) {
// Has to be a HashMap for graph serialization
Map<MobilityProfile, Float> result = new HashMap<>();
Map<MobilityProfile, Float> result = new EnumMap<>(MobilityProfile.class);
cost.forEach((k, v) -> result.put(k, v * ratio));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class StreetEdgeBuilder<B extends StreetEdgeBuilder<B>> {
private float bicycleSafetyFactor;
private short flags;
private StreetElevationExtension streetElevationExtension;
private Map<MobilityProfile, Float> profileCosts = new HashMap<>(); // new EnumMap<>(MobilityProfile.class);
private Map<MobilityProfile, Float> profileCosts = new EnumMap<>(MobilityProfile.class);

public StreetEdgeBuilder() {
this.defaultLength = true;
Expand Down

0 comments on commit 5cc3a4b

Please sign in to comment.