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

personthecat.catlib.data.collections.SimpleObserverSet Maven / Gradle / Ivy

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
package personthecat.catlib.data.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class SimpleObserverSet implements ObserverSet {

    protected final List> tracked;

    public SimpleObserverSet() {
        this.tracked = new ArrayList<>();
    }

    public SimpleObserverSet(final Collection entries) {
        this.tracked = beginTracking(entries);
    }

    private static  List> beginTracking(final Collection entries) {
        final List> tracked = new ArrayList<>();
        for (final O o : entries) {
            tracked.add(new SimpleTrackedEntry<>(o));
        }
        return tracked;
    }

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

    @Override
    public synchronized void add(final O o) {
        this.tracked.add(new SimpleTrackedEntry<>(o));
    }

    @Override
    public boolean contains(final O o) {
        for (final SimpleTrackedEntry entry : this.tracked) {
            if (!entry.isRemoved() && entry.getObserver().equals(o)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public synchronized void remove(final O o) {
        final Iterator> iterator = this.tracked.iterator();
        while (iterator.hasNext()) {
            final SimpleTrackedEntry entry = iterator.next();
            if (entry.getObserver().equals(o)) {
                entry.remove();
                iterator.remove();
                return;
            }
        }
    }

    @Override
    public synchronized void clear() {
        for (final SimpleTrackedEntry entry : this.tracked) {
            entry.remove();
        }
        this.tracked.clear();
    }

    @Override
    public Collection getUntracked() {
        final List untracked = new ArrayList<>();
        for (final SimpleTrackedEntry entry : this.tracked) {
            untracked.add(entry.getObserver());
        }
        return untracked;
    }

    @Override
    public void forEach(final Consumer fn) {
        if (this.tracked.isEmpty()) {
            return;
        }
        for (final SimpleTrackedEntry entry : new ArrayList<>(this.tracked)) {
            if (!entry.isRemoved()) {
                fn.accept(entry.getObserver());
            }
        }
    }

    protected static class SimpleTrackedEntry implements TrackedEntry {
        volatile boolean active = false;
        volatile boolean removed = false;
        final O observer;

        SimpleTrackedEntry(final O observer) {
            this.observer = observer;
        }

        @Override
        public boolean isActive() {
            return this.active;
        }

        protected void setActive(final boolean active) {
            this.active = active;
        }

        @Override
        public boolean isRemoved() {
            return this.removed;
        }

        @Override
        public void remove() {
            this.removed = true;
        }

        @Override
        public O getObserver() {
            return this.observer;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy