/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.maps;

import java.io.Serializable;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.function.Supplier;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IntLazyMap<V> extends AIntMap<V> {
    
    private final IntMap<V> data;
    
    private final IntFunction<? extends V> function;

    public IntLazyMap(IntMap<V> data, Supplier<? extends V> supplier) {
        this(data, (IntFunction<V> & Serializable)$0 -> supplier.get());
    }
    
    public IntLazyMap(IntMap<V> data, IntFunction<? extends V> function) {
        this.data = data;
        this.function = function;
    }

    @Override
    public Set<IntEntry<V>> fastEntrySet() {
        return data.fastEntrySet();
    }

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

    @Override
    public boolean containsKey(int key) {
        return data.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return data.containsValue(value);
    }
    
    public V getIfPresent(Object key) {
        return getIfPresent(((Integer)key).intValue());
    }
    
    public V getIfPresent(int key) {
        return data.containsKey(key) ? data.get(key) : null;
    }

    @Override
    public V get(int key) {
        V ret = data.get(key);
        if (null == ret && !data.containsKey(key)) {
            data.put(key, ret = function.apply(key));
        }
        return ret;
    }

    @Override
    public void clear() {
        data.clear();
    }

    @Override
    public V remove(int key) {
        return data.remove(key);
    }
    
    @Override
    public boolean remove(int key, Object value) {
        return data.remove(key, value);
    }

    @Override
    public V put(int key, V value) {
        return data.put(key, value);
    }
    
}
