/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityManager
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.util.Functions$Unary
 *  com.l7tech.util.Option
 *  org.springframework.context.ApplicationListener
 */
package com.l7tech.server;

import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.util.Functions;
import com.l7tech.util.Option;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationListener;

public abstract class EntityCacheSupport<ET extends PersistentEntity, HT extends EntityHeader, EM extends EntityManager<ET, HT>>
implements ApplicationListener<EntityInvalidationEvent> {
    protected final Logger logger = Logger.getLogger(this.getClass().getName());
    private final EM a;
    private final ReadWriteLock b = new ReentrantReadWriteLock();
    private boolean c = true;
    private boolean d = false;
    private final Map<Goid, ET> e = new HashMap<Goid, ET>();
    static final /* synthetic */ boolean f;
    private static final String[] g;

    public final Option<ET> findByPrimaryKey(Goid goid) {
        this.b.readLock().lock();
        try {
            Option option = Option.optional((Object)((PersistentEntity)this.e.get(goid)));
            return option;
        }
        finally {
            this.b.readLock().unlock();
        }
    }

    public final void onApplicationEvent(EntityInvalidationEvent entityInvalidationEvent) {
        if (this.getEntityClass().isAssignableFrom(entityInvalidationEvent.getEntityClass())) {
            this.a();
            Goid goid = PersistentEntity.DEFAULT_GOID;
            try {
                Goid[] goidArray = entityInvalidationEvent.getEntityIds();
                int n2 = goidArray.length;
                for (int i2 = 0; i2 < n2; ++i2) {
                    Goid goid2;
                    goid = goid2 = goidArray[i2];
                    PersistentEntity persistentEntity = this.a.findByPrimaryKey(goid2);
                    try {
                        if (persistentEntity == null) {
                            this.a(goid2);
                            continue;
                        }
                    }
                    catch (FindException findException) {
                        throw EntityCacheSupport.a(findException);
                    }
                    this.a(this.readOnly(persistentEntity));
                }
            }
            catch (FindException findException) {
                this.b();
                String[] stringArray = g;
                this.logger.log(Level.WARNING, stringArray[5] + String.valueOf(goid) + stringArray[4], findException);
            }
        }
    }

    protected EntityCacheSupport(EM EM) {
        this.a = EM;
    }

    protected abstract Class<ET> getEntityClass();

    protected abstract ET readOnly(ET var1);

    protected final <R> R doWithCacheReadOnly(Functions.Unary<R, Map<Goid, ET>> unary) {
        this.b.readLock().lock();
        try {
            Object object = unary.call(this.e);
            return (R)object;
        }
        finally {
            this.b.readLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     */
    protected final void initializeCache() {
        Lock lock = this.b.writeLock();
        lock.lock();
        try {
            this.d = true;
            Collection collection = this.a.findAll();
            this.e.clear();
            for (PersistentEntity persistentEntity : collection) {
                block10: {
                    if (f) break block10;
                    try {
                        block12: {
                            if (!Goid.isDefault((Goid)persistentEntity.getGoid())) break block10;
                            break block12;
                            catch (FindException findException) {
                                throw EntityCacheSupport.a(findException);
                            }
                        }
                        throw new AssertionError();
                    }
                    catch (FindException findException) {
                        throw EntityCacheSupport.a(findException);
                    }
                }
                this.e.put(persistentEntity.getGoid(), persistentEntity);
            }
            this.c = false;
        }
        catch (FindException findException) {
            this.b();
            this.logger.log(Level.WARNING, g[2], findException);
        }
        finally {
            lock.unlock();
        }
    }

    private void a(ET ET) {
        if (this.logger.isLoggable(Level.FINE)) {
            this.logger.log(Level.FINE, g[0], ET.getGoid().toHexString());
        }
        Lock lock = this.b.writeLock();
        lock.lock();
        try {
            this.e.put(ET.getGoid(), ET);
        }
        finally {
            lock.unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void a(Goid goid) {
        Lock lock = this.b.writeLock();
        lock.lock();
        try {
            PersistentEntity persistentEntity = (PersistentEntity)this.e.get(goid);
            if (persistentEntity == null) {
                String[] stringArray = g;
                this.logger.log(Level.FINE, stringArray[3], goid);
            } else {
                this.logger.log(Level.FINE, g[1], goid);
                this.e.remove(goid);
            }
        }
        finally {
            lock.unlock();
        }
    }

    private void a() {
        boolean bl2;
        Lock lock = this.b.readLock();
        lock.lock();
        try {
            bl2 = this.d && this.c;
        }
        finally {
            lock.unlock();
        }
        if (bl2) {
            this.initializeCache();
        }
    }

    private void b() {
        Lock lock = this.b.writeLock();
        lock.lock();
        try {
            this.c = true;
        }
        finally {
            lock.unlock();
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "\u0003~bM9\u0001<ek&Ym\u0010}50tA(\u0016<%bsE9\u001dxf\u007fd\u00048\bx'ds@vXi6twP$\u0016{fswG%\u001do0\u0003~bM9\u0001<ek&Ym\u0010}50tA#Xx#|sP(\u001c'fbsI\"\u000eu(w6B?\u0017qfswG%\u001d\u0016\u0003bdK?Xl)`cH,\fu(w6G,\u001bt#%\b\u007f6A#\fu2i6B\"\rr\"0pK?Xu\"05_}\u00050fyqJ\"\nu(w";
                var4_3 = "\u0003~bM9\u0001<ek&Ym\u0010}50tA(\u0016<%bsE9\u001dxf\u007fd\u00048\bx'ds@vXi6twP$\u0016{fswG%\u001do0\u0003~bM9\u0001<ek&Ym\u0010}50tA#Xx#|sP(\u001c'fbsI\"\u000eu(w6B?\u0017qfswG%\u001d\u0016\u0003bdK?Xl)`cH,\fu(w6G,\u001bt#%\b\u007f6A#\fu2i6B\"\rr\"0pK?Xu\"05_}\u00050fyqJ\"\nu(w".length();
                var1_4 = 56;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 105;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = EntityCacheSupport.a(v0, EntityCacheSupport.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "8('\b3E#{&-\u0015]<:\u0015a\u0006.w/,\u0013}Ab} <\u0013g_b";
                    var4_3 = "8('\b3E#{&-\u0015]<:\u0015a\u0006.w/,\u0013}Ab} <\u0013g_b".length();
                    var1_4 = 10;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 55;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = EntityCacheSupport.a(v0, EntityCacheSupport.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        EntityCacheSupport.g = var5;
        EntityCacheSupport.f = EntityCacheSupport.class.desiredAssertionStatus() == false;
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x75);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 47;
                    case 1 -> 121;
                    case 2 -> 127;
                    case 3 -> 77;
                    case 4 -> 36;
                    case 5 -> 17;
                    default -> 117;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

