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

net.digitalid.utility.collections.iterator.FreezableIterator Maven / Gradle / Ivy

The newest version!
package net.digitalid.utility.collections.iterator;

import java.util.Iterator;
import java.util.NoSuchElementException;

import javax.annotation.Nonnull;

import net.digitalid.utility.annotations.method.Impure;
import net.digitalid.utility.annotations.method.Pure;
import net.digitalid.utility.annotations.ownership.Captured;
import net.digitalid.utility.annotations.ownership.Shared;
import net.digitalid.utility.collections.collection.FreezableCollection;
import net.digitalid.utility.contracts.Require;
import net.digitalid.utility.generator.annotations.generators.GenerateSubclass;
import net.digitalid.utility.validation.annotations.type.Mutable;

/**
 * This class models an iterator which is backed by a freezable collection.
 */
@Mutable
@GenerateSubclass
public abstract class FreezableIterator implements Iterator {
    
    /* -------------------------------------------------- Iterator -------------------------------------------------- */
    
    @Pure
    abstract @Nonnull Iterator getIterator();
    
    /* -------------------------------------------------- Collection -------------------------------------------------- */
    
    @Pure
    abstract @Nonnull FreezableCollection getCollection();
    
    /* -------------------------------------------------- Constructors -------------------------------------------------- */
    
    /**
     * Returns a new freezable iterator backed by the given iterator and freezable collection.
     */
    @Pure
    public static  @Nonnull FreezableIterator with(@Captured @Nonnull Iterator iterator, @Shared @Nonnull FreezableCollection collection) {
        return new FreezableIteratorSubclass(iterator, collection);
    }
    
    /* -------------------------------------------------- Operations -------------------------------------------------- */
    
    @Pure
    @Override
    public boolean hasNext() {
        return getIterator().hasNext();
    }
    
    @Impure
    @Override
    public E next() throws NoSuchElementException {
        return getIterator().next();
    }
    
    @Impure
    @Override
    public void remove() throws IllegalStateException {
        Require.that(!getCollection().isFrozen()).orThrow("The underlying collection may not be frozen.");
        
        getIterator().remove();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy