From ba77ddf6f67254f74e1111349f9ea309ae758b87 Mon Sep 17 00:00:00 2001 From: evgenij27 Date: Wed, 14 Nov 2018 13:22:20 +0200 Subject: [PATCH] fix sort order for incline boundaries --- .../net/osmand/router/RouteStatistics.java | 119 ++++++------ OsmAnd/res/layout/route_info_header.xml | 181 ++++++++++++++++++ .../ShowRouteInfoDialogFragment.java | 12 +- 3 files changed, 245 insertions(+), 67 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteStatistics.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteStatistics.java index b9ef29a0c1..6435e5a45d 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteStatistics.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteStatistics.java @@ -35,7 +35,7 @@ public class RouteStatistics { } - private abstract static class RouteStatisticComputer { + private abstract static class RouteStatisticComputer> { private final List route; @@ -43,13 +43,13 @@ public class RouteStatistics { this.route = route; } - protected Map makePartition(List routeAttributes) { - Map partition = new TreeMap<>(); - for (RouteSegmentAttribute attribute : routeAttributes) { - String key = attribute.getAttribute(); - RouteSegmentAttribute pattr = partition.get(key); + protected Map> makePartition(List> routeAttributes) { + Map> partition = new TreeMap<>(); + for (RouteSegmentAttribute attribute : routeAttributes) { + E key = attribute.getAttribute(); + RouteSegmentAttribute pattr = partition.get(key); if (pattr == null) { - pattr = new RouteSegmentAttribute(attribute.getIndex(), attribute.getAttribute(), attribute.getColorAttrName()); + pattr = new RouteSegmentAttribute<>(attribute.getIndex(), attribute.getAttribute(), attribute.getColorAttrName()); partition.put(key, pattr); } pattr.incrementDistanceBy(attribute.getDistance()); @@ -57,7 +57,7 @@ public class RouteStatistics { return partition; } - private float computeTotalDistance(List attributes) { + private float computeTotalDistance(List> attributes) { float distance = 0f; for (RouteSegmentAttribute attribute : attributes) { distance += attribute.getDistance(); @@ -69,18 +69,18 @@ public class RouteStatistics { return route; } - protected List processRoute() { + protected List> processRoute() { int index = 0; - List routes = new ArrayList<>(); - String prev = null; + List> routes = new ArrayList<>(); + E prev = null; for (RouteSegmentResult segment : getRoute()) { - String current = getAttribute(segment); + E current = getAttribute(segment); if (prev != null && !prev.equals(current)) { index++; } if (index >= routes.size()) { String colorAttrName = determineColor(current); - routes.add(new RouteSegmentAttribute(index, current, colorAttrName)); + routes.add(new RouteSegmentAttribute<>(index, current, colorAttrName)); } RouteSegmentAttribute surface = routes.get(index); surface.incrementDistanceBy(segment.getDistance()); @@ -89,20 +89,20 @@ public class RouteStatistics { return routes; } - public Statistics computeStatistic() { - List routeAttributes = processRoute(); - Map partition = makePartition(routeAttributes); + public Statistics computeStatistic() { + List> routeAttributes = processRoute(); + Map> partition = makePartition(routeAttributes); float totalDistance = computeTotalDistance(routeAttributes); - return new Statistics(routeAttributes, partition, totalDistance); + return new Statistics<>(routeAttributes, partition, totalDistance); } - public abstract String getAttribute(RouteSegmentResult segment); + public abstract E getAttribute(RouteSegmentResult segment); - public abstract String determineColor(String attribute); + public abstract String determineColor(E attribute); } - private static class RouteSurfaceStatisticComputer extends RouteStatisticComputer { + private static class RouteSurfaceStatisticComputer extends RouteStatisticComputer { public RouteSurfaceStatisticComputer(List route) { super(route); @@ -129,7 +129,7 @@ public class RouteStatistics { } } - private static class RouteSmoothnessStatisticComputer extends RouteStatisticComputer { + private static class RouteSmoothnessStatisticComputer extends RouteStatisticComputer { public RouteSmoothnessStatisticComputer(List route) { super(route); @@ -157,7 +157,7 @@ public class RouteStatistics { } - private static class RouteClassStatisticComputer extends RouteStatisticComputer { + private static class RouteClassStatisticComputer extends RouteStatisticComputer { public RouteClassStatisticComputer(List route) { super(route); @@ -185,7 +185,7 @@ public class RouteStatistics { } - private static class RouteSteepnessStatisticComputer extends RouteStatisticComputer { + private static class RouteSteepnessStatisticComputer extends RouteStatisticComputer { private static final String POSITIVE_INCLINE_COLOR_ATTR_NAME = "greenColor"; private static final String NEGATIVE_INCLINE_COLOR_ATTR_NAME = "redColor"; @@ -198,22 +198,20 @@ public class RouteStatistics { } @Override - public List processRoute() { - List routeInclines = new ArrayList<>(); + public List> processRoute() { + List> routeInclines = new ArrayList<>(); int index = 0; - String prev = null; + Boundaries prev = null; Incline prevIncline = null; for (Incline incline : inclines) { - String current = incline.getBoundariesAsString(); + Boundaries current = incline.getBoundaries(); if (prev != null && !prev.equals(current)) { index++; } if (index >= routeInclines.size()) { - String colorAttrName = determineColor(incline); - RouteSegmentAttribute attribute = new RouteSegmentAttribute(index, current, colorAttrName); - if ( - //index > 0 && - prevIncline != null) { + String colorAttrName = determineColor(current); + RouteSegmentAttribute attribute = new RouteSegmentAttribute<>(index, current, colorAttrName); + if (prevIncline != null) { attribute.setInitDistance(prevIncline.getDistance()); } routeInclines.add(attribute); @@ -227,32 +225,30 @@ public class RouteStatistics { } @Override - public String getAttribute(RouteSegmentResult segment) { + public Boundaries getAttribute(RouteSegmentResult segment) { + /* + no-op + */ return null; } @Override - public String determineColor(String attribute) { - return null; - } - - public String determineColor(Incline incline) { - float value = incline.getValue(); - return value >= 0 ? POSITIVE_INCLINE_COLOR_ATTR_NAME : NEGATIVE_INCLINE_COLOR_ATTR_NAME; + public String determineColor(Boundaries attribute) { + return attribute.getLowerBoundary() >= 0 ? POSITIVE_INCLINE_COLOR_ATTR_NAME : NEGATIVE_INCLINE_COLOR_ATTR_NAME; } } - public static class RouteSegmentAttribute { + public static class RouteSegmentAttribute { private final int index; - private final String attribute; + private final E attribute; private final String colorAttrName; private float distance; private float initDistance; - public RouteSegmentAttribute(int index, String attribute, String colorAttrName) { + public RouteSegmentAttribute(int index, E attribute, String colorAttrName) { this.index = index; this.attribute = attribute; this.colorAttrName = colorAttrName; @@ -262,7 +258,7 @@ public class RouteStatistics { return index; } - public String getAttribute() { + public E getAttribute() { return attribute; } @@ -317,10 +313,6 @@ public class RouteStatistics { return distance; } - public String getBoundariesAsString() { - return String.format("%d-%d", Math.round(boundaries.getLowerBoundary()), Math.round(boundaries.getUpperBoundary())); - } - public Boundaries getBoundaries() { return this.boundaries; } @@ -340,13 +332,13 @@ public class RouteStatistics { private static final int MAX_INCLINE = 100; private static final int STEP = 4; private static final int NUM; - private static final int[] BOUNDARIES; + private static final int[] BOUNDARIES_ARRAY; static { - NUM = (int) ((MAX_INCLINE - MIN_INCLINE) / STEP + 1); - BOUNDARIES = new int[NUM]; + NUM = ((MAX_INCLINE - MIN_INCLINE) / STEP + 1); + BOUNDARIES_ARRAY = new int[NUM]; for (int i = 0; i < NUM; i++) { - BOUNDARIES[i] = MIN_INCLINE + i * STEP; + BOUNDARIES_ARRAY[i] = MIN_INCLINE + i * STEP; } } @@ -366,8 +358,8 @@ public class RouteStatistics { return new Boundaries(MIN_INCLINE + STEP, MIN_INCLINE); } for (int i = 1; i < NUM; i++) { - if (incline >= BOUNDARIES[i - 1] && incline < BOUNDARIES[i]) { - return new Boundaries(BOUNDARIES[i], BOUNDARIES[i - 1]); + if (incline >= BOUNDARIES_ARRAY[i - 1] && incline < BOUNDARIES_ARRAY[i]) { + return new Boundaries(BOUNDARIES_ARRAY[i], BOUNDARIES_ARRAY[i - 1]); } } return null; @@ -403,16 +395,21 @@ public class RouteStatistics { public int compareTo(Boundaries boundaries) { return (int) (getLowerBoundary() - boundaries.getLowerBoundary()); } + + @Override + public String toString() { + return String.format("%d-%d", Math.round(getLowerBoundary()), Math.round(getUpperBoundary())); + } } - public static class Statistics { + public static class Statistics { - private final List elements; - private final Map partition; + private final List> elements; + private final Map> partition; private final float totalDistance; - private Statistics(List elements, - Map partition, + private Statistics(List> elements, + Map> partition, float totalDistance) { this.elements = elements; this.partition = partition; @@ -423,11 +420,11 @@ public class RouteStatistics { return totalDistance; } - public List getElements() { + public List> getElements() { return elements; } - public Map getPartition() { + public Map> getPartition() { return partition; } } diff --git a/OsmAnd/res/layout/route_info_header.xml b/OsmAnd/res/layout/route_info_header.xml index 5e6af71f8e..5927fb1f53 100644 --- a/OsmAnd/res/layout/route_info_header.xml +++ b/OsmAnd/res/layout/route_info_header.xml @@ -252,6 +252,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + segments = routeStatistics.getElements(); + private void buildStatisticChart(View view, int chartId, Statistics routeStatistics) { + List> segments = routeStatistics.getElements(); HorizontalBarChart hbc = view.findViewById(chartId); List entries = new ArrayList<>(); float[] stacks = new float[segments.size()]; @@ -299,10 +299,10 @@ public class ShowRouteInfoDialogFragment extends DialogFragment { hbc.invalidate(); } - private void attachLegend(OsmandApplication app, LayoutInflater inflater, ViewGroup container, Statistics routeStatistics) { - Map partition = routeStatistics.getPartition(); - for (String key : partition.keySet()) { - RouteSegmentAttribute segment = partition.get(key); + private void attachLegend(OsmandApplication app, LayoutInflater inflater, ViewGroup container, Statistics routeStatistics) { + Map> partition = routeStatistics.getPartition(); + for (E key : partition.keySet()) { + RouteSegmentAttribute segment = partition.get(key); View view = inflater.inflate(R.layout.route_info_stat_item, container, false); TextView textView = view.findViewById(R.id.route_stat_item_text); String formattedDistance = OsmAndFormatter.getFormattedDistance(segment.getDistance(), getMyApplication());