Code example for Properties

Methods: containsKey, getProperty

0
   * @param  name           The property name 
   */ 
  public static int getRequiredIntParam(Properties props, String name)
                                        throws IllegalArgumentException 
  { 
    if(props.containsKey(name))
      return getRequiredIntParam(props, name, -1);
    else 
      throw new IllegalArgumentException("Missing required property '" + name + "'");
  } 
 
  /** 
   * Read an integer from the properties instance 
   * @param  props          The properties to read from 
   * @param  name           The property name 
   * @param  defaultValue   The default value to use if the property is not found 
   * @return the integer value 
   */ 
  public static int getRequiredIntParam(Properties props, String name, int defaultValue)
  { 
    return getRequiredIntParamInRange(props, name, defaultValue, Integer.MIN_VALUE, Integer.MAX_VALUE);
  } 
 
  /** 
   * Read an integer from the properties instance. Throw an exception 
   * if the value is not in the given range (inclusive) 
   * @param props           The properties to read from 
   * @param name            The property name 
   * @param defaultValue    The default value to use if the property is not found 
   * @param rangeMin        The minimum value of the range in which the value must fall (inclusive) 
   * @param rangeMax        The maximum value of the range in which the value must fall (inclusive) 
   * @throws IllegalArgumentException If the value is not in the given range 
   * @return the integer value 
   */ 
  public static int getRequiredIntParamInRange(Properties props, String name, int defaultValue,
                                               int rangeMin, int rangeMax)
                                               throws IllegalArgumentException 
  { 
    int value = defaultValue;
    if(props.containsKey(name))
        value = Integer.parseInt(props.getProperty(name));
 
    if(value < rangeMin || value > rangeMax)
      throw new IllegalArgumentException(name + " has value " + value + " which is not in the range (" + rangeMin + "," + rangeMax + ").");
 
    return value;