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

package net.ranides.assira.reflection.impl;

import java.util.Objects;
import java.util.function.Supplier;
import net.ranides.assira.collection.maps.Cache;
import net.ranides.assira.reflection.IContext;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class FCache<T> extends Cache<FCache.CKey,T> {
    
    public T get(IContext context, Object key, Supplier<T> supplier) {
        return get(new CKey(context, key), supplier);
    }

    static final class CKey {
        
        private final IContext parent;
        private final Object key;

        public CKey(IContext parent, Object key) {
            this.parent = parent;
            this.key = key;
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(this.parent) ^ Objects.hashCode(this.key);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            final CKey that = (CKey) obj;
            return (this.parent == that.parent) && Objects.equals(this.key, that.key);
        }

    }
}
