Changeset 15908 in josm for trunk/src


Ignore:
Timestamp:
2020-02-23T00:29:51+01:00 (5 years ago)
Author:
simon04
Message:

see #18729 - TaginfoRegionalInstance: use Stream API

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r15906 r15908  
    5959import org.openstreetmap.josm.command.Command;
    6060import org.openstreetmap.josm.data.UndoRedoHandler;
     61import org.openstreetmap.josm.data.coor.LatLon;
    6162import org.openstreetmap.josm.data.osm.AbstractPrimitive;
    6263import org.openstreetmap.josm.data.osm.DataSelectionListener;
     
    370371        destroyTaginfoNationalActions();
    371372        if (!newSel.isEmpty()) {
    372             Territories.getRegionalTaginfoUrls(
    373                     newSel.iterator().next().getBBox().getCenter()).values().stream().flatMap(List::stream).forEach(
    374                 taginfo -> taginfoNationalActions.add(new TaginfoAction(tagTable, editHelper::getDataKey, editHelper::getDataValues,
    375                         membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0),
    376                         taginfo.getUrl(), String.join("/", taginfo.getIsoCodes())
    377                             + (taginfo.getSuffix() == null ? "" : " (" + taginfo.getSuffix() + ")")))
    378             );
     373            final LatLon center = newSel.iterator().next().getBBox().getCenter();
     374            Territories.getRegionalTaginfoUrls(center)
     375                    .map(taginfo -> new TaginfoAction(
     376                            tagTable, editHelper::getDataKey, editHelper::getDataValues,
     377                            membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), taginfo.getUrl(),
     378                            String.join("/", taginfo.getIsoCodes()) + (taginfo.getSuffix() == null ? "" : " (" + taginfo.getSuffix() + ")"))
     379                    ).forEach(taginfoNationalActions::add);
    379380            taginfoNationalActions.stream().map(membershipMenu::add).forEach(membershipMenuTagInfoNatItems::add);
    380381            taginfoNationalActions.stream().map(tagMenu::add).forEach(tagMenuTagInfoNatItems::add);
  • trunk/src/org/openstreetmap/josm/tools/Territories.java

    r15876 r15908  
    77import java.io.InputStream;
    88import java.util.ArrayList;
    9 import java.util.Arrays;
    109import java.util.Collection;
    1110import java.util.Collections;
    1211import java.util.HashMap;
    13 import java.util.List;
    1412import java.util.Locale;
    1513import java.util.Map;
    1614import java.util.Map.Entry;
    17 import java.util.Optional;
     15import java.util.Objects;
    1816import java.util.Set;
    1917import java.util.TreeMap;
    2018import java.util.stream.Collectors;
     19import java.util.stream.Stream;
    2120
    2221import javax.json.Json;
     
    199198
    200199    /**
    201      * Returns a map of regional taginfo instances for the given location.
     200     * Returns regional taginfo instances for the given location.
    202201     * @param ll lat/lon where to look.
    203      * @return a map of regional taginfo instances for the given location (code / url)
     202     * @return regional taginfo instances for the given location (code / url)
    204203     * @since 15876
    205204     */
    206     public static Map<String, List<TaginfoRegionalInstance>> getRegionalTaginfoUrls(LatLon ll) {
    207         Map<String, List<TaginfoRegionalInstance>> result = new TreeMap<>();
    208         if (iso3166Cache != null) {
    209             for (String code : iso3166Cache.entrySet().parallelStream().distinct()
     205    public static Stream<TaginfoRegionalInstance> getRegionalTaginfoUrls(LatLon ll) {
     206        if (iso3166Cache == null) {
     207            return Stream.empty();
     208        }
     209        return iso3166Cache.entrySet().parallelStream().distinct()
    210210                .filter(e -> Boolean.TRUE.equals(e.getValue().get(ll)))
    211211                .map(Entry<String, GeoPropertyIndex<Boolean>>::getKey)
    212                 .collect(Collectors.toSet())) {
    213                 for (Map<String, TaginfoRegionalInstance> cache : Arrays.asList(taginfoCache, taginfoGeofabrikCache)) {
    214                     Optional.ofNullable(cache.get(code)).ifPresent(
    215                             taginfo -> result.computeIfAbsent(code, c -> new ArrayList<>()).add(taginfo));
    216                 }
    217             }
    218         }
    219         return result;
     212                .distinct()
     213                .flatMap(code -> Stream.of(taginfoCache, taginfoGeofabrikCache).map(cache -> cache.get(code)))
     214                .filter(Objects::nonNull);
    220215    }
    221216}
Note: See TracChangeset for help on using the changeset viewer.