Fixes according to review

This commit is contained in:
PaulStets 2017-09-16 14:39:02 +03:00
parent 8f2ab2d077
commit 37b91eeadb

View file

@ -532,73 +532,73 @@ public class SearchPhrase {
} }
} }
public void sortFiles() { public void sortFiles() {
if (indexes == null) { if(indexes == null) {
indexes = new ArrayList<>(getOfflineIndexes()); indexes = new ArrayList<>(getOfflineIndexes());
} }
Map<String, List<BinaryMapIndexReader>> diffsByRegion = getDiffsByRegion(); Map<String, List<BinaryMapIndexReader>> diffsByRegion = getDiffsByRegion();
final LatLon ll = getLastTokenLocation(); final LatLon ll = getLastTokenLocation();
if (ll != null) { if(ll != null) {
Collections.sort(indexes, new Comparator<BinaryMapIndexReader>() { Collections.sort(indexes, new Comparator<BinaryMapIndexReader>() {
Map<BinaryMapIndexReader, LatLon> locations = new HashMap<>(); Map<BinaryMapIndexReader, LatLon> locations = new HashMap<>();
@Override @Override
public int compare(BinaryMapIndexReader o1, BinaryMapIndexReader o2) { public int compare(BinaryMapIndexReader o1, BinaryMapIndexReader o2) {
LatLon rc1 = o1 == null ? null : getLocation(o1); LatLon rc1 = o1 == null ? null : getLocation(o1);
LatLon rc2 = o2 == null ? null : getLocation(o2); LatLon rc2 = o2 == null ? null : getLocation(o2);
double d1 = rc1 == null ? 10000000d : MapUtils.getDistance(rc1, ll); double d1 = rc1 == null ? 10000000d : MapUtils.getDistance(rc1, ll);
double d2 = rc2 == null ? 10000000d : MapUtils.getDistance(rc2, ll); double d2 = rc2 == null ? 10000000d : MapUtils.getDistance(rc2, ll);
return Double.compare(d1, d2); return Double.compare(d1, d2);
} }
private LatLon getLocation(BinaryMapIndexReader o1) { private LatLon getLocation(BinaryMapIndexReader o1) {
if (locations.containsKey(o1)) { if(locations.containsKey(o1)) {
return locations.get(o1); return locations.get(o1);
} }
LatLon rc1 = null; LatLon rc1 = null;
if (o1.containsMapData()) { if(o1.containsMapData()) {
rc1 = o1.getMapIndexes().get(0).getCenterLatLon(); rc1 = o1.getMapIndexes().get(0).getCenterLatLon();
} else { } else {
rc1 = o1.getRegionCenter(); rc1 = o1.getRegionCenter();
} }
locations.put(o1, rc1); locations.put(o1, rc1);
return rc1; return rc1;
} }
}); });
if (!diffsByRegion.isEmpty()) { if (!diffsByRegion.isEmpty()) {
List<BinaryMapIndexReader> finalSort = new ArrayList<>(); List<BinaryMapIndexReader> finalSort = new ArrayList<>();
for (int i = 0; i < indexes.size(); i++) { for (int i = 0; i < indexes.size(); i++) {
BinaryMapIndexReader currFile = indexes.get(i); BinaryMapIndexReader currFile = indexes.get(i);
if (diffsByRegion.get(currFile.getRegionName()) != null) { if (diffsByRegion.get(currFile.getRegionName()) != null) {
finalSort.addAll(diffsByRegion.get(currFile.getRegionName())); finalSort.addAll(diffsByRegion.get(currFile.getRegionName()));
finalSort.add(currFile); finalSort.add(currFile);
} else { } else {
finalSort.add(currFile); finalSort.add(currFile);
} }
} }
indexes.clear(); indexes.clear();
indexes.addAll(finalSort); indexes.addAll(finalSort);
} }
} }
} }
private Map<String, List<BinaryMapIndexReader>> getDiffsByRegion() { private Map<String, List<BinaryMapIndexReader>> getDiffsByRegion() {
Map<String, List<BinaryMapIndexReader>> result = new HashMap<>(); Map<String, List<BinaryMapIndexReader>> result = new HashMap<>();
Iterator<BinaryMapIndexReader> it = indexes.iterator(); Iterator<BinaryMapIndexReader> it = indexes.iterator();
while (it.hasNext()) { while (it.hasNext()) {
BinaryMapIndexReader r = it.next(); BinaryMapIndexReader r = it.next();
if (r.getFile().getName().matches("[a-zA-Z_-]+([0-9]+_*){3}[.a-z]+")) { if (r.getFile().getName().matches("[a-zA-Z_-]+([0-9]+_*){3}[.a-z]+")) {
String currRegionName = r.getRegionName(); String currRegionName = r.getRegionName();
if (result.containsKey(currRegionName)) { if (result.containsKey(currRegionName)) {
result.get(currRegionName).add(r); result.get(currRegionName).add(r);
} else { } else {
result.put(currRegionName, new ArrayList<>(Arrays.asList(r))); result.put(currRegionName, new ArrayList<>(Arrays.asList(r)));
} }
it.remove(); it.remove();
} }
} }
return result; return result;
} }
public static class NameStringMatcher implements StringMatcher { public static class NameStringMatcher implements StringMatcher {