    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside;
    003
    004import java.util.AbstractSet;
    005import java.util.Collections;
    006import java.util.HashSet;
    007import java.util.Iterator;
    008import java.util.Set;
    009import java.util.concurrent.ConcurrentHashMap;
    010
    011import org.openstreetmap.josm.plugins.streetside.utils.StreetsideChangesetListener;
    012
    013public class StreetsideLocationChangeset extends AbstractSet<StreetsideImage> {
    014  private final Set<StreetsideChangesetListener> listeners = new HashSet<>();
    015  private final Set<StreetsideImage> changeset = Collections.newSetFromMap(new ConcurrentHashMap<>());
    016
    017  public void addChangesetListener(StreetsideChangesetListener listener) {
    018    listeners.add(listener);
    019  }
    020
    021  public void cleanChangeset() {
    022    changeset.clear();
    023    fireListeners();
    024  }
    025
    026  private void fireListeners() {
    027    listeners.forEach(StreetsideChangesetListener::changesetChanged);
    028  }
    029
    030  @Override
    031  public boolean add(StreetsideImage image) {
    032    boolean add = changeset.add(image);
    033    fireListeners();
    034    return add;
    035  }
    036
    037  @Override
    038  public Iterator<StreetsideImage> iterator() {
    039    return changeset.iterator();
    040  }
    041
    042  @Override
    043  public int size() {
    044    return changeset.size();
    045  }
    046
    047  @Override
    048  public boolean remove(Object image) {
    049    boolean remove = changeset.remove(image);
    050    fireListeners();
    051    return remove;
    052  }
    053}