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

View file

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

View file

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

View file

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.GRAPHHOPPER_TYPE;
import static net.osmand.util.Algorithms.isEmpty;
public class GraphhopperEngine extends JsonOnlineRoutingEngine {
@ -32,8 +33,20 @@ public class GraphhopperEngine extends JsonOnlineRoutingEngine {
@NonNull
@Override
public EngineType getType() {
return EngineType.GRAPHHOPPER;
public OnlineRoutingEngine getType() {
return GRAPHHOPPER_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "Graphhopper";
}
@NonNull
@Override
public String getTypeName() {
return "GRAPHHOPPER";
}
@NonNull
@ -52,6 +65,11 @@ public class GraphhopperEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.API_KEY);
}
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new GraphhopperEngine(params);
}
@Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
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.R;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.OnlineRoutingFactory;
import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.util.Algorithms;
@ -50,7 +49,13 @@ public abstract class OnlineRoutingEngine implements Cloneable {
}
@NonNull
public abstract EngineType getType();
public abstract OnlineRoutingEngine getType();
@NonNull
public abstract String getTitle();
@NonNull
public abstract String getTypeName();
@Nullable
public String getStringKey() {
@ -176,7 +181,7 @@ public abstract class OnlineRoutingEngine implements Cloneable {
@NonNull
@Override
public Object clone() {
return OnlineRoutingFactory.createEngine(getType(), getParams());
return newInstance(getParams());
}
@Override
@ -189,6 +194,8 @@ public abstract class OnlineRoutingEngine implements Cloneable {
return Algorithms.objectEquals(getParams(), engine.getParams());
}
public abstract OnlineRoutingEngine newInstance(Map<String, String> params);
@NonNull
public static String generateKey() {
return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis();

View file

@ -19,6 +19,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.ORS_TYPE;
import static net.osmand.util.Algorithms.isEmpty;
public class OrsEngine extends JsonOnlineRoutingEngine {
@ -29,8 +30,20 @@ public class OrsEngine extends JsonOnlineRoutingEngine {
@NonNull
@Override
public EngineType getType() {
return EngineType.ORS;
public OnlineRoutingEngine getType() {
return ORS_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "Openroute Service";
}
@NonNull
@Override
public String getTypeName() {
return "ORS";
}
@NonNull
@ -49,6 +62,11 @@ public class OrsEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.API_KEY);
}
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new OrsEngine(params);
}
@Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
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.Set;
import static net.osmand.plus.onlinerouting.engine.EngineType.OSRM_TYPE;
import static net.osmand.util.Algorithms.isEmpty;
import static net.osmand.util.Algorithms.objectEquals;
@ -34,9 +35,21 @@ public class OsrmEngine extends JsonOnlineRoutingEngine {
}
@Override
public @NonNull
EngineType getType() {
return EngineType.OSRM;
@NonNull
public OnlineRoutingEngine getType() {
return OSRM_TYPE;
}
@Override
@NonNull
public String getTitle() {
return "OSRM";
}
@NonNull
@Override
public String getTypeName() {
return "OSRM";
}
@NonNull
@ -54,6 +67,11 @@ public class OsrmEngine extends JsonOnlineRoutingEngine {
params.add(EngineParameter.CUSTOM_URL);
}
@Override
public OnlineRoutingEngine newInstance(Map<String, String> params) {
return new OsrmEngine(params);
}
@Override
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
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.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.OnlineRoutingFactory;
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
import net.osmand.plus.onlinerouting.OnlineRoutingUtils;
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.EngineType;
import net.osmand.plus.onlinerouting.ui.OnlineRoutingCard.OnTextChangedListener;
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.settings.backend.ApplicationMode;
@ -54,7 +53,9 @@ import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.build(mapActivity);
typeCard.setHeaderTitle(getString(R.string.shared_string_type));
List<HorizontalSelectionItem> serverItems = new ArrayList<>();
for (EngineType server : EngineType.values()) {
serverItems.add(new HorizontalSelectionItem(server.getTitle(), server));
List<HorizontalSelectionItem> typeItems = new ArrayList<>();
for (OnlineRoutingEngine type : EngineType.values()) {
typeItems.add(new HorizontalSelectionItem(type.getTitle(), type));
}
typeCard.setSelectionMenu(serverItems, engine.getType().getTitle(),
typeCard.setSelectionMenu(typeItems, engine.getType().getTitle(),
new CallbackWithObject<HorizontalSelectionItem>() {
@Override
public boolean processResult(HorizontalSelectionItem result) {
EngineType type = (EngineType) result.getObject();
OnlineRoutingEngine type = (OnlineRoutingEngine) result.getObject();
if (engine.getType() != type) {
changeEngineType(type);
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();
engine = OnlineRoutingFactory.createEngine(type, tmp.getParams());
engine = type.newInstance(tmp.getParams());
// after changing the type, select the vehicle
// with the same name that was selected before
@ -615,7 +616,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
}
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()) {
String value = engine.get(key);
if (value != null) {
@ -632,14 +633,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
editedEngineKey = savedState.getString(EngineParameter.KEY.name());
initEngine = createInitStateEngine();
String typeKey = savedState.getString(ENGINE_TYPE_KEY);
EngineType type = EngineType.getTypeByName(typeKey);
engine = OnlineRoutingFactory.createEngine(type);
OnlineRoutingEngine type = EngineType.getTypeByName(typeKey);
Map<String, String> params = new HashMap<>();
for (EngineParameter key : EngineParameter.values()) {
String value = savedState.getString(key.name());
if (value != null) {
engine.put(key, value);
params.put(key.name(), value);
}
}
engine = type.newInstance(params);
customVehicleKey = savedState.getString(ENGINE_CUSTOM_VEHICLE_KEY);
selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY));
appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null);
@ -662,7 +664,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
if (editedEngine != null) {
engine = (OnlineRoutingEngine) editedEngine.clone();
} else {
engine = OnlineRoutingFactory.createEngine(EngineType.values()[0]);
engine = EngineType.values()[0].newInstance(null);
String vehicle = engine.getAllowedVehicles().get(0).getKey();
engine.put(EngineParameter.VEHICLE_KEY, vehicle);
if (editedEngineKey != null) {