Changeset 15832 in josm


Ignore:
Timestamp:
2020-02-10T20:46:11+01:00 (4 years ago)
Author:
simon04
Message:

ConflictCollection: use Java 8 features

File:
1 edited

Legend:

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

    r15295 r15832  
    66import java.util.ArrayList;
    77import java.util.Collection;
    8 import java.util.HashSet;
    98import java.util.Iterator;
    109import java.util.List;
     
    1211import java.util.Set;
    1312import java.util.concurrent.CopyOnWriteArrayList;
     13import java.util.stream.Collectors;
    1414
    1515import org.openstreetmap.josm.data.osm.Node;
     
    6969
    7070    protected void fireConflictAdded() {
    71         for (IConflictListener listener : listeners) {
    72             listener.onConflictsAdded(this);
    73         }
     71        listeners.forEach(listener -> listener.onConflictsAdded(this));
    7472    }
    7573
    7674    protected void fireConflictRemoved() {
    77         for (IConflictListener listener : listeners) {
    78             listener.onConflictsRemoved(this);
    79         }
     75        listeners.forEach(listener -> listener.onConflictsRemoved(this));
    8076    }
    8177
     
    114110    public void add(Collection<Conflict<?>> otherConflicts) {
    115111        if (otherConflicts == null) return;
    116         for (Conflict<?> c : otherConflicts) {
    117             addConflict(c);
    118         }
     112        otherConflicts.forEach(this::addConflict);
    119113        fireConflictAdded();
    120114    }
     
    146140     *
    147141     * @param my  the primitive
    148      */
     142     * @deprecated use {@link #removeForMy(OsmPrimitive)}
     143     */
     144    @Deprecated
    149145    public void remove(OsmPrimitive my) {
    150         Iterator<Conflict<?>> it = iterator();
    151         while (it.hasNext()) {
    152             if (it.next().isMatchingMy(my)) {
    153                 it.remove();
    154             }
    155         }
    156         fireConflictRemoved();
     146        removeForMy(my);
    157147    }
    158148
     
    166156     */
    167157    public Conflict<?> getConflictForMy(OsmPrimitive my) {
    168         for (Conflict<?> c : conflicts) {
    169             if (c.isMatchingMy(my))
    170                 return c;
    171         }
    172         return null;
     158        return conflicts.stream()
     159                .filter(c -> c.isMatchingMy(my))
     160                .findFirst()
     161                .orElse(null);
    173162    }
    174163
     
    182171     */
    183172    public Conflict<?> getConflictForTheir(OsmPrimitive their) {
    184         for (Conflict<?> c : conflicts) {
    185             if (c.isMatchingTheir(their))
    186                 return c;
    187         }
    188         return null;
     173        return conflicts.stream()
     174                .filter(c -> c.isMatchingTheir(their))
     175                .findFirst()
     176                .orElse(null);
    189177    }
    190178
     
    225213     */
    226214    public void removeForMy(OsmPrimitive my) {
    227         Iterator<Conflict<?>> it = iterator();
    228         while (it.hasNext()) {
    229             if (it.next().isMatchingMy(my)) {
    230                 it.remove();
    231             }
     215        if (conflicts.removeIf(c -> c.isMatchingMy(my))) {
     216            fireConflictRemoved();
    232217        }
    233218    }
     
    239224     */
    240225    public void removeForTheir(OsmPrimitive their) {
    241         Iterator<Conflict<?>> it = iterator();
    242         while (it.hasNext()) {
    243             if (it.next().isMatchingTheir(their)) {
    244                 it.remove();
    245             }
     226        if (conflicts.removeIf(c -> c.isMatchingTheir(their))) {
     227            fireConflictRemoved();
    246228        }
    247229    }
     
    290272     */
    291273    public void add(ConflictCollection other) {
    292         for (Conflict<?> c : other) {
    293             if (!hasConflict(c)) {
    294                 add(c);
    295             }
    296         }
     274        other.conflicts.stream()
     275                .filter(c -> !hasConflict(c))
     276                .forEach(this::add);
    297277    }
    298278
     
    305285     */
    306286    public Set<OsmPrimitive> getMyConflictParties() {
    307         Set<OsmPrimitive> ret = new HashSet<>();
    308         for (Conflict<?> c: conflicts) {
    309             ret.add(c.getMy());
    310         }
    311         return ret;
     287        return conflicts.stream()
     288                .map(Conflict::getMy)
     289                .collect(Collectors.toSet());
    312290    }
    313291
     
    320298     */
    321299    public Set<OsmPrimitive> getTheirConflictParties() {
    322         Set<OsmPrimitive> ret = new HashSet<>();
    323         for (Conflict<?> c: conflicts) {
    324             ret.add(c.getTheir());
    325         }
    326         return ret;
     300        return conflicts.stream()
     301                .map(Conflict::getTheir)
     302                .collect(Collectors.toSet());
    327303    }
    328304
Note: See TracChangeset for help on using the changeset viewer.