Refactoring: remove enum EngineType, use OnlineRoutingEngine constants instead

This commit is contained in:
nazar-kutz 2021-02-22 19:09:30 +02:00
parent 1be6bc7eea
commit cea1fa60e7
9 changed files with 133 additions and 87 deletions

View file

@ -1,39 +0,0 @@
package net.osmand.plus.onlinerouting;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.plus.onlinerouting.engine.EngineType;
import net.osmand.plus.onlinerouting.engine.GpxEngine;
import net.osmand.plus.onlinerouting.engine.GraphhopperEngine;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine;
import net.osmand.plus.onlinerouting.engine.OrsEngine;
import net.osmand.plus.onlinerouting.engine.OsrmEngine;
import java.util.Map;
public class OnlineRoutingFactory {
public static OnlineRoutingEngine createEngine(@NonNull EngineType type) {
return createEngine(type, null);
}
@NonNull
public static OnlineRoutingEngine createEngine(@NonNull EngineType type,
@Nullable Map<String, String> params) {
switch (type) {
case GRAPHHOPPER:
return new GraphhopperEngine(params);
case OSRM:
return new OsrmEngine(params);
case ORS:
return new OrsEngine(params);
case GPX:
return new GpxEngine(params);
default:
throw new IllegalArgumentException(
"Online routing type {" + type.name() + "} not supported");
}
}
}

View file

@ -7,8 +7,8 @@ import androidx.annotation.NonNull;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import net.osmand.plus.onlinerouting.engine.EngineType;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine; import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine;
import net.osmand.plus.onlinerouting.engine.EngineType;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.json.JSONArray; import org.json.JSONArray;
@ -60,10 +60,10 @@ public class OnlineRoutingUtils {
for (int i = 0; i < itemsJson.length(); i++) { for (int i = 0; i < itemsJson.length(); i++) {
JSONObject object = itemsJson.getJSONObject(i); JSONObject object = itemsJson.getJSONObject(i);
if (object.has(TYPE) && object.has(PARAMS)) { if (object.has(TYPE) && object.has(PARAMS)) {
EngineType type = EngineType.getTypeByName(object.getString(TYPE)); OnlineRoutingEngine type = EngineType.getTypeByName(object.getString(TYPE));
String paramsString = object.getString(PARAMS); String paramsString = object.getString(PARAMS);
HashMap<String, String> params = gson.fromJson(paramsString, typeToken); HashMap<String, String> params = gson.fromJson(paramsString, typeToken);
OnlineRoutingEngine engine = OnlineRoutingFactory.createEngine(type, params); OnlineRoutingEngine engine = type.newInstance(params);
if (!Algorithms.isEmpty(engine.getStringKey())) { if (!Algorithms.isEmpty(engine.getStringKey())) {
engines.add(engine); engines.add(engine);
} }
@ -82,7 +82,7 @@ public class OnlineRoutingUtils {
continue; continue;
} }
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put(TYPE, engine.getType().name()); jsonObject.put(TYPE, engine.getTypeName());
jsonObject.put(PARAMS, gson.toJson(engine.getParams(), type)); jsonObject.put(PARAMS, gson.toJson(engine.getParams(), type));
jsonArray.put(jsonObject); jsonArray.put(jsonObject);
} }

View file

@ -1,35 +1,38 @@
package net.osmand.plus.onlinerouting.engine; package net.osmand.plus.onlinerouting.engine;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
public enum EngineType { public class EngineType {
GRAPHHOPPER("Graphhopper"),
OSRM("OSRM"),
ORS("Openroute Service"),
GPX("GPX");
private final String title; public final static OnlineRoutingEngine GRAPHHOPPER_TYPE = new GraphhopperEngine(null);
public final static OnlineRoutingEngine OSRM_TYPE = new OsrmEngine(null);
public final static OnlineRoutingEngine ORS_TYPE = new OrsEngine(null);
public final static OnlineRoutingEngine GPX_TYPE = new GpxEngine(null);
EngineType(String title) { private static OnlineRoutingEngine[] enginesTypes;
this.title = title;
}
public String getTitle() { public static OnlineRoutingEngine[] values() {
return title; if (enginesTypes == null) {
enginesTypes = new OnlineRoutingEngine[]{
GRAPHHOPPER_TYPE,
OSRM_TYPE,
ORS_TYPE,
GPX_TYPE
};
}
return enginesTypes;
} }
@NonNull @NonNull
public static EngineType getTypeByName(@Nullable String name) { public static OnlineRoutingEngine getTypeByName(@NonNull String typeName) {
if (!Algorithms.isEmpty(name)) { for (OnlineRoutingEngine type : values()) {
for (EngineType type : values()) { if (Algorithms.objectEquals(type.getTypeName(), typeName)) {
if (type.name().equals(name)) { return type;
return type;
}
} }
} }
return values()[0]; return values()[0];
} }
} }

View file

@ -17,6 +17,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.GPX_TYPE;
public class GpxEngine extends OnlineRoutingEngine { public class GpxEngine extends OnlineRoutingEngine {
public GpxEngine(@Nullable Map<String, String> params) { public GpxEngine(@Nullable Map<String, String> params) {
@ -25,8 +27,20 @@ public class GpxEngine extends OnlineRoutingEngine {
@NonNull @NonNull
@Override @Override
public EngineType getType() { public OnlineRoutingEngine getType() {
return EngineType.GPX; return GPX_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "GPX";
}
@NonNull
@Override
public String getTypeName() {
return "GPX";
} }
@Override @Override
@ -60,6 +74,11 @@ public class GpxEngine extends OnlineRoutingEngine {
params.add(EngineParameter.CUSTOM_URL); params.add(EngineParameter.CUSTOM_URL);
} }
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new GpxEngine(params);
}
@Override @Override
@Nullable @Nullable
public OnlineRoutingResponse parseResponse(@NonNull String content, public OnlineRoutingResponse parseResponse(@NonNull String content,

View file

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.GRAPHHOPPER_TYPE;
import static net.osmand.util.Algorithms.isEmpty; import static net.osmand.util.Algorithms.isEmpty;
public class GraphhopperEngine extends JsonOnlineRoutingEngine { public class GraphhopperEngine extends JsonOnlineRoutingEngine {
@ -32,8 +33,20 @@ public class GraphhopperEngine extends JsonOnlineRoutingEngine {
@NonNull @NonNull
@Override @Override
public EngineType getType() { public OnlineRoutingEngine getType() {
return EngineType.GRAPHHOPPER; return GRAPHHOPPER_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "Graphhopper";
}
@NonNull
@Override
public String getTypeName() {
return "GRAPHHOPPER";
} }
@NonNull @NonNull
@ -52,6 +65,11 @@ public class GraphhopperEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.API_KEY); params.add(EngineParameter.API_KEY);
} }
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new GraphhopperEngine(params);
}
@Override @Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) { protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
vehicles.add(new VehicleType("car", R.string.routing_engine_vehicle_type_car)); vehicles.add(new VehicleType("car", R.string.routing_engine_vehicle_type_car));

View file

@ -12,7 +12,6 @@ import net.osmand.data.LatLon;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.onlinerouting.EngineParameter; import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.OnlineRoutingFactory;
import net.osmand.plus.onlinerouting.VehicleType; import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.routing.RouteDirectionInfo; import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -50,7 +49,13 @@ public abstract class OnlineRoutingEngine implements Cloneable {
} }
@NonNull @NonNull
public abstract EngineType getType(); public abstract OnlineRoutingEngine getType();
@NonNull
public abstract String getTitle();
@NonNull
public abstract String getTypeName();
@Nullable @Nullable
public String getStringKey() { public String getStringKey() {
@ -176,7 +181,7 @@ public abstract class OnlineRoutingEngine implements Cloneable {
@NonNull @NonNull
@Override @Override
public Object clone() { public Object clone() {
return OnlineRoutingFactory.createEngine(getType(), getParams()); return newInstance(getParams());
} }
@Override @Override
@ -189,6 +194,8 @@ public abstract class OnlineRoutingEngine implements Cloneable {
return Algorithms.objectEquals(getParams(), engine.getParams()); return Algorithms.objectEquals(getParams(), engine.getParams());
} }
public abstract OnlineRoutingEngine newInstance(Map<String, String> params);
@NonNull @NonNull
public static String generateKey() { public static String generateKey() {
return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis(); return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis();

View file

@ -19,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.ORS_TYPE;
import static net.osmand.util.Algorithms.isEmpty; import static net.osmand.util.Algorithms.isEmpty;
public class OrsEngine extends JsonOnlineRoutingEngine { public class OrsEngine extends JsonOnlineRoutingEngine {
@ -29,8 +30,20 @@ public class OrsEngine extends JsonOnlineRoutingEngine {
@NonNull @NonNull
@Override @Override
public EngineType getType() { public OnlineRoutingEngine getType() {
return EngineType.ORS; return ORS_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "Openroute Service";
}
@NonNull
@Override
public String getTypeName() {
return "ORS";
} }
@NonNull @NonNull
@ -49,6 +62,11 @@ public class OrsEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.API_KEY); params.add(EngineParameter.API_KEY);
} }
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new OrsEngine(params);
}
@Override @Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) { protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
vehicles.add(new VehicleType("driving-car", R.string.routing_engine_vehicle_type_car)); vehicles.add(new VehicleType("driving-car", R.string.routing_engine_vehicle_type_car));

View file

@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.OSRM_TYPE;
import static net.osmand.util.Algorithms.isEmpty; import static net.osmand.util.Algorithms.isEmpty;
import static net.osmand.util.Algorithms.objectEquals; import static net.osmand.util.Algorithms.objectEquals;
@ -34,9 +35,21 @@ public class OsrmEngine extends JsonOnlineRoutingEngine {
} }
@Override @Override
public @NonNull @NonNull
EngineType getType() { public OnlineRoutingEngine getType() {
return EngineType.OSRM; return OSRM_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "OSRM";
}
@NonNull
@Override
public String getTypeName() {
return "OSRM";
} }
@NonNull @NonNull
@ -54,6 +67,11 @@ public class OsrmEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.CUSTOM_URL); params.add(EngineParameter.CUSTOM_URL);
} }
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new OsrmEngine(params);
}
@Override @Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) { protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
vehicles.add(new VehicleType("car", R.string.routing_engine_vehicle_type_car)); vehicles.add(new VehicleType("car", R.string.routing_engine_vehicle_type_car));

View file

@ -39,12 +39,11 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem;
import net.osmand.plus.onlinerouting.EngineParameter; import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.OnlineRoutingFactory;
import net.osmand.plus.onlinerouting.OnlineRoutingHelper; import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
import net.osmand.plus.onlinerouting.OnlineRoutingUtils; import net.osmand.plus.onlinerouting.OnlineRoutingUtils;
import net.osmand.plus.onlinerouting.VehicleType; import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.onlinerouting.engine.EngineType;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine; import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine;
import net.osmand.plus.onlinerouting.engine.EngineType;
import net.osmand.plus.onlinerouting.ui.OnlineRoutingCard.OnTextChangedListener; import net.osmand.plus.onlinerouting.ui.OnlineRoutingCard.OnTextChangedListener;
import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
@ -54,7 +53,9 @@ import org.json.JSONException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.CUSTOM_VEHICLE; import static net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.CUSTOM_VEHICLE;
@ -201,15 +202,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
typeCard = new OnlineRoutingCard(mapActivity, isNightMode(), appMode); typeCard = new OnlineRoutingCard(mapActivity, isNightMode(), appMode);
typeCard.build(mapActivity); typeCard.build(mapActivity);
typeCard.setHeaderTitle(getString(R.string.shared_string_type)); typeCard.setHeaderTitle(getString(R.string.shared_string_type));
List<HorizontalSelectionItem> serverItems = new ArrayList<>(); List<HorizontalSelectionItem> typeItems = new ArrayList<>();
for (EngineType server : EngineType.values()) { for (OnlineRoutingEngine type : EngineType.values()) {
serverItems.add(new HorizontalSelectionItem(server.getTitle(), server)); typeItems.add(new HorizontalSelectionItem(type.getTitle(), type));
} }
typeCard.setSelectionMenu(serverItems, engine.getType().getTitle(), typeCard.setSelectionMenu(typeItems, engine.getType().getTitle(),
new CallbackWithObject<HorizontalSelectionItem>() { new CallbackWithObject<HorizontalSelectionItem>() {
@Override @Override
public boolean processResult(HorizontalSelectionItem result) { public boolean processResult(HorizontalSelectionItem result) {
EngineType type = (EngineType) result.getObject(); OnlineRoutingEngine type = (OnlineRoutingEngine) result.getObject();
if (engine.getType() != type) { if (engine.getType() != type) {
changeEngineType(type); changeEngineType(type);
return true; return true;
@ -366,9 +367,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
}); });
} }
private void changeEngineType(EngineType type) { private void changeEngineType(OnlineRoutingEngine type) {
OnlineRoutingEngine tmp = (OnlineRoutingEngine) engine.clone(); OnlineRoutingEngine tmp = (OnlineRoutingEngine) engine.clone();
engine = OnlineRoutingFactory.createEngine(type, tmp.getParams()); engine = type.newInstance(tmp.getParams());
// after changing the type, select the vehicle // after changing the type, select the vehicle
// with the same name that was selected before // with the same name that was selected before
@ -615,7 +616,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
} }
private void saveState(@NonNull Bundle outState) { private void saveState(@NonNull Bundle outState) {
outState.putString(ENGINE_TYPE_KEY, engine.getType().name()); outState.putString(ENGINE_TYPE_KEY, engine.getTypeName());
for (EngineParameter key : EngineParameter.values()) { for (EngineParameter key : EngineParameter.values()) {
String value = engine.get(key); String value = engine.get(key);
if (value != null) { if (value != null) {
@ -632,14 +633,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
editedEngineKey = savedState.getString(EngineParameter.KEY.name()); editedEngineKey = savedState.getString(EngineParameter.KEY.name());
initEngine = createInitStateEngine(); initEngine = createInitStateEngine();
String typeKey = savedState.getString(ENGINE_TYPE_KEY); String typeKey = savedState.getString(ENGINE_TYPE_KEY);
EngineType type = EngineType.getTypeByName(typeKey); OnlineRoutingEngine type = EngineType.getTypeByName(typeKey);
engine = OnlineRoutingFactory.createEngine(type); Map<String, String> params = new HashMap<>();
for (EngineParameter key : EngineParameter.values()) { for (EngineParameter key : EngineParameter.values()) {
String value = savedState.getString(key.name()); String value = savedState.getString(key.name());
if (value != null) { if (value != null) {
engine.put(key, value); params.put(key.name(), value);
} }
} }
engine = type.newInstance(params);
customVehicleKey = savedState.getString(ENGINE_CUSTOM_VEHICLE_KEY); customVehicleKey = savedState.getString(ENGINE_CUSTOM_VEHICLE_KEY);
selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY));
appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null); appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null);
@ -662,7 +664,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
if (editedEngine != null) { if (editedEngine != null) {
engine = (OnlineRoutingEngine) editedEngine.clone(); engine = (OnlineRoutingEngine) editedEngine.clone();
} else { } else {
engine = OnlineRoutingFactory.createEngine(EngineType.values()[0]); engine = EngineType.values()[0].newInstance(null);
String vehicle = engine.getAllowedVehicles().get(0).getKey(); String vehicle = engine.getAllowedVehicles().get(0).getKey();
engine.put(EngineParameter.VEHICLE_KEY, vehicle); engine.put(EngineParameter.VEHICLE_KEY, vehicle);
if (editedEngineKey != null) { if (editedEngineKey != null) {