/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.policy.CustomKeyValueStore
 *  com.l7tech.policy.assertion.ext.store.KeyValueStore
 *  com.l7tech.policy.assertion.ext.store.KeyValueStoreException
 *  com.l7tech.policy.assertion.ext.store.KeyValueStoreListener
 *  com.l7tech.util.ExceptionUtils
 */
package com.l7tech.server.store;

import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.policy.CustomKeyValueStore;
import com.l7tech.policy.assertion.ext.store.KeyValueStore;
import com.l7tech.policy.assertion.ext.store.KeyValueStoreException;
import com.l7tech.policy.assertion.ext.store.KeyValueStoreListener;
import com.l7tech.server.policy.CustomKeyValueStoreManager;
import com.l7tech.util.ExceptionUtils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class CustomKeyValueStoreImpl
implements KeyValueStore {
    final CustomKeyValueStoreManager a;
    private static final String[] b;

    public CustomKeyValueStoreImpl(CustomKeyValueStoreManager customKeyValueStoreManager) {
        this.a = customKeyValueStoreManager;
    }

    public Map<String, byte[]> findAllWithKeyPrefix(String string) throws KeyValueStoreException {
        try {
            Collection<CustomKeyValueStore> collection = this.a.findByKeyPrefix(string);
            HashMap<String, byte[]> hashMap = new HashMap<String, byte[]>(collection.size());
            for (CustomKeyValueStore customKeyValueStore : collection) {
                hashMap.put(customKeyValueStore.getName(), customKeyValueStore.getValue());
            }
            return hashMap;
        }
        catch (FindException findException) {
            throw new KeyValueStoreException(b[0] + ExceptionUtils.getMessage((Throwable)findException), (Throwable)findException);
        }
    }

    public byte[] get(String string) throws KeyValueStoreException {
        try {
            CustomKeyValueStore customKeyValueStore = (CustomKeyValueStore)this.a.findByUniqueName(string);
            if (customKeyValueStore != null) {
                return customKeyValueStore.getValue();
            }
            return null;
        }
        catch (FindException findException) {
            throw new KeyValueStoreException(b[5] + ExceptionUtils.getMessage((Throwable)findException), (Throwable)findException);
        }
    }

    public boolean contains(String string) throws KeyValueStoreException {
        try {
            boolean bl2;
            try {
                bl2 = this.a.findByUniqueName(string) != null;
            }
            catch (FindException findException) {
                throw CustomKeyValueStoreImpl.a((Exception)((Object)findException));
            }
            return bl2;
        }
        catch (FindException findException) {
            throw new KeyValueStoreException(b[3] + ExceptionUtils.getMessage((Throwable)findException), (Throwable)findException);
        }
    }

    public void save(String string, byte[] byArray) throws KeyValueStoreException {
        try {
            try {
                if (this.contains(string)) {
                    throw new KeyValueStoreException(b[6] + string);
                }
            }
            catch (SaveException saveException) {
                throw CustomKeyValueStoreImpl.a((Exception)((Object)saveException));
            }
            CustomKeyValueStore customKeyValueStore = new CustomKeyValueStore();
            customKeyValueStore.setName(string);
            customKeyValueStore.setValue(byArray);
            this.a.save((PersistentEntity)customKeyValueStore);
        }
        catch (SaveException saveException) {
            throw new KeyValueStoreException(b[4] + ExceptionUtils.getMessage((Throwable)saveException), (Throwable)saveException);
        }
    }

    public void update(String string, byte[] byArray) throws KeyValueStoreException {
        try {
            CustomKeyValueStore customKeyValueStore = (CustomKeyValueStore)this.a.findByUniqueName(string);
            try {
                if (customKeyValueStore == null) {
                    throw new KeyValueStoreException(b[2] + string);
                }
            }
            catch (FindException findException) {
                throw CustomKeyValueStoreImpl.a((Exception)((Object)findException));
            }
            customKeyValueStore.setValue(byArray);
            this.a.update((PersistentEntity)customKeyValueStore);
        }
        catch (FindException | UpdateException throwable) {
            throw new KeyValueStoreException(b[1] + ExceptionUtils.getMessage((Throwable)throwable), throwable);
        }
    }

    public void saveOrUpdate(String string, byte[] byArray) throws KeyValueStoreException {
        block3: {
            block2: {
                try {
                    if (this.contains(string)) break block2;
                    this.save(string, byArray);
                    break block3;
                }
                catch (KeyValueStoreException keyValueStoreException) {
                    throw CustomKeyValueStoreImpl.a((Exception)((Object)keyValueStoreException));
                }
            }
            this.update(string, byArray);
        }
    }

    public void delete(String string) throws KeyValueStoreException {
        try {
            this.a.deleteByKey(string);
        }
        catch (DeleteException deleteException) {
            throw new KeyValueStoreException(b[7] + ExceptionUtils.getMessage((Throwable)deleteException), (Throwable)deleteException);
        }
    }

    public <L extends KeyValueStoreListener> L getListener(Class<L> clazz) {
        return this.a.getListener(clazz);
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[8];
                var3_1 = 0;
                var2_2 = "\u00009\ny\u0014rp!8K}\u0011y4u6\u0007wB7\u0012\u00009\ny\u0014rp!8Kn\bs1!2Q;*\u00009\ny\u0014rp!8Kn\bs1!2E;,\u007f5u<\u000ebXs?0$Ku\u0017cp0/\u0002h\f-p\u001f\u00009\ny\u0014rp!8Kx\u0010r3>w\u0002}X|5,w\u000ec\u0011d$&mK\u0010\u00009\ny\u0014rp!8Kh\u0019a5ow\u000f\u00009\ny\u0014rp!8K|\u001dcju";
                var4_3 = "\u00009\ny\u0014rp!8K}\u0011y4u6\u0007wB7\u0012\u00009\ny\u0014rp!8Kn\bs1!2Q;*\u00009\ny\u0014rp!8Kn\bs1!2E;,\u007f5u<\u000ebXs?0$Ku\u0017cp0/\u0002h\f-p\u001f\u00009\ny\u0014rp!8Kx\u0010r3>w\u0002}X|5,w\u000ec\u0011d$&mK\u0010\u00009\ny\u0014rp!8Kh\u0019a5ow\u000f\u00009\ny\u0014rp!8K|\u001dcju".length();
                var1_4 = 20;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = CustomKeyValueStoreImpl.a(v0, CustomKeyValueStoreImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "Ul_,A'%tm\u001e=L4`.\"j&Hbne{\u001e/A0`afGnH:lsvMt\r\u0012Ul_,A'%tm\u001e*H.`tg\u0004n";
                    var4_3 = "Ul_,A'%tm\u001e=L4`.\"j&Hbne{\u001e/A0`afGnH:lsvMt\r\u0012Ul_,A'%tm\u001e*H.`tg\u0004n".length();
                    var1_4 = 40;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 37;
                        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++] = CustomKeyValueStoreImpl.a(v0, CustomKeyValueStoreImpl.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;
            }
        }
        CustomKeyValueStoreImpl.b = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x20);
        }
        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 -> 37;
                    case 1 -> 39;
                    case 2 -> 27;
                    case 3 -> 107;
                    case 4 -> 8;
                    case 5 -> 103;
                    default -> 32;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

