All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jhotdraw8.icollection.facade.MutableSetFacade Maven / Gradle / Ivy

/*
 * @(#)MutableSetFacade.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.icollection.facade;

import org.jhotdraw8.icollection.immutable.ImmutableSet;
import org.jhotdraw8.icollection.impl.iteration.FailFastIterator;
import org.jhotdraw8.icollection.impl.iteration.FailFastSpliterator;
import org.jhotdraw8.icollection.readonly.ReadOnlySet;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.stream.Stream;

/**
 * Provides a {@link Set} facade to a set of {@code ImmutableSet} functions.
 *
 * @param  the element type
 * @author Werner Randelshofer
 */
public class MutableSetFacade extends AbstractSet implements ReadOnlySet {
    private ImmutableSet backingSet;
    private int modCount;

    public MutableSetFacade(ImmutableSet backingSet) {
        this.backingSet = backingSet;
    }


    @SuppressWarnings("unchecked")
    @Override
    public boolean remove(Object o) {
        ImmutableSet oldSet = backingSet;
        backingSet = backingSet.remove((E) o);
        if (oldSet != backingSet) {
            modCount++;
            return true;
        }
        return false;
    }

    @Override
    public void clear() {
        ImmutableSet oldSet = backingSet;
        backingSet = backingSet.empty();
        if (oldSet != backingSet) {
            modCount++;
        }
    }

    @Override
    public Spliterator spliterator() {
        return new FailFastSpliterator<>(backingSet.spliterator(), () -> this.modCount, null);
    }

    @Override
    public Stream stream() {
        return backingSet.stream();
    }

    @Override
    public Iterator iterator() {
        Iterator it = new Iterator<>() {
            final Iterator b = backingSet.iterator();
            E current;
            boolean canRemove;

            @Override
            public boolean hasNext() {
                return b.hasNext();
            }

            @Override
            public E next() {
                current = b.next();
                canRemove = true;
                return current;
            }

            @Override
            public void remove() {
                if (canRemove) {
                    MutableSetFacade.this.remove(current);
                    canRemove = false;
                } else {
                    throw new IllegalStateException();
                }
            }
        };

        return new FailFastIterator<>(it, () -> this.modCount);
    }


    @Override
    public int size() {
        return backingSet.size();
    }

    @Override
    public boolean contains(Object o) {
        return backingSet.contains(o);
    }

    @Override
    public boolean add(E e) {
        ImmutableSet oldSet = backingSet;
        backingSet = backingSet.add(e);
        if (oldSet != backingSet) {
            modCount++;
            return true;
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy