Changeset 14654 in josm for trunk/src


Ignore:
Timestamp:
2019-01-06T20:22:48+01:00 (6 years ago)
Author:
simon04
Message:

Deprecate OsmPrimitive.getFilteredList/getFilteredSet in favour of Stream

Most use-cases involved filtering referring primitives. This can now be
accomplished using OsmPrimitive.referrers involving the Stream API and
thus avoids creation of intermediate collections.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java

    r14542 r14654  
    99import java.io.Serializable;
    1010import java.util.ArrayList;
    11 import java.util.Arrays;
    1211import java.util.Collection;
    1312import java.util.Collections;
     
    139138     */
    140139    public static void runOn(DataSet ds) {
    141         Collection<OsmPrimitive> sel = ds.getSelected();
    142         List<Node> nodes = OsmPrimitive.getFilteredList(sel, Node.class);
    143         List<Way> ways = OsmPrimitive.getFilteredList(sel, Way.class);
     140        List<Node> nodes = new ArrayList<>(ds.getSelectedNodes());
     141        Collection<Way> ways = ds.getSelectedWays();
    144142
    145143        Way existingWay = null;
     
    148146        // then use the way's nodes
    149147        if (nodes.isEmpty() && (ways.size() == 1)) {
    150             existingWay = ways.get(0);
     148            existingWay = ways.iterator().next();
    151149            for (Node n : existingWay.getNodes()) {
    152150                if (!nodes.contains(n)) {
     
    203201
    204202        // Order nodes by angle
    205         PolarNode[] angles = new PolarNode[nodes.size()];
    206         for (int i = 0; i < nodes.size(); i++) {
    207             angles[i] = new PolarNode(center, nodes.get(i));
    208         }
    209         Arrays.sort(angles, new PolarNodeComparator());
     203        final PolarNode[] angles = nodes.stream()
     204                .map(n -> new PolarNode(center, n))
     205                .sorted()
     206                .toArray(PolarNode[]::new);
    210207        int[] count = distributeNodes(angles,
    211208                numberOfNodesInCircle >= nodes.size() ? (numberOfNodesInCircle - nodes.size()) : 0);
  • trunk/src/org/openstreetmap/josm/actions/CreateMultipolygonAction.java

    r14302 r14654  
    190190            final Set<Relation> relatedRelations = new HashSet<>();
    191191            for (final Way w : selectedWays) {
    192                 relatedRelations.addAll(Utils.filteredCollection(w.getReferrers(), Relation.class));
     192                w.referrers(Relation.class).forEach(relatedRelations::add);
    193193            }
    194194            return relatedRelations.size() == 1 ? relatedRelations.iterator().next() : null;
  • trunk/src/org/openstreetmap/josm/actions/MergeNodesAction.java

    r14397 r14654  
    1717import java.util.Optional;
    1818import java.util.Set;
     19import java.util.stream.Collectors;
    1920
    2021import javax.swing.JOptionPane;
     
    7576        if (!isEnabled())
    7677            return;
    77         Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getAllSelected();
    78         List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
    79         selectedNodes.removeIf(n -> n.isDeleted() || n.isIncomplete());
     78        final List<Node> selectedNodes = getLayerManager().getEditDataSet().getSelectedNodes().stream()
     79                .filter(n -> !n.isDeleted() && !n.isIncomplete())
     80                .collect(Collectors.toList());
    8081
    8182        if (selectedNodes.size() == 1) {
     
    203204        Set<Way> waysToDelete = new HashSet<>();
    204205
    205         for (Way w: OsmPrimitive.getFilteredList(OsmPrimitive.getReferrer(nodesToDelete), Way.class)) {
     206        for (Way w: (Iterable<Way>) nodesToDelete.stream().flatMap(p -> p.referrers(Way.class))::iterator) {
    206207            List<Node> newNodes = new ArrayList<>(w.getNodesCount());
    207208            for (Node n: w.getNodes()) {
  • trunk/src/org/openstreetmap/josm/actions/SelectNonBranchingWaySequences.java

    r12048 r14654  
    8989        Way foundWay = null;
    9090
    91         for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
    92                 Way.class)) {
     91        for (Way way : node.getParentWays()) {
    9392            if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
    9493                    || !way.isSelectable()
  • trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java

    r14397 r14654  
    1616import java.util.List;
    1717import java.util.Set;
     18import java.util.stream.Collectors;
    1819
    1920import javax.swing.JOptionPane;
     
    9899        ds.beginUpdate();
    99100        try {
    100             List<Way> ways = OsmPrimitive.getFilteredList(ds.getSelected(), Way.class);
    101             ways.removeIf(OsmPrimitive::isIncomplete);
     101            List<Way> ways = ds.getSelectedWays().stream()
     102                    .filter(p -> !p.isIncomplete())
     103                    .collect(Collectors.toList());
    102104            if (ways.isEmpty()) {
    103105                alertSelectAtLeastOneWay();
  • trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java

    r14542 r14654  
    3232import org.openstreetmap.josm.data.osm.OsmUtils;
    3333import org.openstreetmap.josm.data.osm.PrimitiveId;
    34 import org.openstreetmap.josm.data.osm.Relation;
    3534import org.openstreetmap.josm.data.osm.Way;
    3635import org.openstreetmap.josm.data.osm.WaySegment;
     
    8382        }
    8483
    85         Collection<OsmPrimitive> selection = ds.getSelected();
    86 
    87         List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
    88         List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
     84        List<Node> selectedNodes = new ArrayList<>(ds.getSelectedNodes());
     85        List<Way> selectedWays = new ArrayList<>(ds.getSelectedWays());
    8986        List<Way> applicableWays = getApplicableWays(selectedWays, selectedNodes);
    9087
     
    139136        final List<List<Node>> wayChunks = SplitWayCommand.buildSplitChunks(selectedWay, selectedNodes);
    140137        if (wayChunks != null) {
    141             List<Relation> selectedRelations = OsmPrimitive.getFilteredList(selection, Relation.class);
    142             final List<OsmPrimitive> sel = new ArrayList<>(selectedWays.size() + selectedRelations.size());
     138            final List<OsmPrimitive> sel = new ArrayList<>(ds.getSelectedRelations());
    143139            sel.addAll(selectedWays);
    144             sel.addAll(selectedRelations);
    145140
    146141            final List<Way> newWays = SplitWayCommand.createNewWaysFromChunks(selectedWay, wayChunks);
  • trunk/src/org/openstreetmap/josm/actions/UnGlueAction.java

    r14653 r14654  
    390390        }
    391391        // modify all relations containing the node
    392         for (Relation r : OsmPrimitive.getFilteredList(originalNode.getReferrers(), Relation.class)) {
     392        for (Relation r : OsmPrimitive.getParentRelations(Collections.singleton(originalNode))) {
    393393            if (r.isDeleted()) {
    394394                continue;
  • trunk/src/org/openstreetmap/josm/actions/UnJoinNodeWayAction.java

    r14397 r14654  
    1717import org.openstreetmap.josm.command.RemoveNodesCommand;
    1818import org.openstreetmap.josm.data.UndoRedoHandler;
     19import org.openstreetmap.josm.data.osm.DataSet;
    1920import org.openstreetmap.josm.data.osm.Node;
    2021import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    4647    public void actionPerformed(ActionEvent e) {
    4748
    48         Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
    49 
    50         List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
    51         List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
     49        final DataSet dataSet = getLayerManager().getEditDataSet();
     50        List<Node> selectedNodes = new ArrayList<>(dataSet.getSelectedNodes());
     51        List<Way> selectedWays = new ArrayList<>(dataSet.getSelectedWays());
    5252
    5353        selectedNodes = cleanSelectedNodes(selectedWays, selectedNodes);
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r14510 r14654  
    10241024    public static Way getWayForNode(Node n) {
    10251025        Way way = null;
    1026         for (Way w : Utils.filteredCollection(n.getReferrers(), Way.class)) {
     1026        for (Way w : (Iterable<Way>) n.referrers(Way.class)::iterator) {
    10271027            if (!w.isUsable() || w.getNodesCount() < 1) {
    10281028                continue;
    10291029            }
    1030             Node firstNode = w.getNode(0);
    1031             Node lastNode = w.getNode(w.getNodesCount() - 1);
     1030            Node firstNode = w.firstNode();
     1031            Node lastNode = w.lastNode();
    10321032            if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
    10331033                if (way != null)
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ImproveWayAccuracyAction.java

    r14153 r14654  
    1818import java.util.LinkedList;
    1919import java.util.List;
     20import java.util.stream.Collectors;
    2021
    2122import javax.swing.JOptionPane;
     
    432433                virtualCmds.add(new AddCommand(ds, virtualNode));
    433434
    434                 // Looking for candidateSegment copies in ways that are
    435                 // referenced
    436                 // by candidateSegment nodes
    437                 List<Way> firstNodeWays = OsmPrimitive.getFilteredList(
    438                         candidateSegment.getFirstNode().getReferrers(),
    439                         Way.class);
    440                 List<Way> secondNodeWays = OsmPrimitive.getFilteredList(
    441                         candidateSegment.getFirstNode().getReferrers(),
    442                         Way.class);
     435                // Looking for candidateSegment copies in ways that are referenced by candidateSegment nodes
     436                List<Way> firstNodeWays = candidateSegment.getFirstNode().referrers(Way.class).collect(Collectors.toList());
     437                List<Way> secondNodeWays = candidateSegment.getFirstNode().referrers(Way.class).collect(Collectors.toList());
    443438
    444439                Collection<WaySegment> virtualSegments = new LinkedList<>();
     
    484479
    485480                //check to see if node is in use by more than one object
    486                 List<OsmPrimitive> referrers = candidateNode.getReferrers();
    487                 List<Way> ways = OsmPrimitive.getFilteredList(referrers, Way.class);
    488                 if (referrers.size() != 1 || ways.size() != 1) {
     481                long referrersCount = candidateNode.referrers(OsmPrimitive.class).count();
     482                long referrerWayCount = candidateNode.referrers(Way.class).count();
     483                if (referrersCount != 1 || referrerWayCount != 1) {
    489484                    // detach node from way
    490485                    final Way newWay = new Way(targetWay);
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r14397 r14654  
    5959import org.openstreetmap.josm.tools.PlatformManager;
    6060import org.openstreetmap.josm.tools.Shortcut;
    61 import org.openstreetmap.josm.tools.Utils;
    6261
    6362/**
     
    884883    private boolean movesHiddenWay() {
    885884        DataSet ds = getLayerManager().getEditDataSet();
    886         final Collection<OsmPrimitive> elementsToTest = new HashSet<>(ds.getSelected());
     885        final Collection<Node> elementsToTest = new HashSet<>(ds.getSelectedNodes());
    887886        for (Way osm : ds.getSelectedWays()) {
    888887            elementsToTest.addAll(osm.getNodes());
    889888        }
    890         for (OsmPrimitive node : Utils.filteredCollection(elementsToTest, Node.class)) {
    891             for (Way ref : Utils.filteredCollection(node.getReferrers(), Way.class)) {
    892                 if (ref.isDisabledAndHidden()) {
    893                     return true;
    894                 }
    895             }
    896         }
    897         return false;
     889        return elementsToTest.stream()
     890                .flatMap(n -> n.referrers(Way.class))
     891                .anyMatch(Way::isDisabledAndHidden);
    898892    }
    899893
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r13173 r14654  
    1919import java.util.Objects;
    2020import java.util.Set;
     21import java.util.stream.Collectors;
    2122
    2223import javax.swing.Icon;
     
    361362    protected static Collection<Node> computeNodesToDelete(Collection<OsmPrimitive> primitivesToDelete) {
    362363        Collection<Node> nodesToDelete = new HashSet<>();
    363         for (Way way : OsmPrimitive.getFilteredList(primitivesToDelete, Way.class)) {
     364        for (Way way : Utils.filteredCollection(primitivesToDelete, Way.class)) {
    364365            for (Node n : way.getNodes()) {
    365366                if (n.isTagged()) {
     
    435436            return null;
    436437
    437         Collection<Way> waysToBeChanged = new HashSet<>(OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Way.class));
     438        Collection<Way> waysToBeChanged = primitivesToDelete.stream()
     439                .flatMap(p -> p.referrers(Way.class))
     440                .collect(Collectors.toSet());
    438441
    439442        Collection<Command> cmds = new LinkedList<>();
    440443        for (Way w : waysToBeChanged) {
    441444            Way wnew = new Way(w);
    442             wnew.removeNodes(OsmPrimitive.getFilteredSet(primitivesToDelete, Node.class));
     445            wnew.removeNodes(new HashSet<>(Utils.filteredCollection(primitivesToDelete, Node.class)));
    443446            if (wnew.getNodesCount() < 2) {
    444447                primitivesToDelete.add(w);
     
    460463        // remove the objects from their parent relations
    461464        //
    462         for (Relation cur : OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Relation.class)) {
     465        final Set<Relation> relationsToBeChanged = primitivesToDelete.stream()
     466                .flatMap(p -> p.referrers(Relation.class))
     467                .collect(Collectors.toSet());
     468        for (Relation cur : relationsToBeChanged) {
    463469            Relation rel = new Relation(cur);
    464470            rel.removeMembersFor(primitivesToDelete);
  • trunk/src/org/openstreetmap/josm/command/SplitWayCommand.java

    r14143 r14654  
    88import java.util.Arrays;
    99import java.util.Collection;
     10import java.util.Collections;
    1011import java.util.HashSet;
    1112import java.util.Iterator;
     
    297298        // now copy all relations to new way also
    298299
    299         for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) {
     300        for (Relation r : OsmPrimitive.getParentRelations(Collections.singleton(way))) {
    300301            if (!r.isUsable()) {
    301302                continue;
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r14120 r14654  
    99import java.util.TreeSet;
    1010import java.util.function.Predicate;
     11import java.util.stream.Collectors;
    1112
    1213import org.openstreetmap.josm.data.coor.EastNorth;
     
    1718import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    1819import org.openstreetmap.josm.tools.CheckParameterUtil;
    19 import org.openstreetmap.josm.tools.Utils;
    2020
    2121/**
     
    370370        if (hops > 0 && visited != null) {
    371371            visited.add(this);
    372             for (final Way w : Utils.filteredCollection(this.getReferrers(), Way.class)) {
     372            for (final Way w : getParentWays()) {
    373373                for (final Node n : w.getNodes()) {
    374374                    final boolean containsN = visited.contains(n);
     
    401401     */
    402402    public List<Way> getParentWays() {
    403         return getFilteredList(getReferrers(), Way.class);
     403        return referrers(Way.class).collect(Collectors.toList());
    404404    }
    405405}
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r14003 r14654  
    66import java.text.MessageFormat;
    77import java.util.ArrayList;
     8import java.util.Arrays;
    89import java.util.Collection;
    910import java.util.Collections;
    1011import java.util.Date;
    11 import java.util.HashSet;
    1212import java.util.LinkedHashSet;
    13 import java.util.LinkedList;
    1413import java.util.List;
    1514import java.util.Locale;
     
    1817import java.util.Set;
    1918import java.util.function.Consumer;
     19import java.util.stream.Collectors;
     20import java.util.stream.Stream;
    2021
    2122import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     
    9192     * @param type the type to filter for
    9293     * @return the sub-list of OSM primitives of type <code>type</code>
    93      */
     94     * @deprecated Use {@link Stream} or {@link Utils#filteredCollection(Collection, Class)} instead.
     95     */
     96    @Deprecated
    9497    public static <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
    95         if (list == null) return Collections.emptyList();
    96         List<T> ret = new LinkedList<>();
    97         for (OsmPrimitive p: list) {
    98             if (type.isInstance(p)) {
    99                 ret.add(type.cast(p));
    100             }
    101         }
    102         return ret;
     98        return (list != null ? list.stream() : Stream.empty())
     99                .filter(type::isInstance)
     100                .map(type::cast)
     101                .collect(Collectors.toList());
    103102    }
    104103
     
    113112     * @param type the type to filter for
    114113     * @return the sub-set of OSM primitives of type <code>type</code>
    115      */
     114     * @deprecated Use {@link Stream} instead
     115     */
     116    @Deprecated
    116117    public static <T extends OsmPrimitive> Set<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
    117         Set<T> ret = new LinkedHashSet<>();
    118         if (set != null) {
    119             for (OsmPrimitive p: set) {
    120                 if (type.isInstance(p)) {
    121                     ret.add(type.cast(p));
    122                 }
    123             }
    124         }
    125         return ret;
     118        return (set != null ? set.stream() : Stream.empty())
     119                .filter(type::isInstance)
     120                .map(type::cast)
     121                .collect(Collectors.toCollection(LinkedHashSet::new));
    126122    }
    127123
     
    134130     */
    135131    public static Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives) {
    136         Set<OsmPrimitive> ret = new HashSet<>();
    137         if (primitives == null || primitives.isEmpty()) return ret;
    138         for (OsmPrimitive p: primitives) {
    139             ret.addAll(p.getReferrers());
    140         }
    141         return ret;
     132        return (primitives != null ? primitives.stream() : Stream.<OsmPrimitive>empty())
     133                .flatMap(p -> p.referrers(OsmPrimitive.class))
     134                .collect(Collectors.toSet());
    142135    }
    143136
     
    816809    }
    817810
    818     @Override
    819     public final List<OsmPrimitive> getReferrers(boolean allowWithoutDataset) {
     811    private <T extends OsmPrimitive> Stream<T> referrers(boolean allowWithoutDataset, Class<T> filter) {
    820812        // Returns only referrers that are members of the same dataset (primitive can have some fake references, for example
    821813        // when way is cloned
    822814
    823         if (dataSet == null && allowWithoutDataset)
    824             return Collections.emptyList();
    825 
     815        if (dataSet == null && allowWithoutDataset) {
     816            return Stream.empty();
     817        }
    826818        checkDataset();
    827         Object referrers = this.referrers;
    828         List<OsmPrimitive> result = new ArrayList<>();
    829         if (referrers != null) {
    830             if (referrers instanceof OsmPrimitive) {
    831                 OsmPrimitive ref = (OsmPrimitive) referrers;
    832                 if (ref.dataSet == dataSet) {
    833                     result.add(ref);
    834                 }
    835             } else {
    836                 for (OsmPrimitive o:(OsmPrimitive[]) referrers) {
    837                     if (dataSet == o.dataSet) {
    838                         result.add(o);
    839                     }
    840                 }
    841             }
    842         }
    843         return result;
     819        final Object referrers = this.referrers;
     820        if (referrers == null) {
     821            return Stream.empty();
     822        }
     823        final Stream<OsmPrimitive> stream = referrers instanceof OsmPrimitive
     824                ? Stream.of((OsmPrimitive) referrers)
     825                : Arrays.stream((OsmPrimitive[]) referrers);
     826        return stream
     827                .filter(p -> p.dataSet == dataSet)
     828                .filter(filter::isInstance)
     829                .map(filter::cast);
     830    }
     831
     832    /**
     833     * Gets all primitives in the current dataset that reference this primitive.
     834     * @param filter restrict primitives to subclasses
     835     * @param <T> type of primitives
     836     * @return the referrers as Stream
     837     * @since 14654
     838     */
     839    public final <T extends OsmPrimitive> Stream<T> referrers(Class<T> filter) {
     840        return referrers(false, filter);
     841    }
     842
     843    @Override
     844    public final List<OsmPrimitive> getReferrers(boolean allowWithoutDataset) {
     845        return referrers(allowWithoutDataset, OsmPrimitive.class)
     846                .collect(Collectors.toList());
    844847    }
    845848
     
    11591162     */
    11601163    public static Set<Relation> getParentRelations(Collection<? extends OsmPrimitive> primitives) {
    1161         Set<Relation> ret = new HashSet<>();
    1162         for (OsmPrimitive w : primitives) {
    1163             ret.addAll(OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class));
    1164         }
    1165         return ret;
     1164        return primitives.stream()
     1165                .flatMap(p -> p.referrers(Relation.class))
     1166                .collect(Collectors.toSet());
    11661167    }
    11671168
  • trunk/src/org/openstreetmap/josm/data/osm/RelationToChildReference.java

    r12190 r14654  
    66import java.util.Objects;
    77import java.util.Set;
     8import java.util.stream.Collectors;
    89
    910/**
     
    1920     */
    2021    public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
    21         Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
     22        Set<Relation> parents = child.referrers(Relation.class).collect(Collectors.toSet());
    2223        Set<RelationToChildReference> references = new HashSet<>();
    2324        for (Relation parent: parents) {
  • trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java

    r14561 r14654  
    4848import org.openstreetmap.josm.tools.Logging;
    4949import org.openstreetmap.josm.tools.UncheckedParseException;
    50 import org.openstreetmap.josm.tools.Utils;
    5150import org.openstreetmap.josm.tools.date.DateUtils;
    5251
     
    14731472        protected Long getNumber(OsmPrimitive osm) {
    14741473            if (osm instanceof Node) {
    1475                 return (long) Utils.filteredCollection(osm.getReferrers(), Way.class).size();
     1474                return osm.referrers(Way.class).count();
    14761475            } else if (osm instanceof Relation) {
    14771476                return (long) ((Relation) osm).getMemberPrimitives(Way.class).size();
  • trunk/src/org/openstreetmap/josm/data/validation/tests/Addresses.java

    r14273 r14654  
    7676
    7777    protected List<Relation> getAndCheckAssociatedStreets(OsmPrimitive p) {
    78         List<Relation> list = OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class);
    79         list.removeIf(r -> !r.hasTag("type", ASSOCIATED_STREET));
     78        final List<Relation> list = p.referrers(Relation.class)
     79                .filter(r -> r.hasTag("type", ASSOCIATED_STREET))
     80                .collect(Collectors.toList());
    8081        if (list.size() > 1) {
    8182            Severity level;
     
    107108                }
    108109            }
    109             for (Way w : OsmPrimitive.getFilteredList(p.getReferrers(), Way.class)) {
    110                 if (w.hasKey(ADDR_INTERPOLATION) && w.hasKey(ADDR_STREET)) {
    111                     return;
    112                 }
     110            if (p.referrers(Way.class).anyMatch(w -> w.hasKey(ADDR_INTERPOLATION) && w.hasKey(ADDR_STREET))) {
     111                return;
    113112            }
    114113            // No street found
  • trunk/src/org/openstreetmap/josm/data/validation/tests/DuplicateNode.java

    r12846 r14654  
    88
    99import java.util.ArrayList;
    10 import java.util.Collection;
    1110import java.util.Collections;
    1211import java.util.HashMap;
     
    1413import java.util.Iterator;
    1514import java.util.LinkedHashSet;
    16 import java.util.LinkedList;
    1715import java.util.List;
    1816import java.util.Map;
    1917import java.util.Map.Entry;
    2018import java.util.Set;
     19import java.util.stream.Collectors;
    2120
    2221import org.openstreetmap.josm.actions.MergeNodesAction;
    2322import org.openstreetmap.josm.command.Command;
    2423import org.openstreetmap.josm.data.coor.LatLon;
    25 import org.openstreetmap.josm.data.osm.AbstractPrimitive;
    2624import org.openstreetmap.josm.data.osm.Hash;
    2725import org.openstreetmap.josm.data.osm.Node;
     
    312310    @Override
    313311    public Command fixError(TestError testError) {
    314         Collection<OsmPrimitive> sel = new LinkedList<>(testError.getPrimitives());
    315         Set<Node> nodes = new LinkedHashSet<>(OsmPrimitive.getFilteredList(sel, Node.class));
    316 
    317         // Filter nodes that have already been deleted (see #5764 and #5773)
    318         nodes.removeIf(AbstractPrimitive::isDeleted);
     312        final Set<Node> nodes = testError.getPrimitives().stream()
     313                .filter(Node.class::isInstance)
     314                .map(Node.class::cast)
     315                // Filter nodes that have already been deleted (see #5764 and #5773)
     316                .filter(n -> !n.isDeleted())
     317                .collect(Collectors.toCollection(LinkedHashSet::new));
    319318
    320319        // Merge only if at least 2 nodes remain
  • trunk/src/org/openstreetmap/josm/data/validation/tests/DuplicateRelation.java

    r13809 r14654  
    1212import java.util.Objects;
    1313import java.util.Set;
     14import java.util.stream.Collectors;
    1415
    1516import org.openstreetmap.josm.command.ChangeCommand;
     
    261262        // Find the relation that is member of one or more relations. (If any)
    262263        Relation relationWithRelations = null;
    263         List<Relation> relRef = null;
     264        Collection<Relation> relRef = null;
    264265        for (Relation w : relFix) {
    265             List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
     266            Collection<Relation> rel = w.referrers(Relation.class).collect(Collectors.toList());
    266267            if (!rel.isEmpty()) {
    267268                if (relationWithRelations != null)
     
    318319            return false;
    319320
    320         int relationsWithRelations = 0;
    321         for (Relation w : rels) {
    322             List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
    323             if (!rel.isEmpty()) {
    324                 ++relationsWithRelations;
    325             }
    326         }
    327         return relationsWithRelations <= 1;
     321        // count relations with relations
     322        return rels.stream()
     323                .filter(x -> x.referrers(Relation.class).anyMatch(y -> true))
     324                .limit(2)
     325                .count() <= 1;
    328326    }
    329327}
  • trunk/src/org/openstreetmap/josm/data/validation/tests/DuplicateWay.java

    r13809 r14654  
    1313import java.util.Objects;
    1414import java.util.Set;
     15import java.util.stream.Collectors;
    1516
    1617import org.openstreetmap.josm.command.ChangeCommand;
     
    267268        List<Relation> relations = null;
    268269        for (Way w : wayz) {
    269             List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
     270            List<Relation> rel = w.referrers(Relation.class).collect(Collectors.toList());
    270271            if (!rel.isEmpty()) {
    271272                if (wayWithRelations != null)
     
    328329        int waysWithRelations = 0;
    329330        for (Way w : wayz) {
    330             List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
     331            List<Relation> rel = w.referrers(Relation.class).collect(Collectors.toList());
    331332            if (!rel.isEmpty()) {
    332333                ++waysWithRelations;
  • trunk/src/org/openstreetmap/josm/data/validation/tests/Highways.java

    r14198 r14654  
    115115        // As roundabouts are closed ways, take care of not processing the first/last node twice
    116116        for (Node n : new HashSet<>(w.getNodes())) {
    117             for (Way h : Utils.filteredCollection(n.getReferrers(), Way.class)) {
     117            for (Way h : (Iterable<Way>) n.referrers(Way.class)::iterator) {
    118118                String value = h.get(HIGHWAY);
    119119                if (h != w && value != null) {
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OverlappingWays.java

    r14467 r14654  
    6969
    7070    private static boolean parentMultipolygonConcernsArea(OsmPrimitive p) {
    71         for (Relation r : OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class)) {
    72             if (r.concernsArea()) {
    73                 return true;
    74             }
    75         }
    76         return false;
     71        return p.referrers(Relation.class)
     72                .anyMatch(Relation::concernsArea);
    7773    }
    7874
  • trunk/src/org/openstreetmap/josm/gui/dialogs/PropertiesMembershipChoiceDialog.java

    r14653 r14654  
    66import java.awt.GridBagLayout;
    77import java.util.Collection;
     8import java.util.Objects;
    89
    910import javax.swing.AbstractButton;
     
    154155    private static boolean isUsedInRelations(final Collection<Node> existingNodes) {
    155156        return existingNodes.stream().anyMatch(
    156                 selectedNode -> selectedNode.getReferrers().stream().anyMatch(Relation.class::isInstance));
     157                selectedNode -> selectedNode.referrers(Relation.class).anyMatch(Objects::nonNull));
    157158    }
    158159}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSorter.java

    r12865 r14654  
    1717import org.openstreetmap.josm.data.osm.RelationMember;
    1818import org.openstreetmap.josm.tools.AlphanumComparator;
    19 import org.openstreetmap.josm.tools.Utils;
    2019
    2120/**
     
    9695
    9796        private static String getStopName(OsmPrimitive p) {
    98             for (Relation ref : Utils.filteredCollection(p.getReferrers(), Relation.class)) {
    99                 if (ref.hasTag("type", "public_transport") && ref.hasTag("public_transport", "stop_area") && ref.getName() != null) {
    100                     return ref.getName();
    101                 }
    102             }
    103             return p.getName();
     97            return p.referrers(Relation.class)
     98                    .filter(ref -> ref.hasTag("type", "public_transport")
     99                            && ref.hasTag("public_transport", "stop_area")
     100                            && ref.getName() != null)
     101                    .map(Relation::getName)
     102                    .findFirst()
     103                    .orElse(p.getName());
    104104        }
    105105
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r14466 r14654  
    2424import org.openstreetmap.josm.data.osm.OsmUtils;
    2525import org.openstreetmap.josm.data.osm.Relation;
     26import org.openstreetmap.josm.data.osm.Way;
    2627import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    2728import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
     
    3334import org.openstreetmap.josm.tools.Logging;
    3435import org.openstreetmap.josm.tools.Pair;
    35 import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    3636import org.openstreetmap.josm.tools.Utils;
    3737
     
    354354                    // if right selector also matches relations and if matched primitive is a way which is part of a multipolygon,
    355355                    // use the multipolygon for further analysis
    356                     if (!(e.osm instanceof IWay)
     356                    if (!(e.osm instanceof Way)
    357357                            || (right instanceof OptimizedGeneralSelector
    358358                            && !((OptimizedGeneralSelector) right).matchesBase(OsmPrimitiveType.RELATION))) {
    359359                        throw new NoSuchElementException();
    360360                    }
    361                     final Collection<Relation> multipolygons = Utils.filteredCollection(SubclassFilteredCollection.filter(
    362                             e.osm.getReferrers(), p -> p.hasTag("type", "multipolygon")), Relation.class);
    363                     final Relation multipolygon = multipolygons.iterator().next();
    364                     if (multipolygon == null) throw new NoSuchElementException();
     361                    final Relation multipolygon = ((Way) e.osm).referrers(Relation.class)
     362                            .filter(p -> p.hasTag("type", "multipolygon"))
     363                            .findFirst()
     364                            .orElseThrow(NoSuchElementException::new);
    365365                    final Set<OsmPrimitive> members = multipolygon.getMemberPrimitives();
    366366                    containsFinder = new ContainsFinder(new Environment(multipolygon)) {
  • trunk/src/org/openstreetmap/josm/tools/RotationAngle.java

    r13623 r14654  
    22package org.openstreetmap.josm.tools;
    33
     4import java.util.List;
    45import java.util.Locale;
    56
    67import org.openstreetmap.josm.data.osm.IPrimitive;
    78import org.openstreetmap.josm.data.osm.Node;
    8 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    99import org.openstreetmap.josm.data.osm.Way;
    1010
     
    2828            }
    2929            final Node n = (Node) p;
    30             final SubclassFilteredCollection<OsmPrimitive, Way> ways = Utils.filteredCollection(n.getReferrers(), Way.class);
     30            final List<Way> ways = n.getParentWays();
    3131            if (ways.isEmpty()) {
    3232                return 0;
Note: See TracChangeset for help on using the changeset viewer.