package org.xbib.datastructures.immutable;

import java.util.Map;
import java.util.Spliterator;
import java.util.function.Consumer;

final class RegularEntrySet<K, V> extends ImmutableMapEntrySet<K, V> {
    private final transient ImmutableMap<K, V> map;
    private final transient ImmutableList<Map.Entry<K, V>> entries;

    RegularEntrySet(ImmutableMap<K, V> map, Map.Entry<K, V>[] entries) {
        this(map, ImmutableList.asImmutableList(entries));
    }

    RegularEntrySet(ImmutableMap<K, V> map, ImmutableList<Map.Entry<K, V>> entries) {
        this.map = map;
        this.entries = entries;
    }

    @Override
    ImmutableMap<K, V> map() {
        return map;
    }

    @Override
    int copyIntoArray(Object[] dst, int offset) {
        return entries.copyIntoArray(dst, offset);
    }

    @Override
    public UnmodifiableIterator<Map.Entry<K, V>> iterator() {
        return entries.iterator();
    }

    @Override
    public Spliterator<Map.Entry<K, V>> spliterator() {
        return entries.spliterator();
    }

    @Override
    public void forEach(Consumer<? super Map.Entry<K, V>> action) {
        entries.forEach(action);
    }

    @Override
    ImmutableList<Map.Entry<K, V>> createAsList() {
        return new RegularImmutableAsList<>(this, entries);
    }
}
