Changeset 11175 in josm for trunk/src


Ignore:
Timestamp:
2016-10-27T00:32:44+02:00 (3 years ago)
Author:
simon04
Message:

APIDataSet: simplify implementation using streams, add test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/APIDataSet.java

    r10647 r11175  
    1212import java.util.Set;
    1313import java.util.Stack;
     14import java.util.stream.Collectors;
     15import java.util.stream.Stream;
    1416
    1517import org.openstreetmap.josm.actions.upload.CyclicUploadDependencyException;
     
    5759    }
    5860
     61    /**
     62     * Initializes the API data set with the modified primitives, ignores unmodified primitives.
     63     *
     64     * @param primitives the primitives
     65     */
    5966    public final void init(Collection<OsmPrimitive> primitives) {
    6067        toAdd.clear();
     
    8996    /**
    9097     * Replies true if one of the primitives to be updated or to be deleted
    91      * participates in the conflict <code>conflict</code>
    92      *
    93      * @param conflict the conflict
    94      * @return true if one of the primitives to be updated or to be deleted
    95      * participates in the conflict <code>conflict</code>
    96      */
    97     public boolean participatesInConflict(Conflict<?> conflict) {
    98         if (conflict == null) return false;
    99         for (OsmPrimitive p: toUpdate) {
    100             if (conflict.isParticipating(p)) return true;
    101         }
    102         for (OsmPrimitive p: toDelete) {
    103             if (conflict.isParticipating(p)) return true;
    104         }
    105         return false;
    106     }
    107 
    108     /**
    109      * Replies true if one of the primitives to be updated or to be deleted
    11098     * participates in at least one conflict in <code>conflicts</code>
    11199     *
     
    116104    public boolean participatesInConflict(ConflictCollection conflicts) {
    117105        if (conflicts == null || conflicts.isEmpty()) return false;
    118         Set<PrimitiveId> idsParticipatingInConflicts = new HashSet<>();
    119         for (OsmPrimitive p: conflicts.getMyConflictParties()) {
    120             idsParticipatingInConflicts.add(p.getPrimitiveId());
    121         }
    122         for (OsmPrimitive p: conflicts.getTheirConflictParties()) {
    123             idsParticipatingInConflicts.add(p.getPrimitiveId());
    124         }
    125         for (OsmPrimitive p: toUpdate) {
    126             if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
    127         }
    128         for (OsmPrimitive p: toDelete) {
    129             if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
    130         }
    131         return false;
     106        Set<PrimitiveId> idsParticipatingInConflicts = conflicts.get().stream()
     107                .flatMap(c -> Stream.of(c.getMy(), c.getTheir()))
     108                .map(OsmPrimitive::getPrimitiveId)
     109                .collect(Collectors.toSet());
     110        return Stream.of(toUpdate, toDelete)
     111                .flatMap(Collection::stream)
     112                .map(OsmPrimitive::getPrimitiveId)
     113                .anyMatch(idsParticipatingInConflicts::contains);
    132114    }
    133115
Note: See TracChangeset for help on using the changeset viewer.