package net.ranides.assira.index;

import java.util.Collection;
import java.util.function.Consumer;

/**
 * Base class for IQueryMap containers.
 * It has default implementations of bulk methods (putAll, removeAll).
 *
 * It has default implementation of update method,
 * which simply removes record before modification and reinserts it again.
 *
 * Subclasses can provide better implementations, if we have better knowledge about index structure.
 *
 * @param <V> type of result
 */
abstract class IQueryMapAbstract<V> implements IQueryMap<V> {

    @Override
    public final void putAll(Collection<? extends V> values) {
        for (V value : values) {
            put(value);
        }
    }

    @Override
    public void removeAll(Collection<V> values) {
        for (V item : values) {
            remove(item);
        }
    }

    @Override
    public void update(V value, Consumer<? super V> consumer) {
        remove(value);
        consumer.accept(value);
        put(value);
    }

}
