/*
 * @author Ranides Atterwim {@literal <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;

/**
 * LazyIntMap allows for creation of mappings on demand.
 *
 * Method {@link #get} returns mapped value or creates mapping if it does not exist.
 * If you want to avoid creation of mapping, you can use {@link #getIfPresent}
 * It is adapter class around another IntMap implementation: it defines retrieval strategy,
 * but it does not define storage strategy.
 *
 * @param <V> type of value
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
public class IntLazyMap<V> extends AIntMap<V> {
    
    private final IntMap<V> data;
    
    private final IntFunction<? extends V> function;

    /**
     * Creates new lazy map, using specified map as internal storage for already created
     * mappings. If some mapping does not exist, it creates it with default value, which
     * is key independent
     *
     * @param data data
     * @param supplier supplier
     */
    public IntLazyMap(IntMap<V> data, Supplier<? extends V> supplier) {
        this(data, (IntFunction<V> & Serializable)$0 -> supplier.get());
    }

    /**
     * Creates new lazy map, using specified map as internal storage for already created
     * mappings. If some mapping does not exist, it creates it using {@code function} to
     * compute value assigned to key.
     *
     * @param data data
     * @param function function
     */
    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);
    }

    /**
     * Returns mapped value or {@code null} if mapping does not exist.
     * It does not create mapping.
     *
     * @param key key
     * @return value or null
     */
    public V getIfPresent(Object key) {
        return getIfPresent(((Integer)key).intValue());
    }

    /**
     * Returns mapped value or {@code null} if mapping does not exist.
     * It does not create mapping.
     *
     * @param key key
     * @return value or null
     */
    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);
    }
    
}
