Package no.priv.garshol.duke.utils

Source Code of no.priv.garshol.duke.utils.PropertyUtils

package no.priv.garshol.duke.utils;

import java.util.Properties;
import no.priv.garshol.duke.DukeConfigException;

/**
* Utilities for making Java Properties objects easier to deal with.
*/
public class PropertyUtils {

  /**
   * Used for getting required properties, will throw an exception if
   * the property is not specified.
   */
  public static String get(Properties props, String name) {
    String value = props.getProperty(name);
    if (value == null)
      throw new DukeConfigException("Required property " + name +
                                    " not specified");
    return value;
  }

  /**
   * Returns the value of an optional property, if the property is
   * set.  If it is not set defval is returned.
   */
  public static String get(Properties props, String name, String defval) {
    String value = props.getProperty(name);
    if (value == null)
      value = defval;
    return value;
  }

  /**
   * Returns the value of an optional property, if the property is
   * set.  If it is not set defval is returned.
   */
  public static int get(Properties props, String name, int defval) {
    String value = props.getProperty(name);
    if (value == null)
      return defval;
    return Integer.parseInt(value);
  }
 
}
TOP

Related Classes of no.priv.garshol.duke.utils.PropertyUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.