From b43fb6332a9980f3abad8d0f4c1e5ab2e85ec3e0 Mon Sep 17 00:00:00 2001 From: MadWasp79 Date: Mon, 9 Dec 2019 15:57:20 +0200 Subject: [PATCH] refactoring --- .../java/net/osmand/router/GeneralRouter.java | 80 ++++++++++--------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/router/GeneralRouter.java b/OsmAnd-java/src/main/java/net/osmand/router/GeneralRouter.java index 9c83addeda..1dade6b075 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/GeneralRouter.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/GeneralRouter.java @@ -53,10 +53,10 @@ public class GeneralRouter implements VehicleRouter { private String filename = null; private String profileName = ""; - Map> priorityCache = new HashMap<>(); - Map> speedCache = new HashMap<>(); - Map> obstacleCache = new HashMap<>(); - Map> penaltyCache = new HashMap<>(); + Map> priorityCache; + Map> speedCache; + Map> obstacleCache; + Map> penaltyCache; int count = 0; int count2 = 0; @@ -136,6 +136,11 @@ public class GeneralRouter implements VehicleRouter { tagRuleMask = new LinkedHashMap(); ruleToValue = new ArrayList(); parameters = new LinkedHashMap(); + + priorityCache = new HashMap<>(); + speedCache = new HashMap<>(); + obstacleCache = new HashMap<>(); + penaltyCache = new HashMap<>(); } @@ -189,6 +194,11 @@ public class GeneralRouter implements VehicleRouter { if (params.containsKey(MAX_SPEED)) { maxSpeed = parseSilentFloat(params.get(MAX_SPEED), maxSpeed); } + + priorityCache = new HashMap<>(); + speedCache = new HashMap<>(); + obstacleCache = new HashMap<>(); + penaltyCache = new HashMap<>(); } public GeneralRouterProfile getProfile() { @@ -353,19 +363,20 @@ public class GeneralRouter implements VehicleRouter { public float defineRoutingObstacle(RouteDataObject road, int point) { int[] pointTypes = road.getPointTypes(point); String region = road.region.getName(); - if (obstacleCache.containsKey(region)) { - if (obstacleCache.get(region).containsKey(pointTypes)) { - return obstacleCache.get(region).get(pointTypes); - } - } else { + if (!obstacleCache.containsKey(region)) { obstacleCache.put(region, new HashMap()); } + if (obstacleCache.get(region).containsKey(pointTypes)) { + return obstacleCache.get(region).get(pointTypes); + } + if(pointTypes != null){ float obst = getObjContext(RouteDataObjectAttribute.ROUTING_OBSTACLES).evaluateFloat(road.region, pointTypes, 0); obstacleCache.get(region).put(pointTypes, obst); return obst; } + return 0; } @@ -418,16 +429,14 @@ public class GeneralRouter implements VehicleRouter { @Override public float defineRoutingSpeed(RouteDataObject road) { String regionName = road.region.getName(); - if (speedCache.containsKey(regionName)) { - if (speedCache.get(regionName).get(road.types) != null) { - return speedCache.get(regionName).get(road.types); - } - } else { + if (!speedCache.containsKey(regionName)) { speedCache.put(regionName, new HashMap()); + } + if (speedCache.get(regionName).get(road.types) != null) { + return speedCache.get(regionName).get(road.types); } float spd = getObjContext(RouteDataObjectAttribute.ROAD_SPEED).evaluateFloat(road, defaultSpeed); float definedSpd = Math.max(Math.min(spd, maxSpeed), minSpeed); - speedCache.get(regionName).put(road.types, definedSpd); return definedSpd; } @@ -441,15 +450,13 @@ public class GeneralRouter implements VehicleRouter { @Override public float defineSpeedPriority(RouteDataObject road) { String regionName = road.region.getName(); - if (priorityCache.containsKey(regionName)) { - if (priorityCache.get(regionName).get(road.types) != null) { - return priorityCache.get(regionName).get(road.types); - } - } else { + if (!priorityCache.containsKey(regionName)) { priorityCache.put(regionName, new HashMap()); } - float sp = getObjContext(RouteDataObjectAttribute.ROAD_PRIORITIES).evaluateFloat(road, 1f); - + if (priorityCache.get(regionName).get(road.types) != null) { + return priorityCache.get(regionName).get(road.types); + } + float sp = getObjContext(RouteDataObjectAttribute.ROAD_PRIORITIES).evaluateFloat(road, 1f); priorityCache.get(regionName).put(road.types, sp); return sp; } @@ -487,28 +494,25 @@ public class GeneralRouter implements VehicleRouter { String regionPs = prev.getRoad().region.getName(); float ts; float prevTs; - if (penaltyCache.containsKey(regionS)) { - if (penaltyCache.get(regionS).get(segment.getRoad().types) != null) { - ts = penaltyCache.get(regionS).get(segment.getRoad().types); - } else { - ts = getPenaltyTransition(segment.getRoad()); - penaltyCache.get(regionS).put(segment.getRoad().types, ts); - } - } else { + + if (!penaltyCache.containsKey(regionS)) { penaltyCache.put(regionS, new HashMap()); + } + //maybe we don't need it? region should already be present in cache + if (!penaltyCache.containsKey(regionPs)) { + penaltyCache.put(regionPs, new HashMap()); + } + + if (penaltyCache.get(regionS).get(segment.getRoad().types) != null) { + ts = penaltyCache.get(regionS).get(segment.getRoad().types); + } else { ts = getPenaltyTransition(segment.getRoad()); penaltyCache.get(regionS).put(segment.getRoad().types, ts); } - - if (penaltyCache.containsKey(regionPs)) { - if (penaltyCache.get(regionPs).get(prev.getRoad().types) != null) { + + if (penaltyCache.get(regionPs).get(prev.getRoad().types) != null) { prevTs = penaltyCache.get(regionPs).get(prev.getRoad().types); - } else { - prevTs = getPenaltyTransition(prev.getRoad()); - penaltyCache.get(regionPs).put(prev.getRoad().types, prevTs); - } } else { - penaltyCache.put(regionPs, new HashMap()); prevTs = getPenaltyTransition(prev.getRoad()); penaltyCache.get(regionPs).put(prev.getRoad().types, prevTs); }