OsmAnd/OsmAnd-java/src/net/osmand/search/SearchUICore.java

584 lines
17 KiB
Java
Raw Normal View History

2016-07-13 11:59:44 +02:00
package net.osmand.search;
2016-07-08 14:32:02 +02:00
2016-07-21 11:34:54 +02:00
import net.osmand.Collator;
2016-07-12 20:16:42 +02:00
import net.osmand.OsmAndCollator;
2016-07-08 14:32:02 +02:00
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
2016-07-21 11:34:54 +02:00
import net.osmand.data.Amenity;
2016-07-08 14:32:02 +02:00
import net.osmand.data.LatLon;
2016-07-10 11:46:39 +02:00
import net.osmand.osm.MapPoiTypes;
import net.osmand.search.core.CustomSearchPoiFilter;
2016-07-13 22:32:52 +02:00
import net.osmand.search.core.ObjectType;
2016-07-13 11:59:44 +02:00
import net.osmand.search.core.SearchCoreAPI;
import net.osmand.search.core.SearchCoreFactory;
2016-08-03 23:44:52 +02:00
import net.osmand.search.core.SearchCoreFactory.SearchAmenityTypesAPI;
2016-07-24 22:34:26 +02:00
import net.osmand.search.core.SearchCoreFactory.SearchBuildingAndIntersectionsByStreetAPI;
import net.osmand.search.core.SearchCoreFactory.SearchStreetByCityAPI;
2016-07-13 11:59:44 +02:00
import net.osmand.search.core.SearchPhrase;
import net.osmand.search.core.SearchPhrase.NameStringMatcher;
2016-07-13 11:59:44 +02:00
import net.osmand.search.core.SearchResult;
import net.osmand.search.core.SearchSettings;
import net.osmand.search.core.SearchWord;
2016-07-08 14:32:02 +02:00
import net.osmand.util.Algorithms;
2016-07-21 11:34:54 +02:00
import net.osmand.util.MapUtils;
2016-07-08 14:32:02 +02:00
2016-07-11 10:01:25 +02:00
import org.apache.commons.logging.Log;
2016-08-13 13:29:02 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
2016-11-04 00:13:40 +01:00
import java.util.LinkedList;
2016-08-13 13:29:02 +02:00
import java.util.List;
2016-11-04 00:13:40 +01:00
import java.util.ListIterator;
2016-08-13 13:29:02 +02:00
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
2016-07-08 14:32:02 +02:00
public class SearchUICore {
2016-07-21 11:34:54 +02:00
private static final int TIMEOUT_BETWEEN_CHARS = 200;
2017-03-24 18:13:07 +01:00
private static final Log LOG = PlatformUtil.getLog(SearchUICore.class);
2016-07-08 14:32:02 +02:00
private SearchPhrase phrase;
2017-03-24 18:13:07 +01:00
private SearchResultCollection currentSearchResult;
2016-07-08 14:32:02 +02:00
private ThreadPoolExecutor singleThreadedExecutor;
private LinkedBlockingQueue<Runnable> taskQueue;
2016-08-13 13:29:02 +02:00
private Runnable onSearchStart = null;
2016-07-08 14:32:02 +02:00
private Runnable onResultsComplete = null;
private AtomicInteger requestNumber = new AtomicInteger();
2016-07-10 11:46:39 +02:00
private int totalLimit = -1; // -1 unlimited - not used
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
List<SearchCoreAPI> apis = new ArrayList<>();
private SearchSettings searchSettings;
2016-07-10 11:46:39 +02:00
private MapPoiTypes poiTypes;
2017-03-23 20:23:40 +01:00
2016-08-05 14:15:17 +02:00
public SearchUICore(MapPoiTypes poiTypes, String locale, boolean transliterate) {
2016-07-10 11:46:39 +02:00
this.poiTypes = poiTypes;
2016-07-08 14:32:02 +02:00
taskQueue = new LinkedBlockingQueue<Runnable>();
2016-07-28 21:07:49 +02:00
searchSettings = new SearchSettings(new ArrayList<BinaryMapIndexReader>());
2016-08-05 14:15:17 +02:00
searchSettings = searchSettings.setLang(locale, transliterate);
2016-08-05 13:33:29 +02:00
phrase = new SearchPhrase(searchSettings, OsmAndCollator.primaryCollator());
currentSearchResult = new SearchResultCollection(phrase);
2016-07-08 14:32:02 +02:00
singleThreadedExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, taskQueue);
}
2017-03-23 20:23:40 +01:00
2016-07-11 10:01:25 +02:00
public static class SearchResultCollection {
private List<SearchResult> searchResults;
private SearchPhrase phrase;
2016-11-04 00:13:40 +01:00
private static final int DEPTH_TO_CHECK_SAME_SEARCH_RESULTS = 20;
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
public SearchResultCollection(SearchPhrase phrase) {
searchResults = new ArrayList<>();
2016-07-11 10:01:25 +02:00
this.phrase = phrase;
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
public SearchResultCollection combineWithCollection(SearchResultCollection collection, boolean resort, boolean removeDuplicates) {
SearchResultCollection src = new SearchResultCollection(phrase);
src.addSearchResults(searchResults, false, false);
src.addSearchResults(collection.searchResults, resort, removeDuplicates);
return src;
2016-07-11 10:01:25 +02:00
}
2017-03-24 18:13:07 +01:00
2016-08-05 21:07:49 +02:00
public SearchResultCollection addSearchResults(List<SearchResult> sr, boolean resortAll, boolean removeDuplicates) {
2017-03-24 18:13:07 +01:00
if (resortAll) {
2016-08-05 13:33:29 +02:00
this.searchResults.addAll(sr);
sortSearchResults();
2017-03-24 18:13:07 +01:00
if (removeDuplicates) {
2016-08-05 13:33:29 +02:00
filterSearchDuplicateResults();
}
} else {
2017-03-24 18:13:07 +01:00
if (!removeDuplicates) {
2016-08-05 16:08:07 +02:00
this.searchResults.addAll(sr);
} else {
2016-08-05 13:33:29 +02:00
ArrayList<SearchResult> addedResults = new ArrayList<>(sr);
SearchResultComparator cmp = new SearchResultComparator(phrase);
Collections.sort(addedResults, cmp);
filterSearchDuplicateResults(addedResults);
2017-03-24 18:13:07 +01:00
int i = 0;
2016-08-05 13:33:29 +02:00
int j = 0;
2017-03-24 18:13:07 +01:00
while (j < addedResults.size()) {
2016-08-05 13:33:29 +02:00
SearchResult addedResult = addedResults.get(j);
2017-03-24 18:13:07 +01:00
if (i >= searchResults.size()) {
2016-11-04 00:13:40 +01:00
int k = 0;
boolean same = false;
2017-03-24 18:13:07 +01:00
while (searchResults.size() > k && k < DEPTH_TO_CHECK_SAME_SEARCH_RESULTS) {
if (sameSearchResult(addedResult, searchResults.get(searchResults.size() - k - 1))) {
2016-11-04 00:13:40 +01:00
same = true;
break;
}
k++;
}
2017-03-24 18:13:07 +01:00
if (!same) {
2016-08-05 13:33:29 +02:00
searchResults.add(addedResult);
}
j++;
continue;
}
SearchResult existingResult = searchResults.get(i);
2017-03-24 18:13:07 +01:00
if (sameSearchResult(addedResult, existingResult)) {
2016-08-05 13:33:29 +02:00
j++;
continue;
}
int compare = cmp.compare(existingResult, addedResult);
2017-03-24 18:13:07 +01:00
if (compare == 0) {
2016-08-05 13:33:29 +02:00
// existingResult == addedResult
j++;
2017-03-24 18:13:07 +01:00
} else if (compare > 0) {
2016-08-05 13:33:29 +02:00
// existingResult > addedResult
this.searchResults.add(addedResults.get(j));
j++;
} else {
// existingResult < addedResult
i++;
}
}
}
}
2016-08-05 21:07:49 +02:00
return this;
2016-08-05 13:33:29 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-11 10:01:25 +02:00
public List<SearchResult> getCurrentSearchResults() {
2016-08-05 13:33:29 +02:00
return Collections.unmodifiableList(searchResults);
2016-07-11 10:01:25 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-11 10:01:25 +02:00
public SearchPhrase getPhrase() {
return phrase;
}
2016-08-05 13:33:29 +02:00
public void sortSearchResults() {
2017-03-24 18:13:07 +01:00
Collections.sort(searchResults, new SearchResultComparator(phrase));
2016-08-05 13:33:29 +02:00
}
public void filterSearchDuplicateResults() {
filterSearchDuplicateResults(searchResults);
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
private void filterSearchDuplicateResults(List<SearchResult> lst) {
2016-11-04 00:13:40 +01:00
ListIterator<SearchResult> it = lst.listIterator();
LinkedList<SearchResult> lstUnique = new LinkedList<SearchResult>();
2017-03-24 18:13:07 +01:00
while (it.hasNext()) {
2016-08-05 13:33:29 +02:00
SearchResult r = it.next();
2016-11-04 00:13:40 +01:00
boolean same = false;
2017-03-24 18:13:07 +01:00
for (SearchResult rs : lstUnique) {
2016-11-04 00:13:40 +01:00
same = sameSearchResult(rs, r);
2017-03-24 18:13:07 +01:00
if (same) {
2016-11-04 00:13:40 +01:00
break;
}
}
2017-03-24 18:13:07 +01:00
if (same) {
2016-08-05 13:33:29 +02:00
it.remove();
} else {
2016-11-04 00:13:40 +01:00
lstUnique.add(r);
2017-03-24 18:13:07 +01:00
if (lstUnique.size() > DEPTH_TO_CHECK_SAME_SEARCH_RESULTS) {
2016-11-04 00:13:40 +01:00
lstUnique.remove(0);
}
2016-08-05 13:33:29 +02:00
}
2017-03-24 18:13:07 +01:00
}
2016-08-05 13:33:29 +02:00
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
public boolean sameSearchResult(SearchResult r1, SearchResult r2) {
2017-03-24 18:13:07 +01:00
if (r1.location != null && r2.location != null) {
2016-08-05 13:33:29 +02:00
Amenity a1 = null;
2017-03-24 18:13:07 +01:00
if (r1.object instanceof Amenity) {
2016-08-05 13:33:29 +02:00
a1 = (Amenity) r1.object;
}
Amenity a2 = null;
2017-03-24 18:13:07 +01:00
if (r2.object instanceof Amenity) {
2016-08-05 13:33:29 +02:00
a2 = (Amenity) r2.object;
}
if (r1.localeName.equals(r2.localeName)) {
double similarityRadius = 30;
if (a1 != null && a2 != null) {
// here 2 points are amenity
String type1 = a1.getType().getKeyName();
String type2 = a2.getType().getKeyName();
String subType1 = a1.getSubType();
String subType2 = a2.getSubType();
2017-03-25 00:22:37 +01:00
if(a1.getId().longValue() == a2.getId().longValue() && (subType1.equals("building") || subType2.equals("building"))) {
return true;
}
2016-08-05 13:33:29 +02:00
if (!type1.equals(type2)) {
return false;
}
if (type1.equals("natural")) {
2016-11-04 00:13:40 +01:00
similarityRadius = 50000;
2016-08-05 13:33:29 +02:00
} else if (subType1.equals(subType2)) {
if (subType1.contains("cn_ref") || subType1.contains("wn_ref")
|| (subType1.startsWith("route_hiking_") && subType1.endsWith("n_poi"))) {
2016-10-13 19:09:30 +02:00
similarityRadius = 50000;
2016-08-05 13:33:29 +02:00
}
}
2017-03-24 18:13:07 +01:00
} else if (ObjectType.isAddress(r1.objectType) && ObjectType.isAddress(r2.objectType)) {
2016-08-05 13:33:29 +02:00
similarityRadius = 100;
}
return MapUtils.getDistance(r1.location, r2.location) < similarityRadius;
}
2016-11-09 19:29:18 +01:00
} else if (r1.object != null && r2.object != null) {
return r1.object == r2.object;
2016-08-05 13:33:29 +02:00
}
return false;
}
2016-07-11 10:01:25 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-28 21:02:23 +02:00
public void setPoiTypes(MapPoiTypes poiTypes) {
this.poiTypes = poiTypes;
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
public int getTotalLimit() {
return totalLimit;
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
public void setTotalLimit(int totalLimit) {
this.totalLimit = totalLimit;
}
2017-03-24 18:13:07 +01:00
2016-08-03 23:44:52 +02:00
@SuppressWarnings("unchecked")
public <T> T getApiByClass(Class<T> cl) {
2017-03-23 20:23:40 +01:00
for (SearchCoreAPI a : apis) {
2017-03-24 18:13:07 +01:00
if (cl.isInstance(a)) {
2016-08-03 23:44:52 +02:00
return (T) a;
}
}
return null;
}
2017-03-24 18:13:07 +01:00
2016-08-03 23:44:52 +02:00
public <T extends SearchCoreAPI> SearchResultCollection shallowSearch(Class<T> cl,
2017-03-24 18:13:07 +01:00
String text, final ResultMatcher<SearchResult> matcher) throws IOException {
2016-08-03 23:44:52 +02:00
T api = getApiByClass(cl);
2017-03-24 18:13:07 +01:00
if (api != null) {
2016-08-03 23:44:52 +02:00
SearchPhrase sphrase = this.phrase.generateNewPhrase(text, searchSettings);
preparePhrase(sphrase);
AtomicInteger ai = new AtomicInteger();
SearchResultMatcher rm = new SearchResultMatcher(matcher, ai.get(), ai, totalLimit);
api.search(sphrase, rm);
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
SearchResultCollection collection = new SearchResultCollection(
2016-08-03 23:44:52 +02:00
sphrase);
2016-08-05 13:33:29 +02:00
collection.addSearchResults(rm.getRequestResults(), true, true);
LOG.info(">> Shallow Search phrase " + phrase + " " + rm.getRequestResults().size());
return collection;
2016-08-03 23:44:52 +02:00
}
2016-08-05 13:33:29 +02:00
return null;
2016-08-03 23:44:52 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
public void init() {
2016-07-24 22:34:26 +02:00
apis.add(new SearchCoreFactory.SearchLocationAndUrlAPI());
2016-07-10 11:46:39 +02:00
apis.add(new SearchCoreFactory.SearchAmenityTypesAPI(poiTypes));
2016-07-10 12:20:18 +02:00
apis.add(new SearchCoreFactory.SearchAmenityByTypeAPI(poiTypes));
2016-07-10 11:46:39 +02:00
apis.add(new SearchCoreFactory.SearchAmenityByNameAPI());
2017-03-24 18:13:07 +01:00
SearchBuildingAndIntersectionsByStreetAPI streetsApi =
2016-07-24 22:34:26 +02:00
new SearchCoreFactory.SearchBuildingAndIntersectionsByStreetAPI();
apis.add(streetsApi);
SearchStreetByCityAPI cityApi = new SearchCoreFactory.SearchStreetByCityAPI(streetsApi);
apis.add(cityApi);
2017-03-24 18:13:07 +01:00
apis.add(new SearchCoreFactory.SearchAddressByNameAPI(streetsApi, cityApi));
2016-07-08 14:32:02 +02:00
}
public void clearCustomSearchPoiFilters() {
2017-03-24 18:13:07 +01:00
for (SearchCoreAPI capi : apis) {
if (capi instanceof SearchAmenityTypesAPI) {
((SearchAmenityTypesAPI) capi).clearCustomFilters();
}
}
}
2016-08-03 23:44:52 +02:00
public void addCustomSearchPoiFilter(CustomSearchPoiFilter poiFilter, int priority) {
2017-03-24 18:13:07 +01:00
for (SearchCoreAPI capi : apis) {
if (capi instanceof SearchAmenityTypesAPI) {
2016-08-03 23:44:52 +02:00
((SearchAmenityTypesAPI) capi).addCustomFilter(poiFilter, priority);
}
}
}
2017-03-24 18:13:07 +01:00
2016-07-12 15:47:34 +02:00
public void registerAPI(SearchCoreAPI api) {
apis.add(api);
}
2017-03-24 18:13:07 +01:00
2016-07-11 10:01:25 +02:00
public SearchResultCollection getCurrentSearchResult() {
return currentSearchResult;
2016-07-08 14:32:02 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
public SearchPhrase getPhrase() {
return phrase;
}
2016-08-13 13:29:02 +02:00
public void setOnSearchStart(Runnable onSearchStart) {
this.onSearchStart = onSearchStart;
}
2016-07-08 14:32:02 +02:00
public void setOnResultsComplete(Runnable onResultsComplete) {
this.onResultsComplete = onResultsComplete;
}
2016-07-19 17:39:21 +02:00
public SearchSettings getSearchSettings() {
return searchSettings;
}
2016-07-10 11:46:39 +02:00
public void updateSettings(SearchSettings settings) {
searchSettings = settings;
2016-07-08 14:32:02 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-11 10:01:25 +02:00
private List<SearchResult> filterCurrentResults(List<SearchResult> rr, SearchPhrase phrase) {
List<SearchResult> l = currentSearchResult.searchResults;
2017-03-24 18:13:07 +01:00
for (SearchResult r : l) {
if (filterOneResult(r, phrase)) {
2016-07-08 14:32:02 +02:00
rr.add(r);
}
}
return rr;
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
private boolean filterOneResult(SearchResult object, SearchPhrase phrase) {
2016-07-10 11:46:39 +02:00
NameStringMatcher nameStringMatcher = phrase.getNameStringMatcher();
2017-03-24 18:13:07 +01:00
return nameStringMatcher.matches(object.localeName) || nameStringMatcher.matches(object.otherNames);
2016-07-08 14:32:02 +02:00
}
public boolean selectSearchResult(SearchResult r) {
this.phrase = this.phrase.selectWord(r);
return true;
}
2017-03-24 18:13:07 +01:00
2016-10-20 12:05:13 +02:00
public SearchPhrase resetPhrase() {
2016-07-28 19:17:26 +02:00
this.phrase = this.phrase.generateNewPhrase("", searchSettings);
2016-10-20 12:05:13 +02:00
return this.phrase;
}
public SearchPhrase resetPhrase(String text) {
this.phrase = this.phrase.generateNewPhrase(text, searchSettings);
return this.phrase;
2016-07-28 19:17:26 +02:00
}
2016-07-11 10:01:25 +02:00
public SearchResultCollection search(final String text, final ResultMatcher<SearchResult> matcher) {
2016-07-08 14:32:02 +02:00
final int request = requestNumber.incrementAndGet();
final SearchPhrase phrase = this.phrase.generateNewPhrase(text, searchSettings);
this.phrase = phrase;
2016-08-05 13:33:29 +02:00
SearchResultCollection quickRes = new SearchResultCollection(phrase);
2016-07-11 10:01:25 +02:00
filterCurrentResults(quickRes.searchResults, phrase);
2016-07-12 20:16:42 +02:00
LOG.info("> Search phrase " + phrase + " " + quickRes.searchResults.size());
2016-07-08 14:32:02 +02:00
singleThreadedExecutor.submit(new Runnable() {
@Override
public void run() {
try {
2016-08-13 13:29:02 +02:00
if (onSearchStart != null) {
onSearchStart.run();
}
2016-07-13 11:59:44 +02:00
SearchResultMatcher rm = new SearchResultMatcher(matcher, request, requestNumber, totalLimit);
2017-03-24 18:13:07 +01:00
if(TIMEOUT_BETWEEN_CHARS > 0) {
2016-07-21 11:34:54 +02:00
Thread.sleep(TIMEOUT_BETWEEN_CHARS);
}
2016-07-24 19:05:51 +02:00
if(rm.isCancelled()) {
return;
}
2016-07-08 14:32:02 +02:00
searchInBackground(phrase, rm);
if (!rm.isCancelled()) {
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
SearchResultCollection collection = new SearchResultCollection(
2016-07-11 10:01:25 +02:00
phrase);
2016-08-05 13:33:29 +02:00
collection.addSearchResults(rm.getRequestResults(), true, true);
LOG.info(">> Search phrase " + phrase + " " + rm.getRequestResults().size());
2016-07-11 10:01:25 +02:00
currentSearchResult = collection;
2016-07-08 14:32:02 +02:00
if (onResultsComplete != null) {
onResultsComplete.run();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
});
2016-07-11 10:01:25 +02:00
return quickRes;
2016-07-08 14:32:02 +02:00
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:58:01 +02:00
public boolean isSearchMoreAvailable(SearchPhrase phrase) {
2017-03-24 18:13:07 +01:00
for (SearchCoreAPI api : apis) {
if (api.getSearchPriority(phrase) >= 0 && api.isSearchMoreAvailable(phrase)) {
2016-08-05 13:58:01 +02:00
return true;
}
}
return false;
}
2016-07-24 19:05:51 +02:00
2016-07-08 14:32:02 +02:00
private void searchInBackground(final SearchPhrase phrase, SearchResultMatcher matcher) {
2016-08-03 23:44:52 +02:00
preparePhrase(phrase);
2017-03-24 18:13:07 +01:00
ArrayList<SearchCoreAPI> lst = new ArrayList<>(apis);
2016-07-08 14:32:02 +02:00
Collections.sort(lst, new Comparator<SearchCoreAPI>() {
@Override
public int compare(SearchCoreAPI o1, SearchCoreAPI o2) {
return Algorithms.compare(o1.getSearchPriority(phrase),
o2.getSearchPriority(phrase));
}
});
2017-03-23 20:23:40 +01:00
for (SearchCoreAPI api : lst) {
if (matcher.isCancelled()) {
2016-07-08 14:32:02 +02:00
break;
}
2017-03-23 20:23:40 +01:00
if (api.getSearchPriority(phrase) == -1) {
2016-07-10 11:46:39 +02:00
continue;
}
2016-07-08 14:32:02 +02:00
try {
api.search(phrase, matcher);
2017-03-24 18:13:07 +01:00
2016-07-13 22:32:52 +02:00
matcher.apiSearchFinished(api, phrase);
2016-07-12 20:16:42 +02:00
} catch (Throwable e) {
2016-07-08 14:32:02 +02:00
e.printStackTrace();
2016-07-12 20:16:42 +02:00
LOG.error(e.getMessage(), e);
2016-07-08 14:32:02 +02:00
}
}
}
2016-08-03 23:44:52 +02:00
private void preparePhrase(final SearchPhrase phrase) {
for (SearchWord sw : phrase.getWords()) {
2017-03-24 18:13:07 +01:00
if (sw.getResult() != null && sw.getResult().file != null) {
2016-08-03 23:44:52 +02:00
phrase.selectFile(sw.getResult().file);
}
}
phrase.sortFiles();
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
public static class SearchResultMatcher implements ResultMatcher<SearchResult>{
2016-07-08 14:32:02 +02:00
private final List<SearchResult> requestResults = new ArrayList<>();
2016-07-13 11:59:44 +02:00
private final ResultMatcher<SearchResult> matcher;
2016-07-08 14:32:02 +02:00
private final int request;
2016-07-13 11:59:44 +02:00
private final int totalLimit;
2016-07-24 19:05:51 +02:00
private SearchResult parentSearchResult;
2016-07-13 11:59:44 +02:00
private final AtomicInteger requestNumber;
2016-07-08 14:32:02 +02:00
int count = 0;
2017-03-24 18:13:07 +01:00
public SearchResultMatcher(ResultMatcher<SearchResult> matcher, int request,
AtomicInteger requestNumber, int totalLimit) {
2016-07-08 14:32:02 +02:00
this.matcher = matcher;
this.request = request;
2016-07-13 11:59:44 +02:00
this.requestNumber = requestNumber;
this.totalLimit = totalLimit;
2016-07-08 14:32:02 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-24 22:34:26 +02:00
public SearchResult setParentSearchResult(SearchResult parentSearchResult) {
SearchResult prev = this.parentSearchResult;
2016-07-24 19:05:51 +02:00
this.parentSearchResult = parentSearchResult;
2016-07-24 22:34:26 +02:00
return prev;
2016-07-24 19:05:51 +02:00
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
public List<SearchResult> getRequestResults() {
return requestResults;
}
2017-03-24 18:13:07 +01:00
2016-07-24 22:34:26 +02:00
public int getCount() {
return requestResults.size();
}
2017-03-24 18:13:07 +01:00
2016-07-13 22:32:52 +02:00
public void apiSearchFinished(SearchCoreAPI api, SearchPhrase phrase) {
2017-03-24 18:13:07 +01:00
if (matcher != null) {
2016-07-13 22:32:52 +02:00
SearchResult sr = new SearchResult(phrase);
sr.objectType = ObjectType.SEARCH_API_FINISHED;
sr.object = api;
2016-07-24 19:05:51 +02:00
sr.parentSearchResult = parentSearchResult;
2016-07-13 22:32:52 +02:00
matcher.publish(sr);
}
}
2017-03-24 18:13:07 +01:00
public void apiSearchRegionFinished(SearchCoreAPI api, BinaryMapIndexReader region, SearchPhrase phrase) {
if(matcher != null) {
SearchResult sr = new SearchResult(phrase);
sr.objectType = ObjectType.SEARCH_API_REGION_FINISHED;
sr.object = api;
2016-07-24 19:05:51 +02:00
sr.parentSearchResult = parentSearchResult;
sr.file = region;
matcher.publish(sr);
}
}
2017-03-24 18:13:07 +01:00
2016-07-08 14:32:02 +02:00
@Override
public boolean publish(SearchResult object) {
2017-03-24 18:13:07 +01:00
if (matcher == null || matcher.publish(object)) {
2016-07-08 14:32:02 +02:00
count++;
2016-07-24 19:05:51 +02:00
object.parentSearchResult = parentSearchResult;
2017-03-24 18:13:07 +01:00
if (totalLimit == -1 || count < totalLimit) {
requestResults.add(object);
2016-07-08 14:32:02 +02:00
}
return true;
}
return false;
}
@Override
public boolean isCancelled() {
boolean cancelled = request != requestNumber.get();
return cancelled || (matcher != null && matcher.isCancelled());
2017-03-24 18:13:07 +01:00
}
2016-08-05 13:33:29 +02:00
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
public static class SearchResultComparator implements Comparator<SearchResult> {
private SearchPhrase sp;
private Collator collator;
private LatLon loc;
2017-03-24 18:13:07 +01:00
private boolean sortByName;
2016-08-05 13:33:29 +02:00
public SearchResultComparator(SearchPhrase sp) {
this.sp = sp;
this.collator = sp.getCollator();
loc = sp.getLastTokenLocation();
2017-03-24 18:13:07 +01:00
sortByName = sp.isSortByName();
2016-08-05 13:33:29 +02:00
}
@Override
public int compare(SearchResult o1, SearchResult o2) {
2017-03-24 18:13:07 +01:00
if (o1.getFoundWordCount() != o2.getFoundWordCount()) {
2016-08-05 13:33:29 +02:00
return -Algorithms.compare(o1.getFoundWordCount(), o2.getFoundWordCount());
}
2017-03-24 21:01:46 +01:00
if (!sortByName) {
2017-03-24 18:13:07 +01:00
double s1 = o1.getSearchDistance(loc);
double s2 = o2.getSearchDistance(loc);
int cmp = Double.compare(s1, s2);
if (cmp != 0) {
return cmp;
}
2016-08-05 13:33:29 +02:00
}
int st1 = Algorithms.extractFirstIntegerNumber(o1.localeName);
int st2 = Algorithms.extractFirstIntegerNumber(o2.localeName);
2017-03-24 18:13:07 +01:00
if (st1 != st2) {
2016-08-05 13:33:29 +02:00
return Algorithms.compare(st1, st2);
}
2017-03-24 18:13:07 +01:00
int cmp = collator.compare(o1.localeName, o2.localeName);
if (cmp != 0) {
2016-08-05 13:33:29 +02:00
return cmp;
}
2016-10-19 11:32:20 +02:00
if (o1.object instanceof Amenity && o2.object instanceof Amenity) {
2016-10-18 14:19:26 +02:00
// here 2 points are amenity
2016-10-19 11:32:20 +02:00
Amenity a1 = (Amenity) o1.object;
Amenity a2 = (Amenity) o2.object;
2016-10-18 14:19:26 +02:00
String type1 = a1.getType().getKeyName();
String type2 = a2.getType().getKeyName();
String subType1 = a1.getSubType();
String subType2 = a2.getSubType();
cmp = collator.compare(type1, type2);
2017-03-24 18:13:07 +01:00
if (cmp != 0) {
2016-10-18 14:19:26 +02:00
return cmp;
}
cmp = collator.compare(subType1, subType2);
2017-03-24 18:13:07 +01:00
if (cmp != 0) {
2016-10-18 14:19:26 +02:00
return cmp;
}
}
2017-03-24 18:13:07 +01:00
double s1 = o1.getSearchDistance(loc, 1);
double s2 = o2.getSearchDistance(loc, 1);
2016-08-05 13:33:29 +02:00
return Double.compare(s1, s2);
}
2017-03-24 18:13:07 +01:00
2016-08-05 13:33:29 +02:00
}
2016-07-08 14:32:02 +02:00
}