001    /*
002    Galois, a framework to exploit amorphous data-parallelism in irregular
003    programs.
004    
005    Copyright (C) 2010, The University of Texas at Austin. All rights reserved.
006    UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE
007    AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY
008    PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY
009    WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
010    NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE
011    SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable
012    for incidental, special, indirect, direct or consequential damages or loss of
013    profits, interruption of business, or related expenses which may arise from use
014    of Software or Documentation, including but not limited to those resulting from
015    defects in Software and/or Documentation, or loss or inaccuracy of data of any
016    kind.
017    
018    File: SystemProperties.java 
019    
020    */
021    
022    
023    
024    package util;
025    
026    /**
027     * Utility class to deal with retrieving properties from System.
028     */
029    public class SystemProperties {
030      /**
031       * Returns the enum value associated with the given system property or the
032       * default if the property was unset or set to an unparseable value.
033       *
034       * @param name Name of the System.getProperty() property to fetch
035       * @param def  Default value if property not found
036       * @return The value associated with the property or the default value
037       */
038      public static <T extends Enum<T>> T getEnumProperty(String name, Class<T> enumType, T def) {
039        String value = System.getProperty(name);
040        if (value == null) {
041          return def;
042        }
043        try {
044          return Enum.valueOf(enumType, value);
045        } catch (IllegalArgumentException _) {
046          return def;
047        }
048      }
049    
050      /**
051       * Returns the integer value associated with the given system property, or the
052       * default if the property was unset or set to an unparseable string.
053       *
054       * @param name Name of the System.getProperty() property to fetch
055       * @param def  Default value if property not found
056       * @return The value associated with the property or the default value
057       */
058      public static int getIntProperty(String name, int def) {
059        return Integer.getInteger(name, def);
060      }
061    
062      /**
063       * Returns the long value associated with the given system property, or the
064       * default if the property was unset or set to an unparseable string.
065       *
066       * @param name Name of the System.getProperty() property to fetch
067       * @param def  Default value if property not found
068       * @return The value associated with the property or the default value
069       */
070      public static long getLongProperty(String name, long def) {
071        return Long.getLong(name, def);
072      }
073    
074      /**
075       * Returns the double value associated with the given system property, or the
076       * default if the property was unset or set to an unparseable string.
077       *
078       * @param name Name of the System.getProperty() property to fetch
079       * @param def  Default value if property not found
080       * @return The value associated with the property or the default value
081       */
082      public static double getDoubleProperty(String name, double def) {
083        String value = System.getProperty(name);
084        if (value == null) {
085          return def;
086        }
087        try {
088          return Double.parseDouble(value);
089        } catch (NumberFormatException _) {
090          return def;
091        }
092      }
093    
094      /**
095       * Returns the boolean value associated with the given system property, or the
096       * default if the property was unset or a string other than "true" or "false."
097       *
098       * @param name Name of the System.getProperty() property to fetch
099       * @param def  Default value if property not found
100       * @return The value associated with the property or the default value
101       */
102      public static boolean getBooleanProperty(String name, boolean def) {
103        String value = System.getProperty(name);
104        if (value == null) {
105          return def;
106        }
107        if (value.equalsIgnoreCase("true")) {
108          return true;
109        }
110        if (value.equalsIgnoreCase("false")) {
111          return false;
112        }
113        return def;
114      }
115    }