package org.dbrct.system;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Helper class for environment variables
 */
public final class Environment {

    private static final ConcurrentMap<String, String> env = new ConcurrentHashMap<String, String>(System.getenv());
    private static final ConcurrentMap<String, String> userEnv = new ConcurrentHashMap<String, String>();

    private Environment() {
        // I'm a singleton
    }

    /**
     * Get environment variable
     * @param key environment variable name
     * @return environment variable value
     */
    public static String get(final String key) {
        final String envKey = Environment.env.get(key);
        if (envKey == null) {
            return Environment.userEnv.get(key);
        }
        return envKey;
    }

    /**
     * Reset user/program defined environment variables
     */
    static void reset() {
        Environment.userEnv.clear();
    }

    static void setUserEnv(final String key, final String value) {
        Environment.userEnv.put(key, value);
    }

}
