Skip to content

Commit

Permalink
Use static method instead of many derived profiles calling
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed May 17, 2022
1 parent 0f8b569 commit 35d5507
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 43 deletions.
13 changes: 0 additions & 13 deletions OsmAnd-java/src/main/java/net/osmand/router/GeneralRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,6 @@ public Map<String, RoutingParameter> getParameters() {
return parameters;
}

public Map<String, RoutingParameter> getParameters(String derivedProfile) {
Map<String, RoutingParameter> parameters = new HashMap<>();
for (Entry<String, RoutingParameter> entry : getParameters().entrySet()) {
String[] profiles = entry.getValue().getProfiles();
if (profiles == null || Algorithms.equalsToAny(derivedProfile, (Object[]) profiles)) {
parameters.put(entry.getKey(), entry.getValue());
}
}
return parameters;
}

public void addAttribute(String k, String v) {
attributes.put(k, v);
if (k.equals("restrictionsAware")) {
Expand Down Expand Up @@ -393,8 +382,6 @@ public float defineObstacle(RouteDataObject road, int point, boolean dir) {
return 0;
}

TIntArrayList filteredRules = new TIntArrayList();

@Override
public float defineRoutingObstacle(RouteDataObject road, int point, boolean dir) {
int[] pointTypes = road.getPointTypes(point);
Expand Down
11 changes: 0 additions & 11 deletions OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,6 @@ public static boolean endsWithAny(String s, String ... args) {
return false;
}

public static boolean equalsToAny(Object o, Object ... args) {
if (o != null && args != null && args.length > 0) {
for (Object arg : args) {
if (objectEquals(o, arg)) {
return true;
}
}
}
return false;
}

public static String capitalizeFirstLetterAndLowercase(String s) {
if (s != null && s.length() > 1) {
// not very efficient algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import net.osmand.plus.routing.GPXRouteParams.GPXRouteParamsBuilder;
import net.osmand.plus.routing.RouteService;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
import net.osmand.plus.settings.backend.OsmandSettings;
Expand Down Expand Up @@ -723,8 +724,7 @@ private List<RoutingParameter> getReliefParameters() {
List<RoutingParameter> reliefFactorParameters = new ArrayList<>();
GeneralRouter router = app.getRouter(applicationMode);
if (router != null) {
String derivedProfile = applicationMode.getDerivedProfile();
Map<String, RoutingParameter> parameters = router.getParameters(derivedProfile);
Map<String, RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(applicationMode, router);
for (Map.Entry<String, RoutingParameter> entry : parameters.entrySet()) {
RoutingParameter routingParameter = entry.getValue();
if (RELIEF_SMOOTHNESS_FACTOR.equals(routingParameter.getGroup())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.osmand.plus.routing.GPXRouteParams.GPXRouteParamsBuilder;
import net.osmand.plus.routing.RouteService;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.preferences.CommonPreference;
Expand Down Expand Up @@ -451,8 +452,7 @@ public LocalRoutingParameter getRoutingParameterInnerById(ApplicationMode am, St
}

LocalRoutingParameter rp;
String derivedProfile = am.getDerivedProfile();
Map<String, GeneralRouter.RoutingParameter> parameters = rm.getParameters(derivedProfile);
Map<String, GeneralRouter.RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(am, rm);
GeneralRouter.RoutingParameter routingParameter = parameters.get(parameterId);

if (routingParameter != null) {
Expand Down Expand Up @@ -532,8 +532,7 @@ public List<LocalRoutingParameter> getRoutingParametersInner(ApplicationMode am)
if (rm == null || (rparams != null && !rparams.isCalculateOsmAndRoute()) && !rparams.getFile().hasRtePt()) {
return list;
}
String derivedProfile = am.getDerivedProfile();
Map<String, GeneralRouter.RoutingParameter> parameters = rm.getParameters(derivedProfile);
Map<String, GeneralRouter.RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(am, rm);
for (GeneralRouter.RoutingParameter r : parameters.values()) {
if (r.getType() == GeneralRouter.RoutingParameterType.BOOLEAN) {
if ("relief_smoothness_factor".equals(r.getGroup())) {
Expand Down Expand Up @@ -623,8 +622,7 @@ public List<GeneralRouter.RoutingParameter> getAvoidRoutingPrefsForAppMode(Appli
List<GeneralRouter.RoutingParameter> avoidParameters = new ArrayList<GeneralRouter.RoutingParameter>();
GeneralRouter router = app.getRouter(applicationMode);
if (router != null) {
String derivedProfile = applicationMode.getDerivedProfile();
Map<String, GeneralRouter.RoutingParameter> parameters = router.getParameters(derivedProfile);
Map<String, GeneralRouter.RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(applicationMode, router);
for (Map.Entry<String, GeneralRouter.RoutingParameter> e : parameters.entrySet()) {
String param = e.getKey();
GeneralRouter.RoutingParameter routingParameter = e.getValue();
Expand All @@ -641,8 +639,7 @@ public GeneralRouter.RoutingParameter getRoutingPrefsForAppModeById(ApplicationM
GeneralRouter.RoutingParameter parameter = null;

if (router != null) {
String derivedProfile = applicationMode.getDerivedProfile();
parameter = router.getParameters(derivedProfile).get(parameterId);
parameter = RoutingHelperUtils.getParameterForDerivedProfile(parameterId, applicationMode, router);
}

return parameter;
Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/routing/RouteProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,8 @@ protected RouteCalculationResult findVectorMapsRoute(final RouteCalculationParam

private RoutingConfiguration initOsmAndRoutingConfig(Builder config, final RouteCalculationParams params, OsmandSettings settings,
GeneralRouter generalRouter) throws IOException, FileNotFoundException {
String derivedProfile = params.mode.getDerivedProfile();
Map<String, String> paramsR = new LinkedHashMap<String, String>();
for (Map.Entry<String, RoutingParameter> e : generalRouter.getParameters(derivedProfile).entrySet()) {
for (Map.Entry<String, RoutingParameter> e : RoutingHelperUtils.getParametersForDerivedProfile(params.mode, generalRouter).entrySet()) {
String key = e.getKey();
RoutingParameter pr = e.getValue();
String vl;
Expand Down Expand Up @@ -795,6 +794,7 @@ private RoutingConfiguration initOsmAndRoutingConfig(Builder config, final Route
if (maxSpeed > 0) {
paramsR.put(GeneralRouter.MAX_SPEED, String.valueOf(maxSpeed));
}
String derivedProfile = params.mode.getDerivedProfile();
if (!Algorithms.isEmpty(derivedProfile)) {
paramsR.put("profile_"+ derivedProfile, String.valueOf(true));
}
Expand Down
23 changes: 23 additions & 0 deletions OsmAnd/src/net/osmand/plus/routing/RoutingHelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
import net.osmand.data.QuadRect;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.helpers.TargetPointsHelper;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.router.GeneralRouter;
import net.osmand.router.GeneralRouter.RoutingParameter;
import net.osmand.util.MapUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class RoutingHelperUtils {

Expand Down Expand Up @@ -177,4 +184,20 @@ public static void checkAndUpdateStartLocation(@NonNull OsmandApplication app, L
checkAndUpdateStartLocation(app, newStartLocation, force);
}
}

public static RoutingParameter getParameterForDerivedProfile(@NonNull String id, @NonNull ApplicationMode appMode, @NonNull GeneralRouter router) {
return getParametersForDerivedProfile(appMode, router).get(id);
}

public static Map<String, RoutingParameter> getParametersForDerivedProfile(@NonNull ApplicationMode appMode, @NonNull GeneralRouter router) {
String derivedProfile = appMode.getDerivedProfile();
Map<String, RoutingParameter> parameters = new HashMap<>();
for (Entry<String, RoutingParameter> entry : router.getParameters().entrySet()) {
String[] profiles = entry.getValue().getProfiles();
if (profiles == null || Arrays.asList(profiles).contains(derivedProfile)) {
parameters.put(entry.getKey(), entry.getValue());
}
}
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.osmand.plus.settings.backend.preferences.CommonPreference;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.router.GeneralRouter;
import net.osmand.router.GeneralRouter.RoutingParameter;
import net.osmand.router.NativeTransportRoutingResult;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.router.RoutingConfiguration;
Expand Down Expand Up @@ -489,7 +490,8 @@ private List<TransportRouteResult> calculateRouteImpl(TransportRouteCalculationP
String derivedProfile = params.mode.getDerivedProfile();
GeneralRouter router = config.getRouter(params.mode.getRoutingProfile());
OsmandSettings settings = params.ctx.getSettings();
for (Map.Entry<String, GeneralRouter.RoutingParameter> e : router.getParameters(derivedProfile).entrySet()) {
Map<String, RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(params.mode, router);
for (Map.Entry<String, GeneralRouter.RoutingParameter> e : parameters.entrySet()) {
String key = e.getKey();
GeneralRouter.RoutingParameter pr = e.getValue();
String vl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.utils.ColorUtilities;
Expand Down Expand Up @@ -80,8 +81,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GeneralRouter router = app.getRouter(appMode);
String derivedProfile = appMode.getDerivedProfile();
Map<String, RoutingParameter> routingParameterMap = router.getParameters(derivedProfile);
Map<String, RoutingParameter> routingParameterMap = RoutingHelperUtils.getParametersForDerivedProfile(appMode, router);
RoutingParameter parameter = routingParameterMap.get(USE_HEIGHT_OBSTACLES);
if (parameter != null) {
useHeightPref = app.getSettings().getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.osmand.plus.plugins.development.OsmandDevelopmentPlugin;
import net.osmand.plus.routing.RouteService;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.preferences.BooleanPreference;
Expand Down Expand Up @@ -210,8 +211,7 @@ private void setupRoutingPrefs() {
GeneralRouter router = app.getRouter(am);
clearParameters();
if (router != null) {
String derivedProfile = am.getDerivedProfile();
Map<String, RoutingParameter> parameters = router.getParameters(derivedProfile);
Map<String, RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(am, router);
if (!am.isDerivedRoutingFrom(ApplicationMode.CAR)) {
screen.addPreference(fastRoute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.android.material.slider.Slider;

import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.OsmAndFormatter;
import net.osmand.plus.R;
Expand Down Expand Up @@ -63,9 +64,8 @@ protected void setupPreferences() {
if (routeService == RouteService.OSMAND) {
GeneralRouter router = app.getRouter(mode);
if (router != null) {
String derivedProfile = mode.getDerivedProfile();
GeneralRouterProfile routerProfile = router.getProfile();
Map<String, RoutingParameter> parameters = router.getParameters(derivedProfile);
Map<String, RoutingParameter> parameters = RoutingHelperUtils.getParametersForDerivedProfile(mode, router);
setupCustomRoutingPropertyPref(parameters.get(VEHICLE_HEIGHT), routerProfile);
setupCustomRoutingPropertyPref(parameters.get(VEHICLE_WEIGHT), routerProfile);
setupCustomRoutingPropertyPref(parameters.get(VEHICLE_WIDTH), routerProfile);
Expand Down

0 comments on commit 35d5507

Please sign in to comment.