package org.xbib.datastructures.immutable;

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

/**
 * {@code keySet()} implementation for {@link ImmutableMap}.
 */
final class ImmutableMapKeySet<K, V> extends IndexedImmutableSet<K> {
    private final ImmutableMap<K, V> map;

    ImmutableMapKeySet(ImmutableMap<K, V> map) {
        this.map = map;
    }

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

    @Override
    public UnmodifiableIterator<K> iterator() {
        return map.keyIterator();
    }

    @Override
    public Spliterator<K> spliterator() {
        return map.keySpliterator();
    }

    @Override
    public boolean contains(Object object) {
        return map.containsKey(object);
    }

    @Override
    K get(int index) {
        return map.entrySet().asList().get(index).getKey();
    }

    @Override
    public void forEach(Consumer<? super K> action) {
        Objects.requireNonNull(action);
        map.forEach((k, v) -> action.accept(k));
    }

    @Override
    boolean isPartialView() {
        return true;
    }
}
