Refactoring: inheritance instead of composition

This commit is contained in:
nazar-kutz 2021-02-22 17:04:38 +02:00
parent fab1df33a6
commit 1be6bc7eea
11 changed files with 400 additions and 492 deletions

View file

@ -9,8 +9,7 @@ import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.Version;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine;
import net.osmand.plus.onlinerouting.parser.ResponseParser;
import net.osmand.plus.onlinerouting.parser.ResponseParser.OnlineRoutingResponse;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.OnlineRoutingResponse;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms;
@ -95,8 +94,7 @@ public class OnlineRoutingHelper {
boolean leftSideNavigation) throws IOException, JSONException {
String url = engine.getFullUrl(path);
String content = makeRequest(url);
ResponseParser parser = engine.getResponseParser();
return parser.parseResponse(content, app, leftSideNavigation);
return engine.parseResponse(content, app, leftSideNavigation);
}
@NonNull

View file

@ -3,12 +3,16 @@ package net.osmand.plus.onlinerouting.engine;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.data.LatLon;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.onlinerouting.parser.GpxParser;
import net.osmand.plus.onlinerouting.parser.ResponseParser;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -56,9 +60,29 @@ public class GpxEngine extends OnlineRoutingEngine {
params.add(EngineParameter.CUSTOM_URL);
}
@NonNull
@Override
protected ResponseParser createParser() {
return new GpxParser();
@Nullable
public OnlineRoutingResponse parseResponse(@NonNull String content,
@NonNull OsmandApplication app,
boolean leftSideNavigation) {
GPXFile gpxFile = parseGpx(content);
return gpxFile != null ? new OnlineRoutingResponse(parseGpx(content)) : null;
}
@Override
public boolean isResultOk(@NonNull StringBuilder errorMessage,
@NonNull String content) {
return parseGpx(content) != null;
}
private GPXFile parseGpx(@NonNull String content) {
InputStream gpxStream = null;
try {
gpxStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
return GPXUtilities.loadGPXFile(gpxStream);
} catch (UnsupportedEncodingException e) {
LOG.debug("Error when parsing GPX from server response: " + e.getMessage());
}
return null;
}
}

View file

@ -9,8 +9,6 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.onlinerouting.parser.JSONParser;
import net.osmand.plus.onlinerouting.parser.ResponseParser;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.router.TurnType;
import net.osmand.util.GeoPolylineParserUtil;
@ -26,7 +24,7 @@ import java.util.Set;
import static net.osmand.util.Algorithms.isEmpty;
public class GraphhopperEngine extends OnlineRoutingEngine {
public class GraphhopperEngine extends JsonOnlineRoutingEngine {
public GraphhopperEngine(@Nullable Map<String, String> params) {
super(params);
@ -67,12 +65,6 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
vehicles.add(new VehicleType("small_truck", R.string.routing_engine_vehicle_type_small_truck));
}
@NonNull
@Override
protected ResponseParser createParser() {
return new GraphhopperParser();
}
@Override
protected void makeFullUrl(@NonNull StringBuilder sb,
@NonNull List<LatLon> path) {
@ -95,8 +87,6 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
sb.append('&').append("details=").append("lanes");
}
private static class GraphhopperParser extends JSONParser {
@Nullable
protected OnlineRoutingResponse parseServerResponse(@NonNull JSONObject root,
@NonNull OsmandApplication app,
@ -238,5 +228,5 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
protected String getRootArrayKey() {
return "paths";
}
}
}

View file

@ -1,4 +1,4 @@
package net.osmand.plus.onlinerouting.parser;
package net.osmand.plus.onlinerouting.engine;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -15,10 +15,15 @@ import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static net.osmand.util.Algorithms.isEmpty;
public abstract class JSONParser extends ResponseParser {
public abstract class JsonOnlineRoutingEngine extends OnlineRoutingEngine {
public JsonOnlineRoutingEngine(@Nullable Map<String, String> params) {
super(params);
}
@Nullable
public OnlineRoutingResponse parseResponse(@NonNull String content,

View file

@ -5,16 +5,20 @@ import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.Location;
import net.osmand.PlatformUtil;
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.onlinerouting.parser.ResponseParser;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Collections;
@ -36,7 +40,6 @@ public abstract class OnlineRoutingEngine implements Cloneable {
private final Map<String, String> params = new HashMap<>();
private final List<VehicleType> allowedVehicles = new ArrayList<>();
private final Set<EngineParameter> allowedParameters = new HashSet<>();
private ResponseParser responseParser;
public OnlineRoutingEngine(@Nullable Map<String, String> params) {
if (!isEmpty(params)) {
@ -97,16 +100,13 @@ public abstract class OnlineRoutingEngine implements Cloneable {
@NonNull
public abstract String getStandardUrl();
@NonNull
public ResponseParser getResponseParser() {
if (responseParser == null) {
responseParser = createParser();
}
return responseParser;
}
@Nullable
public abstract OnlineRoutingResponse parseResponse(@NonNull String content,
@NonNull OsmandApplication app,
boolean leftSideNavigation) throws JSONException;
@NonNull
protected abstract ResponseParser createParser();
public abstract boolean isResultOk(@NonNull StringBuilder errorMessage,
@NonNull String content) throws JSONException;
@NonNull
public Map<String, String> getParams() {
@ -194,4 +194,34 @@ public abstract class OnlineRoutingEngine implements Cloneable {
return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis();
}
public static class OnlineRoutingResponse {
private List<Location> route;
private List<RouteDirectionInfo> directions;
private GPXFile gpxFile;
// constructor for JSON responses
public OnlineRoutingResponse(List<Location> route, List<RouteDirectionInfo> directions) {
this.route = route;
this.directions = directions;
}
// constructor for GPX responses
public OnlineRoutingResponse(GPXFile gpxFile) {
this.gpxFile = gpxFile;
}
public List<Location> getRoute() {
return route;
}
public List<RouteDirectionInfo> getDirections() {
return directions;
}
public GPXFile getGpxFile() {
return gpxFile;
}
}
}

View file

@ -9,8 +9,6 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.onlinerouting.parser.JSONParser;
import net.osmand.plus.onlinerouting.parser.ResponseParser;
import org.json.JSONArray;
import org.json.JSONException;
@ -23,7 +21,7 @@ import java.util.Set;
import static net.osmand.util.Algorithms.isEmpty;
public class OrsEngine extends OnlineRoutingEngine {
public class OrsEngine extends JsonOnlineRoutingEngine {
public OrsEngine(@Nullable Map<String, String> params) {
super(params);
@ -64,12 +62,6 @@ public class OrsEngine extends OnlineRoutingEngine {
vehicles.add(new VehicleType("wheelchair", R.string.routing_engine_vehicle_type_wheelchair));
}
@NonNull
@Override
protected ResponseParser createParser() {
return new OrsParser();
}
@Override
protected void makeFullUrl(@NonNull StringBuilder sb,
@NonNull List<LatLon> path) {
@ -92,8 +84,6 @@ public class OrsEngine extends OnlineRoutingEngine {
}
}
private static class OrsParser extends JSONParser {
@Nullable
@Override
public OnlineRoutingResponse parseServerResponse(@NonNull JSONObject root,
@ -125,5 +115,5 @@ public class OrsEngine extends OnlineRoutingEngine {
protected String getRootArrayKey() {
return "features";
}
}
}

View file

@ -9,8 +9,6 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.onlinerouting.EngineParameter;
import net.osmand.plus.onlinerouting.VehicleType;
import net.osmand.plus.onlinerouting.parser.JSONParser;
import net.osmand.plus.onlinerouting.parser.ResponseParser;
import net.osmand.plus.routing.RouteCalculationResult;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.router.TurnType;
@ -29,7 +27,7 @@ import java.util.Set;
import static net.osmand.util.Algorithms.isEmpty;
import static net.osmand.util.Algorithms.objectEquals;
public class OsrmEngine extends OnlineRoutingEngine {
public class OsrmEngine extends JsonOnlineRoutingEngine {
public OsrmEngine(@Nullable Map<String, String> params) {
super(params);
@ -63,12 +61,6 @@ public class OsrmEngine extends OnlineRoutingEngine {
vehicles.add(new VehicleType("foot", R.string.routing_engine_vehicle_type_foot));
}
@NonNull
@Override
protected ResponseParser createParser() {
return new OsrmParser();
}
@Override
protected void makeFullUrl(@NonNull StringBuilder sb,
@NonNull List<LatLon> path) {
@ -88,8 +80,6 @@ public class OsrmEngine extends OnlineRoutingEngine {
sb.append('&').append("steps=true");
}
private static class OsrmParser extends JSONParser {
@Nullable
@Override
protected OnlineRoutingResponse parseServerResponse(@NonNull JSONObject root,
@ -250,5 +240,5 @@ public class OsrmEngine extends OnlineRoutingEngine {
protected String getErrorMessageKey() {
return "message";
}
}
}

View file

@ -1,42 +0,0 @@
package net.osmand.plus.onlinerouting.parser;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.OsmandApplication;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class GpxParser extends ResponseParser {
@Override
@Nullable
public OnlineRoutingResponse parseResponse(@NonNull String content,
@NonNull OsmandApplication app,
boolean leftSideNavigation) {
GPXFile gpxFile = parseGpx(content);
return gpxFile != null ? new OnlineRoutingResponse(parseGpx(content)) : null;
}
@Override
public boolean isResultOk(@NonNull StringBuilder errorMessage,
@NonNull String content) {
return parseGpx(content) != null;
}
private GPXFile parseGpx(@NonNull String content) {
InputStream gpxStream = null;
try {
gpxStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
return GPXUtilities.loadGPXFile(gpxStream);
} catch (UnsupportedEncodingException e) {
LOG.debug("Error when parsing GPX from server response: " + e.getMessage());
}
return null;
}
}

View file

@ -1,77 +0,0 @@
package net.osmand.plus.onlinerouting.parser;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.routing.RouteDirectionInfo;
import org.apache.commons.logging.Log;
import org.json.JSONException;
import java.util.List;
public abstract class ResponseParser {
protected static final Log LOG = PlatformUtil.getLog(ResponseParser.class);
@Nullable
public abstract OnlineRoutingResponse parseResponse(@NonNull String content,
@NonNull OsmandApplication app,
boolean leftSideNavigation) throws JSONException;
public abstract boolean isResultOk(@NonNull StringBuilder errorMessage,
@NonNull String content) throws JSONException;
public static ResponseParser emptyParser() {
return new ResponseParser() {
@Nullable
@Override
public OnlineRoutingResponse parseResponse(@NonNull String content,
@NonNull OsmandApplication app,
boolean leftSideNavigation) {
return null;
}
@Override
public boolean isResultOk(@NonNull StringBuilder errorMessage,
@NonNull String content) {
return false;
}
};
}
public static class OnlineRoutingResponse {
private List<Location> route;
private List<RouteDirectionInfo> directions;
private GPXFile gpxFile;
// constructor for JSON responses
public OnlineRoutingResponse(List<Location> route, List<RouteDirectionInfo> directions) {
this.route = route;
this.directions = directions;
}
// constructor for GPX responses
public OnlineRoutingResponse(GPXFile gpxFile) {
this.gpxFile = gpxFile;
}
public List<Location> getRoute() {
return route;
}
public List<RouteDirectionInfo> getDirections() {
return directions;
}
public GPXFile getGpxFile() {
return gpxFile;
}
}
}

View file

@ -462,7 +462,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
boolean resultOk = false;
try {
String response = helper.makeRequest(exampleCard.getEditedText());
resultOk = requestedEngine.getResponseParser().isResultOk(errorMessage, response);
resultOk = requestedEngine.isResultOk(errorMessage, response);
} catch (IOException | JSONException e) {
errorMessage.append(e.toString());
}

View file

@ -23,7 +23,7 @@ import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.TargetPointsHelper.TargetPoint;
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
import net.osmand.plus.onlinerouting.parser.ResponseParser.OnlineRoutingResponse;
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.OnlineRoutingResponse;
import net.osmand.plus.render.NativeOsmandLibrary;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.CommonPreference;