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

net.objectlab.kit.collections.ReadOnlyExpiringHashSet Maven / Gradle / Ivy

/**
 *
 */
package net.objectlab.kit.collections;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author Benoit Xhenseval
 *
 */
public class ReadOnlyExpiringHashSet extends AbstractReadOnlyExpiringCollection implements ReadOnlyExpiringSet {
    private final SetLoader loader;
    private Set delegate = new HashSet();

    public ReadOnlyExpiringHashSet(final ReadOnlyExpiringHashSetBuilder builder) {
        this.loader = builder.getLoader();
        setId(builder.getId());
        setExpiryTimeoutMilliseconds(builder.getExpiryTimeoutMilliseconds());
        setReloadOnExpiry(builder.isReloadOnExpiry());
        setLoadOnFirstAccess(builder.isLoadOnFirstAccess());
        setReloadWhenExpired(builder.isReloadWhenExpired());
        setTimeProvider(builder.getTimeProvider());
        start();
    }

    @Override
    protected void doLoad() {
        final DefaultSetBuilder builder = new DefaultSetBuilder(getId());
        loader.load(builder);
        delegate = builder.build();
    }

    @Override
    public boolean add(final T e) {
        validateOnAccess();
        return delegate.add(e);
    }

    @Override
    public boolean addAll(final Collection c) {
        throw new UnsupportedOperationException("Collection is immutable");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("Collection is immutable");
    }

    @Override
    public boolean contains(final Object o) {
        validateOnAccess();
        return delegate.contains(o);
    }

    @Override
    public boolean containsAll(final Collection c) {
        validateOnAccess();
        return delegate.containsAll(c);
    }

    @Override
    public boolean isEmpty() {
        validateOnAccess();
        return delegate.isEmpty();
    }

    @Override
    public Iterator iterator() {
        validateOnAccess();
        return delegate.iterator();
    }

    @Override
    public boolean remove(final Object o) {
        throw new UnsupportedOperationException("Collection is immutable");
    }

    @Override
    public boolean removeAll(final Collection c) {
        throw new UnsupportedOperationException("Collection is immutable");
    }

    @Override
    public boolean retainAll(final Collection c) {
        throw new UnsupportedOperationException("Collection is immutable");
    }

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

    @Override
    public Object[] toArray() {
        validateOnAccess();
        return delegate.toArray();
    }

    @Override
    public  T[] toArray(final T[] a) {
        validateOnAccess();
        return delegate.toArray(a);
    }

    @Override
    protected void doClear() {
        delegate.clear();
    }

    @Override
    public void reload() {
        doLoad();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy