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

package it.unimi.dsi.fastutil.ints;

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

public final class Int2ByteFunctions
{
    public static final EmptyFunction EMPTY_FUNCTION;
    
    private Int2ByteFunctions() {
    }
    
    public static Int2ByteFunction singleton(final int key, final byte value) {
        return new Singleton(key, value);
    }
    
    public static Int2ByteFunction singleton(final Integer key, final Byte value) {
        return new Singleton(key, value);
    }
    
    public static Int2ByteFunction synchronize(final Int2ByteFunction f) {
        return new SynchronizedFunction(f);
    }
    
    public static Int2ByteFunction synchronize(final Int2ByteFunction f, final Object sync) {
        return new SynchronizedFunction(f, sync);
    }
    
    public static Int2ByteFunction unmodifiable(final Int2ByteFunction f) {
        return new UnmodifiableFunction(f);
    }
    
    public static Int2ByteFunction primitive(final Function<? super Integer, ? extends Byte> f) {
        Objects.requireNonNull(f);
        if (f instanceof Int2ByteFunction) {
            return (Int2ByteFunction)f;
        }
        if (f instanceof IntUnaryOperator) {
            return key -> SafeMath.safeIntToByte(((IntUnaryOperator)f).applyAsInt(key));
        }
        return new PrimitiveFunction(f);
    }
    
    static {
        EMPTY_FUNCTION = new EmptyFunction();
    }
    
    public static class EmptyFunction extends AbstractInt2ByteFunction implements Serializable, Cloneable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        
        protected EmptyFunction() {
        }
        
        @Override
        public byte get(final int k) {
            return 0;
        }
        
        @Override
        public byte getOrDefault(final int k, final byte defaultValue) {
            return defaultValue;
        }
        
        @Override
        public boolean containsKey(final int k) {
            return false;
        }
        
        @Override
        public byte defaultReturnValue() {
            return 0;
        }
        
        @Override
        public void defaultReturnValue(final byte defRetValue) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public int size() {
            return 0;
        }
        
        @Override
        public void clear() {
        }
        
        public Object clone() {
            return Int2ByteFunctions.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 Int2ByteFunctions.EMPTY_FUNCTION;
        }
    }
    
    public static class Singleton extends AbstractInt2ByteFunction implements Serializable, Cloneable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final int key;
        protected final byte value;
        
        protected Singleton(final int key, final byte value) {
            this.key = key;
            this.value = value;
        }
        
        @Override
        public boolean containsKey(final int k) {
            return this.key == k;
        }
        
        @Override
        public byte get(final int k) {
            return (this.key == k) ? this.value : this.defRetValue;
        }
        
        @Override
        public byte getOrDefault(final int k, final byte defaultValue) {
            return (this.key == k) ? this.value : defaultValue;
        }
        
        @Override
        public int size() {
            return 1;
        }
        
        public Object clone() {
            return this;
        }
    }
    
    public static class SynchronizedFunction implements Int2ByteFunction, Serializable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final Int2ByteFunction function;
        protected final Object sync;
        
        protected SynchronizedFunction(final Int2ByteFunction f, final Object sync) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
            this.sync = sync;
        }
        
        protected SynchronizedFunction(final Int2ByteFunction f) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
            this.sync = this;
        }
        
        @Override
        public int applyAsInt(final int operand) {
            synchronized (this.sync) {
                return this.function.applyAsInt(operand);
            }
        }
        
        @Deprecated
        @Override
        public Byte apply(final Integer key) {
            synchronized (this.sync) {
                return this.function.apply(key);
            }
        }
        
        @Override
        public int size() {
            synchronized (this.sync) {
                return this.function.size();
            }
        }
        
        @Override
        public byte defaultReturnValue() {
            synchronized (this.sync) {
                return this.function.defaultReturnValue();
            }
        }
        
        @Override
        public void defaultReturnValue(final byte defRetValue) {
            synchronized (this.sync) {
                this.function.defaultReturnValue(defRetValue);
            }
        }
        
        @Override
        public boolean containsKey(final int k) {
            synchronized (this.sync) {
                return this.function.containsKey(k);
            }
        }
        
        @Deprecated
        @Override
        public boolean containsKey(final Object k) {
            synchronized (this.sync) {
                return this.function.containsKey(k);
            }
        }
        
        @Override
        public byte put(final int k, final byte v) {
            synchronized (this.sync) {
                return this.function.put(k, v);
            }
        }
        
        @Override
        public byte get(final int k) {
            synchronized (this.sync) {
                return this.function.get(k);
            }
        }
        
        @Override
        public byte getOrDefault(final int k, final byte defaultValue) {
            synchronized (this.sync) {
                return this.function.getOrDefault(k, defaultValue);
            }
        }
        
        @Override
        public byte remove(final int k) {
            synchronized (this.sync) {
                return this.function.remove(k);
            }
        }
        
        @Override
        public void clear() {
            synchronized (this.sync) {
                this.function.clear();
            }
        }
        
        @Deprecated
        @Override
        public Byte put(final Integer k, final Byte v) {
            synchronized (this.sync) {
                return this.function.put(k, v);
            }
        }
        
        @Deprecated
        @Override
        public Byte get(final Object k) {
            synchronized (this.sync) {
                return this.function.get(k);
            }
        }
        
        @Deprecated
        @Override
        public Byte getOrDefault(final Object k, final Byte defaultValue) {
            synchronized (this.sync) {
                return this.function.getOrDefault(k, defaultValue);
            }
        }
        
        @Deprecated
        @Override
        public Byte remove(final Object k) {
            synchronized (this.sync) {
                return this.function.remove(k);
            }
        }
        
        @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 extends AbstractInt2ByteFunction implements Serializable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final Int2ByteFunction function;
        
        protected UnmodifiableFunction(final Int2ByteFunction f) {
            if (f == null) {
                throw new NullPointerException();
            }
            this.function = f;
        }
        
        @Override
        public int size() {
            return this.function.size();
        }
        
        @Override
        public byte defaultReturnValue() {
            return this.function.defaultReturnValue();
        }
        
        @Override
        public void defaultReturnValue(final byte defRetValue) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public boolean containsKey(final int k) {
            return this.function.containsKey(k);
        }
        
        @Override
        public byte put(final int k, final byte v) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public byte get(final int k) {
            return this.function.get(k);
        }
        
        @Override
        public byte getOrDefault(final int k, final byte defaultValue) {
            return this.function.getOrDefault(k, defaultValue);
        }
        
        @Override
        public byte remove(final int k) {
            throw new UnsupportedOperationException();
        }
        
        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }
        
        @Deprecated
        @Override
        public Byte put(final Integer k, final Byte v) {
            throw new UnsupportedOperationException();
        }
        
        @Deprecated
        @Override
        public Byte get(final Object k) {
            return this.function.get(k);
        }
        
        @Deprecated
        @Override
        public Byte getOrDefault(final Object k, final Byte defaultValue) {
            return this.function.getOrDefault(k, defaultValue);
        }
        
        @Deprecated
        @Override
        public Byte remove(final Object k) {
            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();
        }
    }
    
    public static class PrimitiveFunction implements Int2ByteFunction
    {
        protected final java.util.function.Function<? super Integer, ? extends Byte> function;
        
        protected PrimitiveFunction(final java.util.function.Function<? super Integer, ? extends Byte> function) {
            this.function = function;
        }
        
        @Override
        public boolean containsKey(final int key) {
            return this.function.apply(key) != null;
        }
        
        @Deprecated
        @Override
        public boolean containsKey(final Object key) {
            return key != null && this.function.apply((Integer)key) != null;
        }
        
        @Override
        public byte get(final int key) {
            final Byte v = (Byte)this.function.apply(key);
            if (v == null) {
                return this.defaultReturnValue();
            }
            return v;
        }
        
        @Override
        public byte getOrDefault(final int key, final byte defaultValue) {
            final Byte v = (Byte)this.function.apply(key);
            if (v == null) {
                return defaultValue;
            }
            return v;
        }
        
        @Deprecated
        @Override
        public Byte get(final Object key) {
            if (key == null) {
                return null;
            }
            return (Byte)this.function.apply((Integer)key);
        }
        
        @Deprecated
        @Override
        public Byte getOrDefault(final Object key, final Byte defaultValue) {
            if (key == null) {
                return defaultValue;
            }
            final Byte v;
            return ((v = (Byte)this.function.apply((Integer)key)) == null) ? defaultValue : v;
        }
        
        @Deprecated
        @Override
        public Byte put(final Integer key, final Byte value) {
            throw new UnsupportedOperationException();
        }
    }
}
