// 
// Decompiled by Procyon v0.6.0
// 

package it.unimi.dsi.fastutil.objects;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Objects;
import it.unimi.dsi.fastutil.Function;
import java.io.Serializable;

public final class Object2ReferenceFunctions
{
    public static final EmptyFunction EMPTY_FUNCTION;
    
    private Object2ReferenceFunctions() {
    }
    
    public static <K, V> Object2ReferenceFunction<K, V> singleton(final K key, final V value) {
        return new Singleton<K, V>(key, value);
    }
    
    public static <K, V> Object2ReferenceFunction<K, V> synchronize(final Object2ReferenceFunction<K, V> f) {
        return new SynchronizedFunction<K, V>(f);
    }
    
    public static <K, V> Object2ReferenceFunction<K, V> synchronize(final Object2ReferenceFunction<K, V> f, final Object sync) {
        return new SynchronizedFunction<K, V>(f, sync);
    }
    
    public static <K, V> Object2ReferenceFunction<K, V> unmodifiable(final Object2ReferenceFunction<? extends K, ? extends V> f) {
        return new UnmodifiableFunction<K, V>(f);
    }
    
    static {
        EMPTY_FUNCTION = new EmptyFunction();
    }
    
    public static class EmptyFunction<K, V> extends AbstractObject2ReferenceFunction<K, V> implements Serializable, Cloneable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        
        protected EmptyFunction() {
        }
        
        @Override
        public V get(final Object k) {
            return null;
        }
        
        @Override
        public V getOrDefault(final Object k, final V defaultValue) {
            return defaultValue;
        }
        
        @Override
        public boolean containsKey(final Object k) {
            return false;
        }
        
        @Override
        public V defaultReturnValue() {
            return null;
        }
        
        @Override
        public void defaultReturnValue(final V defRetValue) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public int size() {
            return 0;
        }
        
        @Override
        public void clear() {
        }
        
        public Object clone() {
            return Object2ReferenceFunctions.EMPTY_FUNCTION;
        }
        
        @Override
        public int hashCode() {
            return 0;
        }
        
        @Override
        public boolean equals(final Object o) {
            return o instanceof Function && ((Function)o).size() == 0;
        }
        
        @Override
        public String toString() {
            return "{}";
        }
        
        private Object readResolve() {
            return Object2ReferenceFunctions.EMPTY_FUNCTION;
        }
    }
    
    public static class Singleton<K, V> extends AbstractObject2ReferenceFunction<K, V> implements Serializable, Cloneable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final K key;
        protected final V value;
        
        protected Singleton(final K key, final V value) {
            this.key = key;
            this.value = value;
        }
        
        @Override
        public boolean containsKey(final Object k) {
            return Objects.equals(this.key, k);
        }
        
        @Override
        public V get(final Object k) {
            return Objects.equals(this.key, k) ? this.value : this.defRetValue;
        }
        
        @Override
        public V getOrDefault(final Object k, final V defaultValue) {
            return Objects.equals(this.key, k) ? this.value : defaultValue;
        }
        
        @Override
        public int size() {
            return 1;
        }
        
        public Object clone() {
            return this;
        }
    }
    
    public static class SynchronizedFunction<K, V> implements Object2ReferenceFunction<K, V>, Serializable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final Object2ReferenceFunction<K, V> function;
        protected final Object sync;
        
        protected SynchronizedFunction(final Object2ReferenceFunction<K, V> f, final Object sync) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
            this.sync = sync;
        }
        
        protected SynchronizedFunction(final Object2ReferenceFunction<K, V> f) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
            this.sync = this;
        }
        
        @Override
        public V apply(final K key) {
            synchronized (this.sync) {
                return this.function.apply(key);
            }
        }
        
        @Override
        public int size() {
            synchronized (this.sync) {
                return this.function.size();
            }
        }
        
        @Override
        public V defaultReturnValue() {
            synchronized (this.sync) {
                return this.function.defaultReturnValue();
            }
        }
        
        @Override
        public void defaultReturnValue(final V defRetValue) {
            synchronized (this.sync) {
                this.function.defaultReturnValue(defRetValue);
            }
        }
        
        @Override
        public boolean containsKey(final Object k) {
            synchronized (this.sync) {
                return this.function.containsKey(k);
            }
        }
        
        @Override
        public V put(final K k, final V v) {
            synchronized (this.sync) {
                return this.function.put(k, v);
            }
        }
        
        @Override
        public V get(final Object k) {
            synchronized (this.sync) {
                return this.function.get(k);
            }
        }
        
        @Override
        public V getOrDefault(final Object k, final V defaultValue) {
            synchronized (this.sync) {
                return this.function.getOrDefault(k, defaultValue);
            }
        }
        
        @Override
        public V remove(final Object k) {
            synchronized (this.sync) {
                return this.function.remove(k);
            }
        }
        
        @Override
        public void clear() {
            synchronized (this.sync) {
                this.function.clear();
            }
        }
        
        @Override
        public int hashCode() {
            synchronized (this.sync) {
                return this.function.hashCode();
            }
        }
        
        @Override
        public boolean equals(final Object o) {
            if (o == this) {
                return true;
            }
            synchronized (this.sync) {
                return this.function.equals(o);
            }
        }
        
        @Override
        public String toString() {
            synchronized (this.sync) {
                return this.function.toString();
            }
        }
        
        private void writeObject(final ObjectOutputStream s) throws IOException {
            synchronized (this.sync) {
                s.defaultWriteObject();
            }
        }
    }
    
    public static class UnmodifiableFunction<K, V> extends AbstractObject2ReferenceFunction<K, V> implements Serializable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final Object2ReferenceFunction<? extends K, ? extends V> function;
        
        protected UnmodifiableFunction(final Object2ReferenceFunction<? extends K, ? extends V> f) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
        }
        
        @Override
        public int size() {
            return this.function.size();
        }
        
        @Override
        public V defaultReturnValue() {
            return (V)this.function.defaultReturnValue();
        }
        
        @Override
        public void defaultReturnValue(final V defRetValue) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public boolean containsKey(final Object k) {
            return this.function.containsKey(k);
        }
        
        @Override
        public V put(final K k, final V v) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public V get(final Object k) {
            return (V)this.function.get(k);
        }
        
        @Override
        public V getOrDefault(final Object k, final V defaultValue) {
            return (V)this.function.getOrDefault(k, (V)defaultValue);
        }
        
        @Override
        public V remove(final Object k) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public int hashCode() {
            return this.function.hashCode();
        }
        
        @Override
        public boolean equals(final Object o) {
            return o == this || this.function.equals(o);
        }
        
        @Override
        public String toString() {
            return this.function.toString();
        }
    }
}
