Changeset 32720 in osm for applications


Ignore:
Timestamp:
2016-07-26T17:20:01+02:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia: simplify Wikidata label cache implementation

File:
1 edited

Legend:

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

    r32704 r32720  
    99import java.util.Locale;
    1010import java.util.Map;
    11 import java.util.concurrent.Callable;
     11import java.util.concurrent.CompletableFuture;
    1212import java.util.concurrent.ConcurrentHashMap;
    1313import java.util.concurrent.ExecutionException;
    14 import java.util.concurrent.Future;
    1514import java.util.stream.Collectors;
    1615
     
    2019
    2120import org.openstreetmap.josm.Main;
     21import org.openstreetmap.josm.gui.util.GuiHelper;
    2222import org.openstreetmap.josm.tools.Utils;
    2323
    2424public class WikidataTagCellRenderer extends DefaultTableCellRenderer {
    2525
    26     final Map<String, Future<String>> labelCache = new ConcurrentHashMap<>();
    27 
    28     static class LabelLoader implements Callable<String> {
    29         final String id;
    30         JTable table;
    31 
    32         public LabelLoader(String id, JTable table) {
    33             this.id = id;
    34             this.table = table;
    35         }
    36 
    37         @Override
    38         public String call() throws Exception {
    39             final String label = WikipediaApp.getLabelForWikidata(id, Locale.getDefault());
    40             table.repaint();
    41             table = null;
    42             return label;
    43         }
    44     }
     26    private final Map<String, CompletableFuture<String>> labelCache = new ConcurrentHashMap<>();
    4527
    4628    @Override
     
    7052    protected JLabel renderValues(Collection<String> ids, JTable table, JLabel component) {
    7153
    72         ids.stream()
    73                 .filter(id -> !labelCache.containsKey(id))
    74                 .forEach(id -> {
    75                     labelCache.put(id, Main.worker.submit(new LabelLoader(id, table)));
    76                 });
     54        ids.forEach(id ->
     55                labelCache.computeIfAbsent(id, x ->
     56                        CompletableFuture.supplyAsync(() -> WikipediaApp.getLabelForWikidata(x, Locale.getDefault())))
     57        );
    7758
    7859        final Collection<String> texts = new ArrayList<>(ids.size());
    7960        for (String id : ids) {
    8061            if (!labelCache.get(id).isDone()) {
     62                labelCache.get(id).thenRun(() -> GuiHelper.runInEDT(table::repaint));
    8163                return null;
    8264            }
Note: See TracChangeset for help on using the changeset viewer.