Commit 78eeca07 authored by Simon Legner's avatar Simon Legner

JOSM/wikipedia: refactoring (move label to WikidataEntry)

git-svn-id: https://svn.openstreetmap.org/applications/editors/josm/plugins/wikipedia@32892 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
parent 959dc665
......@@ -82,7 +82,7 @@ public final class WikipediaApp {
if ("wikidata".equals(wikipediaLang)) {
return new WikidataEntry(name, null, latLon, null);
} else {
return new WikipediaEntry(wikipediaLang, name, name, latLon);
return new WikipediaEntry(wikipediaLang, name, latLon);
}
}).collect(Collectors.toList());
if ("wikidata".equals(wikipediaLang)) {
......
......@@ -3,16 +3,22 @@ package org.wikipedia.data;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.Tag;
import org.openstreetmap.josm.tools.AlphanumComparator;
import org.openstreetmap.josm.tools.CheckParameterUtil;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaApp;
import java.util.Comparator;
import java.util.Optional;
public class WikidataEntry extends WikipediaEntry {
public final String label;
public final String description;
public WikidataEntry(String id, String label, LatLon coordinate, String description) {
super("wikidata", id, label, coordinate);
super("wikidata", id, coordinate);
this.label = label;
this.description = description;
ensureValidWikidataId(id);
}
......@@ -32,7 +38,24 @@ public class WikidataEntry extends WikipediaEntry {
return Utils.escapeReservedCharactersHTML(bold) + " <span color='gray'>" + Utils.escapeReservedCharactersHTML(gray) + "</span>";
}
@Override
public String getSearchText() {
return Optional.ofNullable(label).orElse(article);
}
private static void ensureValidWikidataId(String id) {
CheckParameterUtil.ensureThat(WikipediaApp.WIKIDATA_PATTERN.matcher(id).matches(), "Invalid Wikidata ID given: " + id);
}
@Override
public int compareTo(WikipediaEntry o) {
if (o instanceof WikidataEntry) {
return Comparator
.<WikidataEntry, String>comparing(x -> x.label, AlphanumComparator.getInstance())
.thenComparing(x -> x.article, AlphanumComparator.getInstance())
.compare(this, ((WikidataEntry) o));
} else {
return super.compareTo(o);
}
}
}
......@@ -7,7 +7,6 @@ import org.openstreetmap.josm.tools.AlphanumComparator;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaApp;
import java.util.Comparator;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -16,18 +15,16 @@ public class WikipediaEntry implements Comparable<WikipediaEntry> {
public final String lang;
public final String article;
public final String label;
public final LatLon coordinate;
private Boolean wiwosmStatus;
public WikipediaEntry(String lang, String article) {
this(lang, article, null, null);
this(lang, article, null);
}
public WikipediaEntry(String lang, String article, String label, LatLon coordinate) {
public WikipediaEntry(String lang, String article, LatLon coordinate) {
this.lang = lang;
this.article = article;
this.label = label;
this.coordinate = coordinate;
}
......@@ -89,6 +86,10 @@ public class WikipediaEntry implements Comparable<WikipediaEntry> {
return article;
}
public String getSearchText() {
return article;
}
@Override
public String toString() {
return article;
......@@ -96,10 +97,7 @@ public class WikipediaEntry implements Comparable<WikipediaEntry> {
@Override
public int compareTo(WikipediaEntry o) {
return Comparator
.<WikipediaEntry, String>comparing(x -> x.label, AlphanumComparator.getInstance())
.thenComparing(x -> x.article, AlphanumComparator.getInstance())
.compare(this, o);
return AlphanumComparator.getInstance().compare(article, o.article);
}
@Override
......
......@@ -10,7 +10,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.swing.AbstractAction;
......@@ -78,7 +77,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
bbox.visit(entry.coordinate);
Main.map.mapView.zoomTo(bbox);
}
final String search = Optional.ofNullable(entry.label).orElse(entry.article).replaceAll("\\(.*\\)", "");
final String search = entry.getSearchText().replaceAll("\\(.*\\)", "");
SearchAction.search(search, SearchAction.SearchMode.replace);
}
}
......
......@@ -96,6 +96,7 @@ public class WikipediaAppTest {
final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("wikidata",
new LatLon(47.20, 11.30), new LatLon(47.22, 11.32));
final long c = entries.stream()
.map(WikidataEntry.class::cast)
.filter(entry -> "Q865406".equals(entry.article) && "wikidata".equals(entry.lang) && "Birgitzer Alm".equals(entry.label))
.count();
assertEquals(1, c);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment