Ignore:
Timestamp:
2016-07-21T12:20:41+02:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia: switch to Java 8

Location:
applications/editors/josm/plugins/wikipedia
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/Debouncer.java

    r32625 r32687  
    1919     */
    2020    public void debounce(final Object key, final Runnable runnable, long delay, TimeUnit unit) {
    21         final Future<?> prev = delayedMap.put(key, scheduler.schedule(new Runnable() {
    22             @Override
    23             public void run() {
    24                 try {
    25                     runnable.run();
    26                 } finally {
    27                     delayedMap.remove(key);
    28                 }
     21        final Future<?> prev = delayedMap.put(key, scheduler.schedule(() -> {
     22            try {
     23                runnable.run();
     24            } finally {
     25                delayedMap.remove(key);
    2926            }
    3027        }, delay, unit));
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/GuiUtils.java

    r32058 r32687  
    1414import java.util.SortedSet;
    1515import java.util.TreeSet;
    16 import java.util.concurrent.Callable;
     16import java.util.stream.Collectors;
    1717
    1818import static org.openstreetmap.josm.tools.I18n.tr;
    1919import static org.openstreetmap.josm.tools.I18n.trn;
    2020
    21 public class GuiUtils {
     21class GuiUtils {
    2222
    2323    static final String PREF_OVERWRITE = "wikipedia.overwrite-tag";
    2424
    25     protected static boolean confirmOverwrite(final String key, final String newValue, final Collection<OsmPrimitive> primitives) {
    26         final SortedSet<String> existingValues = new TreeSet<>(AlphanumComparator.getInstance());
    27         existingValues.addAll(Utils.transform(primitives, new Utils.Function<OsmPrimitive, String>() {
    28             @Override
    29             public String apply(OsmPrimitive x) {
    30                 return x.get(key);
    31             }
    32         }));
    33         existingValues.remove(newValue);
    34         existingValues.remove(null);
     25    static boolean confirmOverwrite(final String key, final String newValue, final Collection<OsmPrimitive> primitives) {
     26        final SortedSet<String> existingValues = primitives.stream()
     27                .map(x -> x.get(key))
     28                .filter(x -> x != null && !newValue.equals(x))
     29                .collect(Collectors.toCollection(() -> new TreeSet<>(AlphanumComparator.getInstance())));
    3530
    3631        if (existingValues.isEmpty()) {
    3732            return true;
    3833        }
    39         final Boolean r = GuiHelper.runInEDTAndWaitAndReturn(new Callable<Boolean>() {
    40             @Override
    41             public Boolean call() throws Exception {
    42                 return ConditionalOptionPaneUtil.showConfirmationDialog(PREF_OVERWRITE, Main.parent,
     34        final Boolean r = GuiHelper.runInEDTAndWaitAndReturn(() ->
     35                ConditionalOptionPaneUtil.showConfirmationDialog(PREF_OVERWRITE, Main.parent,
    4336                        trn(
    4437                                "Overwrite ''{0}'' tag {1} from {2} with new value ''{3}''?",
     
    4942                        JOptionPane.YES_NO_OPTION,
    5043                        JOptionPane.QUESTION_MESSAGE,
    51                         JOptionPane.YES_OPTION);
    52             }
    53         });
     44                        JOptionPane.YES_OPTION));
    5445        return Boolean.TRUE.equals(r);
    5546    }
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikidataItemSearchDialog.java

    r32670 r32687  
    88import java.awt.GridBagLayout;
    99import java.awt.event.ActionEvent;
    10 import java.awt.event.ActionListener;
    1110import java.util.Collection;
    1211import java.util.Collections;
     
    3029import org.openstreetmap.josm.gui.util.GuiHelper;
    3130import org.openstreetmap.josm.tools.GBC;
    32 import org.openstreetmap.josm.tools.Predicate;
    33 import org.openstreetmap.josm.tools.Utils;
    3431
    3532public final class WikidataItemSearchDialog extends ExtendedDialog {
     
    4239        super(Main.parent, tr("Search Wikidata items"), new String[]{tr("Add Tag"), tr("Cancel")});
    4340        this.selector = new Selector();
    44         this.selector.setDblClickListener(new ActionListener() {
    45             @Override
    46             public void actionPerformed(ActionEvent e) {
    47                 buttonAction(0, null);
    48             }
    49         });
     41        this.selector.setDblClickListener(e -> buttonAction(0, null));
    5042        this.targetKey = new AutoCompletingComboBox();
    5143        this.targetKey.setEditable(true);
     
    9385        keys.add(new AutoCompletionListItem("subject:wikidata"));
    9486        keys.add(new AutoCompletionListItem("name:etymology:wikidata"));
    95         keys.addAll(Utils.filter(editDataSet.getAutoCompletionManager().getKeys(), new Predicate<AutoCompletionListItem>() {
    96             @Override
    97             public boolean evaluate(AutoCompletionListItem object) {
    98                 return object.getValue().contains("wikidata");
    99             }
    100         }));
     87        editDataSet.getAutoCompletionManager().getKeys().stream()
     88                .filter(v -> v.getValue().contains("wikidata"))
     89                .forEach(keys::add);
    10190        targetKey.setPossibleACItems(keys);
    10291    }
     
    134123        protected void filterItems() {
    135124            final String query = edSearchText.getText();
    136             debouncer.debounce(Void.class, new Runnable() {
    137                 @Override
    138                 public void run() {
    139                     final List<WikipediaApp.WikidataEntry> entries = query == null || query.isEmpty()
    140                             ? Collections.<WikipediaApp.WikidataEntry>emptyList()
    141                             : WikipediaApp.getWikidataEntriesForQuery(WikipediaToggleDialog.wikipediaLang.get(), query, Locale.getDefault());
    142                     GuiHelper.runInEDT(new Runnable() {
    143                         @Override
    144                         public void run() {
    145                             lsResultModel.setItems(entries);
    146                         }
    147                     });
    148                 }
     125            debouncer.debounce(getClass(), () -> {
     126                final List<WikipediaApp.WikidataEntry> entries = query == null || query.isEmpty()
     127                        ? Collections.emptyList()
     128                        : WikipediaApp.getWikidataEntriesForQuery(WikipediaToggleDialog.wikipediaLang.get(), query, Locale.getDefault());
     129                GuiHelper.runInEDT(() -> lsResultModel.setItems(entries));
    149130            }, 200, TimeUnit.MILLISECONDS);
    150131        }
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaAddNamesAction.java

    r32424 r32687  
    2020
    2121public class WikipediaAddNamesAction extends JosmAction {
    22 
    23     final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));
    2422
    2523    public WikipediaAddNamesAction() {
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r32671 r32687  
    2020import java.util.regex.Matcher;
    2121import java.util.regex.Pattern;
     22import java.util.stream.Collectors;
     23import java.util.stream.Stream;
    2224
    2325import javax.xml.parsers.DocumentBuilder;
     
    3739import org.openstreetmap.josm.tools.CheckParameterUtil;
    3840import org.openstreetmap.josm.tools.HttpClient;
    39 import org.openstreetmap.josm.tools.Predicate;
    40 import org.openstreetmap.josm.tools.Predicates;
    4141import org.openstreetmap.josm.tools.Utils;
    42 import org.openstreetmap.josm.tools.Utils.Function;
    4342import org.w3c.dom.Document;
    4443import org.w3c.dom.Node;
     
    103102                if ("wikidata".equals(wikipediaLang)) {
    104103                    final List<WikidataEntry> withLabel = getLabelForWikidata(entries, Locale.getDefault());
    105                     return new ArrayList<WikipediaEntry>(withLabel);
     104                    return new ArrayList<>(withLabel);
    106105                } else {
    107106                    return entries;
     
    162161
    163162    static List<WikipediaEntry> getEntriesFromClipboard(final String wikipediaLang) {
    164         final List<String> clipboardLines = Arrays.asList(Utils.getClipboardContent().split("[\\n\\r]+"));
    165         return new ArrayList<>(Utils.transform(clipboardLines, new Function<String, WikipediaEntry>() {
    166 
    167             @Override
    168             public WikipediaEntry apply(String x) {
    169                 return new WikipediaEntry(wikipediaLang, x);
    170             }
    171         }));
     163        return Arrays.stream(Utils.getClipboardContent().split("[\\n\\r]+"))
     164                .map(x -> new WikipediaEntry(wikipediaLang, x))
     165                .collect(Collectors.toList());
    172166    }
    173167
     
    210204    }
    211205
    212     static Collection<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) {
     206    static Stream<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) {
    213207        if ("wikidata".equals(wikipediaLang)) {
    214             return Utils.filter(Collections.singleton(p.get("wikidata")), Predicates.not(Predicates.isNull()));
     208            return Stream.of(p.get("wikidata")).filter(Objects::nonNull);
    215209        }
    216210        final Map<String, String> tags = p.getKeys();
    217         return Utils.transform(Utils.filter(
    218                 Arrays.asList(
     211        return Stream
     212                .of(
    219213                        WikipediaLangArticle.parseTag("wikipedia", tags.get("wikipedia")),
    220214                        WikipediaLangArticle.parseTag("wikipedia:" + wikipediaLang, tags.get("wikipedia:" + wikipediaLang))
    221                 ), new Predicate<WikipediaLangArticle>() {
    222             @Override
    223             public boolean evaluate(WikipediaLangArticle wp) {
    224                 return wp != null && wikipediaLang.equals(wp.lang);
    225             }
    226         }), new Function<WikipediaLangArticle, String>() {
    227             @Override
    228             public String apply(WikipediaLangArticle wp) {
    229                 return wp.article;
    230             }
    231         });
     215                ).filter(Objects::nonNull)
     216                .filter(wikipediaLang::equals)
     217                .map(wp -> wp.article);
    232218    }
    233219
     
    250236                    "&sitefilter=" + wikipediaLang + "wiki" +
    251237                    "&format=xml" +
    252                     "&titles=" + Utils.join("|", Utils.transform(articles, new Function<String, String>() {
    253                 @Override
    254                 public String apply(String x) {
    255                     return Utils.encodeUrl(x);
    256                 }
    257             }));
     238                    "&titles=" + articles.stream().map(Utils::encodeUrl).collect(Collectors.joining("|"));
    258239            final Map<String, String> r = new TreeMap<>();
    259240            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     
    315296            return entriesWithLabel;
    316297        }
    317         final Collection<String> wikidataIds = Utils.transform(entries, new Function<WikipediaEntry, String>() {
    318             @Override
    319             public String apply(WikipediaEntry x) {
    320                 return x.wikipediaArticle;
    321             }
    322         });
    323298        try {
    324299            final String url = "https://www.wikidata.org/w/api.php" +
    325300                    "?action=wbgetentities" +
    326301                    "&props=labels|descriptions" +
    327                     "&ids=" + Utils.join("|", wikidataIds) +
     302                    "&ids=" + entries.stream().map(x -> x.wikipediaArticle).collect(Collectors.joining("|")) +
    328303                    "&format=xml";
    329304            final Collection<String> languages = new ArrayList<>();
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaCategorySearchDialog.java

    r32670 r32687  
    2323        super(Main.parent, tr("Search Wikipedia category"), new String[]{tr("Load category"), tr("Cancel")});
    2424        this.selector = new Selector();
    25         this.selector.setDblClickListener(new ActionListener() {
    26             @Override
    27             public void actionPerformed(ActionEvent e) {
    28                 buttonAction(0, null);
    29             }
    30         });
     25        this.selector.setDblClickListener(e -> buttonAction(0, null));
    3126
    3227        setContent(selector, false);
     
    6661        protected void filterItems() {
    6762            final String query = edSearchText.getText();
    68             debouncer.debounce(Void.class, new Runnable() {
    69                 @Override
    70                 public void run() {
    71                     final List<String> entries = query == null || query.isEmpty()
    72                             ? Collections.<String>emptyList()
    73                             : WikipediaApp.getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(), query);
    74                     GuiHelper.runInEDT(new Runnable() {
    75                         @Override
    76                         public void run() {
    77                             lsResultModel.setItems(entries);
    78                         }
    79                     });
    80                 }
     63            debouncer.debounce(getClass(), () -> {
     64                final List<String> entries = query == null || query.isEmpty()
     65                        ? Collections.emptyList()
     66                        : WikipediaApp.getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(), query);
     67                GuiHelper.runInEDT(() -> lsResultModel.setItems(entries));
    8168            }, 200, TimeUnit.MILLISECONDS);
    8269        }
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java

    r32670 r32687  
    1212import java.util.HashSet;
    1313import java.util.List;
     14import java.util.Optional;
    1415import java.util.Set;
    1516
     
    4344import org.openstreetmap.josm.tools.LanguageInfo;
    4445import org.openstreetmap.josm.tools.OpenBrowser;
    45 import org.openstreetmap.josm.tools.Utils;
    4646import org.wikipedia.WikipediaApp.WikipediaEntry;
    4747
     
    7373                public void mouseClicked(MouseEvent e) {
    7474                    if (e.getClickCount() == 2 && getSelectedValue() != null) {
    75                         final WikipediaEntry entry = (WikipediaEntry) getSelectedValue();
     75                        final WikipediaEntry entry = getSelectedValue();
    7676                        if (entry.coordinate != null) {
    7777                            BoundingXYVisitor bbox = new BoundingXYVisitor();
     
    7979                            Main.map.mapView.zoomTo(bbox);
    8080                        }
    81                         final String search = Utils.firstNonNull(entry.label, entry.wikipediaArticle).replaceAll("\\(.*\\)", "");
     81                        final String search = Optional.ofNullable(entry.label).orElse(entry.wikipediaArticle).replaceAll("\\(.*\\)", "");
    8282                        SearchAction.search(search, SearchAction.SearchMode.replace);
    8383                    }
     
    261261        public void actionPerformed(ActionEvent e) {
    262262            if (list.getSelectedValue() != null) {
    263                 final String url = ((WikipediaEntry) list.getSelectedValue()).getBrowserUrl();
     263                final String url = list.getSelectedValue().getBrowserUrl();
    264264                Main.info("Wikipedia: opening " + url);
    265265                OpenBrowser.displayUrl(url);
     
    357357        articles.clear();
    358358        if (Main.main != null && Main.getLayerManager().getEditDataSet() != null) {
    359             for (final OsmPrimitive p : Main.getLayerManager().getEditDataSet().allPrimitives()) {
    360                 articles.addAll(WikipediaApp.getWikipediaArticles(language, p));
    361             }
     359            Main.getLayerManager().getEditDataSet().allPrimitives().stream()
     360                    .flatMap(p -> WikipediaApp.getWikipediaArticles(language, p))
     361                    .forEach(articles::add);
    362362        }
    363363    }
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r32670 r32687  
    55import org.openstreetmap.josm.Main;
    66import org.openstreetmap.josm.data.coor.LatLon;
    7 import org.openstreetmap.josm.tools.Predicate;
    8 import org.openstreetmap.josm.tools.Predicates;
    9 import org.openstreetmap.josm.tools.Utils;
    107import org.wikipedia.WikipediaApp.WikipediaEntry;
    118import org.wikipedia.WikipediaApp.WikipediaLangArticle;
     
    2017import static org.hamcrest.CoreMatchers.is;
    2118import static org.hamcrest.CoreMatchers.nullValue;
     19import static org.junit.Assert.assertEquals;
    2220import static org.junit.Assert.assertThat;
    2321import static org.junit.Assert.assertTrue;
     
    142140        final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("de",
    143141                new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705));
    144         assertTrue(Utils.exists(entries, new Predicate<WikipediaEntry>() {
    145             @Override
    146             public boolean evaluate(WikipediaEntry entry) {
    147                 return "Reichstagsgebäude".equals(entry.wikipediaArticle) && "de".equals(entry.wikipediaLang);
    148             }
    149         }));
     142        final long c = entries.stream()
     143                .filter(entry -> "Reichstagsgebäude".equals(entry.wikipediaArticle) && "de".equals(entry.wikipediaLang))
     144                .count();
     145        assertEquals(1, c);
    150146    }
    151147
     
    166162        final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("wikidata",
    167163                new LatLon(47.20, 11.30), new LatLon(47.22, 11.32));
    168         assertTrue(Utils.exists(entries, new Predicate<WikipediaEntry>() {
    169             @Override
    170             public boolean evaluate(WikipediaEntry entry) {
    171                 return "Q865406".equals(entry.wikipediaArticle) && "wikidata".equals(entry.wikipediaLang) && "Birgitzer Alm".equals(entry.label);
    172             }
    173         }));
     164        final long c = entries.stream()
     165                .filter(entry -> "Q865406".equals(entry.wikipediaArticle) && "wikidata".equals(entry.wikipediaLang) && "Birgitzer Alm".equals(entry.label))
     166                .count();
     167        assertEquals(1, c);
    174168    }
    175169
     
    219213    public void testCategoriesForPrefix() throws Exception {
    220214        final List<String> categories = WikipediaApp.getCategoriesForPrefix("de", "Gemeinde in Öster");
    221         assertTrue(Utils.exists(categories, Predicates.equalTo("Gemeinde in Österreich")));
     215        assertTrue(categories.contains("Gemeinde in Österreich"));
    222216    }
    223217}
Note: See TracChangeset for help on using the changeset viewer.