Class EnvVar


  • public class EnvVar
    extends Object
    Helper class for environment variables. Uses System.getenv(String) under the hood.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • EnvVar

        public EnvVar()
    • Method Detail

      • get

        public static Map<String,​String> get()
        Returns the current mapping of environment variables.
        Returns:
        the mapping
        See Also:
        System.getenv()
      • has

        public static boolean has​(String name)
        Returns whether the specified environment variable is present.
        Parameters:
        name - the env var to check
        Returns:
        true if present
        See Also:
        System.getenv(String)
      • convert

        public static String convert​(String name,
                                     boolean dotsToUnderscores,
                                     boolean checkUppercase)
        Generates the actual environment variable name.
        Parameters:
        name - the input name
        dotsToUnderscores - whether to convert underscores with dots
        checkUppercase - whether to convert to uppercase
        Returns:
        the converted name
      • find

        public static String find​(String name,
                                  boolean dotsToUnderscores,
                                  boolean checkUppercase)
        Locates the actual environment variable.
        Parameters:
        name - the env var to retrieve
        dotsToUnderscores - whether to check presence of name with underscores replacing dots
        checkUppercase - whether to check presence of uppercased name
        Returns:
        the actual variable name, null if not present
        See Also:
        System.getenv(String)
      • get

        public static String get​(String name)
        Returns the value of the specified environment variable.
        Parameters:
        name - the env var to retrieve
        Returns:
        the associated value or null if not present
        See Also:
        System.getenv(String)
      • get

        public static String get​(String name,
                                 String defValue)
        Returns the value of the specified environment variable.
        Parameters:
        name - the env var to retrieve
        defValue - the default value to use if not set
        Returns:
        the associated value or the default value if not present
        See Also:
        System.getenv(String)
      • get

        public static String get​(String name,
                                 String defValue,
                                 boolean dotsToUnderscores,
                                 boolean checkUppercase)
        Returns the value of the specified environment variable.
        Parameters:
        name - the env var to retrieve
        defValue - the default value to use if not set
        dotsToUnderscores - whether to check presence of name with underscores replacing dots
        checkUppercase - whether to check presence of uppercased name
        Returns:
        the associated value or the default value if not present
        See Also:
        System.getenv(String)
      • getKeyValuePairs

        public static Map<String,​String> getKeyValuePairs​(String name)
        Obtains key=value pairs from the specified environment variable. Uses semi-colon (;) as list separator and equals (=) as pair separator.
        Parameters:
        name - the environment variable to check
        Returns:
        the generated mapping of key=value pairs
      • getKeyValuePairs

        public static Map<String,​String> getKeyValuePairs​(String name,
                                                                boolean dotsToUnderscores,
                                                                boolean checkUppercase)
        Obtains key=value pairs from the specified environment variable.
        Parameters:
        name - the environment variable to check
        dotsToUnderscores - whether to convert check presence of name with underscores replacing dots
        checkUppercase - whether to convert check presence of uppercased name
        Returns:
        the generated mapping of key=value pairs
      • getKeyValuePairs

        public static Map<String,​String> getKeyValuePairs​(String name,
                                                                String listSep,
                                                                String pairSep,
                                                                boolean info)
        Obtains key=value pairs from the specified environment variable.
        Parameters:
        name - the environment variable to check
        listSep - the separator to use for splitting the list of pairs
        pairSep - the separator to use for splitting a pair into key and value
        info - whether to output some info in the console on stdout
        Returns:
        the generated mapping of key=value pairs
      • getKeyValuePairs

        public static Map<String,​String> getKeyValuePairs​(String name,
                                                                boolean dotsToUnderscores,
                                                                boolean checkUppercase,
                                                                String listSep,
                                                                String pairSep,
                                                                boolean info)
        Obtains key=value pairs from the specified environment variable.
        Parameters:
        name - the environment variable to check
        listSep - the separator to use for splitting the list of pairs
        pairSep - the separator to use for splitting a pair into key and value
        info - whether to output some info in the console on stdout
        dotsToUnderscores - whether to convert check presence of name with underscores replacing dots
        checkUppercase - whether to convert check presence of uppercased name
        Returns:
        the generated mapping of key=value pairs