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

package org.bouncycastle.util;

import java.security.AccessController;
import java.security.Security;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.HashSet;
import java.util.Set;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.security.AccessControlException;

public class Properties
{
    public static final String EMULATE_ORACLE = "org.bouncycastle.emulate.oracle";
    private static final ThreadLocal threadProperties;
    
    private Properties() {
    }
    
    public static boolean isOverrideSet(final String s) {
        try {
            return isSetTrue(getPropertyValue(s));
        }
        catch (final AccessControlException ex) {
            return false;
        }
    }
    
    public static boolean isOverrideSet(final String s, final boolean b) {
        try {
            final String propertyValue = getPropertyValue(s);
            if (propertyValue == null) {
                return b;
            }
            return isSetTrue(propertyValue);
        }
        catch (final AccessControlException ex) {
            return false;
        }
    }
    
    public static boolean isOverrideSetTo(final String s, final boolean b) {
        try {
            final String propertyValue = getPropertyValue(s);
            if (b) {
                return isSetTrue(propertyValue);
            }
            return isSetFalse(propertyValue);
        }
        catch (final AccessControlException ex) {
            return false;
        }
    }
    
    public static boolean setThreadOverride(final String s, final boolean b) {
        final boolean overrideSet = isOverrideSet(s);
        Map value = Properties.threadProperties.get();
        if (value == null) {
            value = new HashMap();
            Properties.threadProperties.set(value);
        }
        value.put(s, b ? "true" : "false");
        return overrideSet;
    }
    
    public static boolean removeThreadOverride(final String s) {
        final Map map = Properties.threadProperties.get();
        if (map != null) {
            final String s2 = (String)map.remove(s);
            if (s2 != null) {
                if (map.isEmpty()) {
                    Properties.threadProperties.remove();
                }
                return "true".equals(Strings.toLowerCase(s2));
            }
        }
        return false;
    }
    
    public static int asInteger(final String s, final int n) {
        final String propertyValue = getPropertyValue(s);
        if (propertyValue != null) {
            return Integer.parseInt(propertyValue);
        }
        return n;
    }
    
    public static BigInteger asBigInteger(final String s) {
        final String propertyValue = getPropertyValue(s);
        if (propertyValue != null) {
            return new BigInteger(propertyValue);
        }
        return null;
    }
    
    public static Set<String> asKeySet(final String s) {
        final HashSet s2 = new HashSet();
        final String propertyValue = getPropertyValue(s);
        if (propertyValue != null) {
            final StringTokenizer stringTokenizer = new StringTokenizer(propertyValue, ",");
            while (stringTokenizer.hasMoreElements()) {
                s2.add(Strings.toLowerCase(stringTokenizer.nextToken()).trim());
            }
        }
        return (Set<String>)Collections.unmodifiableSet((Set<?>)s2);
    }
    
    public static String getPropertyValue(final String s) {
        final String s2 = AccessController.doPrivileged((PrivilegedAction<String>)new PrivilegedAction() {
            @Override
            public Object run() {
                return Security.getProperty(s);
            }
        });
        if (s2 != null) {
            return s2;
        }
        final Map map = Properties.threadProperties.get();
        if (map != null) {
            final String s3 = (String)map.get(s);
            if (s3 != null) {
                return s3;
            }
        }
        return AccessController.doPrivileged((PrivilegedAction<String>)new PrivilegedAction() {
            @Override
            public Object run() {
                return System.getProperty(s);
            }
        });
    }
    
    public static String getPropertyValue(final String s, final String s2) {
        final String propertyValue = getPropertyValue(s);
        if (propertyValue == null) {
            return s2;
        }
        return propertyValue;
    }
    
    private static boolean isSetFalse(final String s) {
        return s != null && s.length() == 5 && (s.charAt(0) == 'f' || s.charAt(0) == 'F') && (s.charAt(1) == 'a' || s.charAt(1) == 'A') && (s.charAt(2) == 'l' || s.charAt(2) == 'L') && (s.charAt(3) == 's' || s.charAt(3) == 'S') && (s.charAt(4) == 'e' || s.charAt(4) == 'E');
    }
    
    private static boolean isSetTrue(final String s) {
        return s != null && s.length() == 4 && (s.charAt(0) == 't' || s.charAt(0) == 'T') && (s.charAt(1) == 'r' || s.charAt(1) == 'R') && (s.charAt(2) == 'u' || s.charAt(2) == 'U') && (s.charAt(3) == 'e' || s.charAt(3) == 'E');
    }
    
    static {
        threadProperties = new ThreadLocal();
    }
}
