JEphem Informatic Trail tig source code TigProperties.java
//*********************************************************************************
// class tig.prefs.TigProperties
//*********************************************************************************
package tig;

import tig.Strings;

import java.util.Properties;
import java.lang.reflect.Field;
/******************************************************************
Superclass for Preferences classes.
<BR>The <CODE>getXxxProperty()</CODE> method behave like method <CODE>getProperty(String key)</CODE>, inherited from <CODE>Properties</CODE> class : if the property can't be correctly retrieved, an exception is thrown.

@author Thierry Graff
@history sep 23 2001 : Creation
@history feb 04 2002 : Added default mechanism.
                       Renamed TigProperties from TigPrefs.

@todo add a method : public abstract TigProperties getDefault() ???
@todo add a method : public abstract String getPath() ???
*****************************************************************/
public class TigProperties extends Properties{

  //=================================================================================
  //                            PUBLIC STATIC METHODS
  //=================================================================================

  //******************************************************************
  /** Convenient method to get a <CODE>static final int</CODE> field of a class, using reflection.
  <BR>For example, if you have this piece of code in a class :
  <PRE>package mypackage;
  public class MyClass{
    public static final int MY_CONSTANT = 8;
  }</PRE>
  The call : <CODE>TigBundle.getIntConstant("My_CONSTANT", "mypackage.MyClass");</CODE> will then return 8.
  <BR>
  @throws RuntimeException If the property can't be correctly retrieved.
  */
  public static int getIntConstant(String constantName, String className){
    try{
      Class theClass = Class.forName(className);
      Field field = theClass.getDeclaredField(constantName);
      return field.getInt(null);
    }
    catch(Exception e){
      throw new RuntimeException(e.toString());
    }
  }// end getIntConstant

  //=================================================================================
  //                            PUBLIC METHODS
  //=================================================================================

  //************************** Integers ****************************************
  /** Returns a property of type <CODE>int</CODE>.
  @throws NumberFormatException If the property can't be retrieved, or can't be converted to an integer.
  */
  public int getIntProperty(String key){
    return Integer.parseInt(this.getProperty(key));
  }// end getIntProperty

  /** Returns a property of type <CODE>int</CODE>.
  <BR>If the property can't be retrieved, or can't be converted to an integer,
  the default value is returned.
  */
  public int getIntProperty(String key, int defaultValue){
    try{
      int i = Integer.parseInt(this.getProperty(key));
      return i;
    }
    catch(Exception e){
      return defaultValue;
    }
  }// end getIntProperty

  /** Sets a property of type <CODE>int</CODE>. */
  public void setIntProperty(String key, int intValue){
    this.setProperty(key, Integer.toString(intValue));
  }// end setIntProperty

  //************************** Doubles ****************************************
  /** Returns a property of type <CODE>double</CODE>.
  <BR>If the property can't be retrieved, or if it can't be converted to a double,
  <CODE>null</CODE> is returned.
  */
  public double getDoubleProperty(String key){
    return Double.parseDouble(this.getProperty(key));
  }// end getDoubleProperty

  /** Returns a property of type <CODE>double</CODE>.
  <BR>If the property can't be retrieved, or can't be converted to a double,
  the default value is returned.
  */
  public double getDoubleProperty(String key, double defaultValue){
    try{
      double d = Double.parseDouble(this.getProperty(key));
      return d;
    }
    catch (Exception e){
      return defaultValue;
    }
  }// end getDoubleProperty

  /** Sets a property of type <CODE>double</CODE>. */
  public void setDoubleProperty(String key, double doubleValue){
    this.setProperty(key, Double.toString(doubleValue));
  }// end setDoubleProperty

  //************************** Booleans ****************************************
  /** Returns a property of type <CODE>boolean</CODE>.
  <LI>Returns <CODE>true</CODE> if the property exists and equals (ignoring case) to "true".</LI>
  <LI>Returns <CODE>false</CODE> if the property exists and equals (ignoring case) to "false".</LI>
  <LI>A <CODE>RuntimeException</CODE> is thrown otherwise.</LI>
   */
  public boolean getBooleanProperty(String key){
    String value = this.getProperty(key);
    if(value == null) throw new RuntimeException("Parameter 'key' can't be converted to a boolean");
    else if(value.equalsIgnoreCase("true")) return true;
    else if(value.equalsIgnoreCase("false")) return false;
    else throw new RuntimeException("Parameter 'key' can't be converted to a boolean");
  }// end getBooleanProperty

  /** Returns a property of type <CODE>boolean</CODE>.
  <LI>Returns <CODE>true</CODE> if the property exists and equals (ignoring case) to "true".</LI>
  <LI>Returns <CODE>false</CODE> if the property exists and equals (ignoring case) to "false".</LI>
  <LI>'defaultValue' is returned otherwise.</LI>
   */
  public boolean getBooleanProperty(String key, boolean defaultValue){
    String value = this.getProperty(key);
    if(value == null) return defaultValue;
    else if(value.equalsIgnoreCase("true")) return true;
    else if(value.equalsIgnoreCase("false")) return false;
    else return defaultValue;
  }// end getBooleanProperty

  /** Sets a property of type <CODE>boolean</CODE>. */
  public void setBooleanProperty(String key, boolean booleanValue){
    String strValue;
    if (booleanValue) strValue = "true";
    else strValue = "false";
    this.setProperty(key, strValue);
  }// end getBooleanProperty

  //************************** Integer arrays ****************************************
  /** Returns a property of type <CODE>int[]</CODE>.
  <BR>The <CODE>value</CODE> corresponding to <CODE>key</CODE> is a Sting, and must contain a list of
  substrings which can be parsed to integers, and separated by comas.
  <BR>For example, <CODE>"12, 23, 34"</CODE> is a correct value.
  @throws RuntimeException If the property corresponding to  'key' can't be parsed that way.
  */
  public int[] getIntArrayProperty(String key){
    String[] values = Strings.stringToStringArray(this.getProperty(key), ',');
    int[] res = new int[values.length];
    for (int i = 0; i < values.length; i++){
      try{
        res[i] = Integer.parseInt(values[i]);
      }
      catch(Exception e){
        throw new RuntimeException("'" + values[i] + "' can't be converted to an integer");
      }
    }
    return res;
  }// end getIntArrayProperty

  /** Returns a property of type <CODE>int[]</CODE>.
  <BR>The <CODE>value</CODE> corresponding to <CODE>key</CODE> is a Sting, and must contain a list of
  substrings which can be parsed to integers, and separated by comas.
  <BR>For example, <CODE>"12, 23, 34"</CODE> is a correct value.
  <BR><BR>If the property corresponding to  'key' can't be parsed that way, 'defaultValue' is returned.
  */
  public int[] getIntArrayProperty(String key, int[] defaultValue){
    try{
      String[] values = Strings.stringToStringArray(this.getProperty(key), ',');
      int[] res = new int[values.length];
      for (int i = 0; i < values.length; i++){
        res[i] = Integer.parseInt(values[i]);
      }
      return res;
    }
    catch(Exception e){
      return defaultValue;
    }
  }// end getIntArrayProperty

  /** Sets a property of type <CODE>int[]</CODE>.
  <BR>The <CODE>int[]</CODE> is transformed to a String.
  <BR>Ex : if 'intArray' contains 1, 4, 7 and 5, it will be stored as "<CODE>1, 4, 7, 5</CODE>".
  <BR><CODE>int[]</CODE> stored this way can be retrived with {@link #getIntArrayProperty(String)}
  */
  public void setIntArrayProperty(String key, int[] intArray){
    this.setProperty(key, Strings.intArrayToString(intArray));
  }// end setIntArrayProperty

}// end class TigProperties