Skip to content

Commit b3de471

Browse files
committed
refactor: use common min/max factors and abstract repetitive code to a method
1 parent 6f9b79a commit b3de471

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/LimitedAccessWeighting.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,20 @@
2727
* @author Andrzej Oles
2828
*/
2929
public class LimitedAccessWeighting extends AbstractAdjustedWeighting {
30+
public static final double MIN_FACTOR = 1;// ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1
31+
public static final double MAX_FACTOR = 10;
3032
public static final double DEFAULT_DESTINATION_FACTOR = 1;
3133
public static final double VEHICLE_DESTINATION_FACTOR = 10;
3234
public static final double DEFAULT_PRIVATE_FACTOR = 1.2;
3335
public static final double VEHICLE_PRIVATE_FACTOR = 10;
3436
public static final double DEFAULT_CUSTOMERS_FACTOR = 1.2;
3537
public static final double VEHICLE_CUSTOMERS_FACTOR = 1.5;
3638

37-
static double MIN_DESTINATION_FACTOR = 1;
38-
static double MAX_DESTINATION_FACTOR = 10;
39-
static double MIN_PRIVATE_FACTOR = 1;
40-
static double MAX_PRIVATE_FACTOR = 10;
41-
static double MIN_CUSTOMERS_FACTOR = 1;
42-
static double MAX_CUSTOMERS_FACTOR = 10;
43-
4439
private final EnumEncodedValue<RoadAccess> roadAccessEnc;
4540
// this factor puts a penalty on roads with a "destination"-only or private access, see GH#733 and GH#1936
4641
private final double destinationPenalty;
4742
private final double privatePenalty;
43+
// this factor puts a penalty on roads with a "customers"-only access, see ORS#1981
4844
private final double customersPenalty;
4945

5046
public LimitedAccessWeighting(Weighting superWeighting, PMap map) {
@@ -53,16 +49,18 @@ public LimitedAccessWeighting(Weighting superWeighting, PMap map) {
5349
if (!encoder.hasEncodedValue(RoadAccess.KEY))
5450
throw new IllegalArgumentException("road_access is not available");
5551

56-
// ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1
57-
double defaultDestinationFactor = encoder.getTransportationMode().isMotorVehicle() ? VEHICLE_DESTINATION_FACTOR : DEFAULT_DESTINATION_FACTOR;
58-
destinationPenalty = checkBounds("road_access_destination_factor", map.getDouble("road_access_destination_factor", defaultDestinationFactor), MIN_DESTINATION_FACTOR, MAX_DESTINATION_FACTOR);
59-
double defaultPrivateFactor = encoder.getTransportationMode().isMotorVehicle() ? VEHICLE_PRIVATE_FACTOR : DEFAULT_PRIVATE_FACTOR;
60-
privatePenalty = checkBounds("road_access_private_factor", map.getDouble("road_access_private_factor", defaultPrivateFactor), MIN_PRIVATE_FACTOR, MAX_PRIVATE_FACTOR);
61-
double defaultCustomersFactor = encoder.getTransportationMode().isMotorVehicle() ? VEHICLE_CUSTOMERS_FACTOR : DEFAULT_CUSTOMERS_FACTOR;
62-
customersPenalty = checkBounds("road_access_customers_factor", map.getDouble("road_access_customers_factor", defaultCustomersFactor), MIN_CUSTOMERS_FACTOR, MAX_CUSTOMERS_FACTOR);
52+
destinationPenalty = getFactorValue(encoder, map, "road_access_destination_factor", DEFAULT_DESTINATION_FACTOR, VEHICLE_DESTINATION_FACTOR);
53+
privatePenalty = getFactorValue(encoder, map, "road_access_private_factor", DEFAULT_PRIVATE_FACTOR, VEHICLE_PRIVATE_FACTOR);
54+
customersPenalty = getFactorValue(encoder, map, "road_access_customers_factor", DEFAULT_CUSTOMERS_FACTOR, VEHICLE_CUSTOMERS_FACTOR);
55+
6356
roadAccessEnc = destinationPenalty > 1 || privatePenalty > 1 || customersPenalty > 1 ? encoder.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class) : null;
6457
}
6558

59+
static double getFactorValue(FlagEncoder encoder, PMap map, String key, double defaultFactor, double vehicleFactor) {
60+
double defaultValue = encoder.getTransportationMode().isMotorVehicle() ? vehicleFactor : defaultFactor;
61+
return checkBounds(key, map.getDouble(key, defaultValue), MIN_FACTOR, MAX_FACTOR);
62+
}
63+
6664
static double checkBounds(String key, double val, double from, double to) {
6765
if (val < from || val > to)
6866
throw new IllegalArgumentException(key + " has invalid range should be within [" + from + ", " + to + "]");

0 commit comments

Comments
 (0)