package com.facebook.common.references;

import X.C1106PaZ;
import X.C1107Paa;
import X.C3026QDf;
import X.PHP;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.IdentityHashMap;
import java.util.Map;

public class SharedReference<T> {
    public static final Map<Object, Integer> sLiveObjects = new IdentityHashMap();
    public int mRefCount = 1;
    public final C1107Paa<T> mResourceReleaser;
    public T mValue;

    static {
        Covode.recordClassIndex(41169);
    }

    private void ensureValid() {
        if (!isValid(this)) {
            throw new C1106PaZ();
        }
    }

    public synchronized T get() {
        T t;
        MethodCollector.i(6049);
        t = this.mValue;
        MethodCollector.o(6049);
        return t;
    }

    public synchronized int getRefCountTestOnly() {
        int i;
        MethodCollector.i(3065);
        i = this.mRefCount;
        MethodCollector.o(3065);
        return i;
    }

    public synchronized void addReference() {
        MethodCollector.i(2926);
        ensureValid();
        this.mRefCount++;
        MethodCollector.o(2926);
    }

    public synchronized boolean isValid() {
        MethodCollector.i(6096);
        if (this.mRefCount > 0) {
            MethodCollector.o(6096);
            return true;
        }
        MethodCollector.o(6096);
        return false;
    }

    private synchronized int decreaseRefCount() {
        boolean z;
        int i;
        MethodCollector.i(2995);
        ensureValid();
        if (this.mRefCount > 0) {
            z = true;
        } else {
            z = false;
        }
        PHP.LIZ(z);
        i = this.mRefCount - 1;
        this.mRefCount = i;
        MethodCollector.o(2995);
        return i;
    }

    public void deleteReference() {
        T t;
        MethodCollector.i(2929);
        if (decreaseRefCount() == 0) {
            synchronized (this) {
                try {
                    t = this.mValue;
                    this.mValue = null;
                } finally {
                    MethodCollector.o(2929);
                }
            }
            this.mResourceReleaser.LIZ(t);
            removeLiveReference(t);
        }
    }

    public static boolean isValid(SharedReference<?> sharedReference) {
        if (sharedReference == null || !sharedReference.isValid()) {
            return false;
        }
        return true;
    }

    public static void addLiveReference(Object obj) {
        MethodCollector.i(5984);
        Map<Object, Integer> map = sLiveObjects;
        synchronized (map) {
            try {
                Integer num = map.get(obj);
                if (num == null) {
                    map.put(obj, 1);
                } else {
                    map.put(obj, Integer.valueOf(num.intValue() + 1));
                }
            } finally {
                MethodCollector.o(5984);
            }
        }
    }

    public static void removeLiveReference(Object obj) {
        MethodCollector.i(5989);
        Map<Object, Integer> map = sLiveObjects;
        synchronized (map) {
            try {
                Integer num = map.get(obj);
                if (num == null) {
                    C3026QDf.LIZLLL("SharedReference", "No entry in sLiveObjects for value of type %s", obj.getClass());
                } else if (num.intValue() == 1) {
                    map.remove(obj);
                } else {
                    map.put(obj, Integer.valueOf(num.intValue() - 1));
                }
            } finally {
                MethodCollector.o(5989);
            }
        }
    }

    public SharedReference(T t, C1107Paa<T> paa) {
        PHP.LIZ(t);
        this.mValue = t;
        PHP.LIZ(paa);
        this.mResourceReleaser = paa;
        addLiveReference(t);
    }
}
