Features

Advantages of using Java Config

  • Use a single call to validate your entire property file;
  • Use type safe method calls (getFtpPortnumber());
  • Display an overview of used properties and their values at startup time;
  • Display an overview of properties that are available in the property file but no (longer) used by your application;
  • Advanced property value parsing (filesize = 1.5 KB, ftp.timeout=1 minute and 15 seconds);
  • Simple to use; in a few minutes you'll have it up-and-running;
  • Light and non-intrusive: works with any Java .properties file and requires no external libraries;
  • Use the existing code and effort of Java Config, and spend your energy on interesting problems instead;
  • Use a well-tested Config package, with a unittest coverage of well over 90%.

Supported data-types

The Java Config class comes with a number of supported data-types that can be used out-of-the-box. Also, it is very easy to add a new data-type to your Config class.

NameDescriptionExample
StringBasic java.lang.String. Any value in the property file is accepted, the only requirement is that at least the key exists (in which case an empty string will be returned). Returned Strings are never null, and are always trimmed from pre- and post-fix whitespace.default.css
intAn integer.1023, or -99993
java.net.URLAn URL. The syntax of the URL must be valid, and the protocol in the URL must be one supported by the Java VM. This is validated using the java.net.URL(String string) constructor. No check is done on whether or not the URL exists or is reachable from the current computer.http://www.chess.nl/document.html
java.util.DateA Date: a specific instant in time. The String given must be parseable by the DateFormat that can be retrieved by the following call: DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, Locale.US) . When using a SimpleDateFormat the following String can be used, using Locale.US: MMMM d, yyyy hh:mm:ss a November 26, 2003 2:09:28 PM
booleanA boolean. A number of different values are accepted: on, true, yes and 1, and off, false, no and 0.yes, or no
Size in bytesReturns a long with a size in bytes. The format of the value is a decimal number, optionally followed by a unit indication (KB, MB, GB).

The decimal number may contain a comma (to group numbers) or a dot (to indicate a fraction), it's an American format (Locale.US). A dot can only be used when a unit is given (as the result can only be integral). A value that can not be converted to a long without losing precision will not be accepted.
10KB (meaning 10*1024 bytes), or 777 (meaning 777 bytes)



Not okay: 0.23 KB, as this would result in 235.52 bytes, which cannot be expressed in a long.
Duration in millisecondsReturns a long with a duration in milliseconds. The format of the input String is human-readable text, such as "5 minutes", "one day, 7 hours and a minute", or simply "5000ms".



A time-unit is required (weeks, days, hours, minutes, seconds, milliseconds).
5 minutes (meaning 5 * 60 * 1000 milliseconds), 2 s (meaning 2000 milliseconds).
MessageFormatPatternReturns a String that is a pattern that will work nicely with Java's MessageFormat class. The pattern is tested by rendering it with a number of possible values. There {0,choice,0#are no files|1#is one file|1<are {0,number, integer} files} in this directory.

All the numeric primitives that are supported by Java Config also do a check on the maximum-size of their primitive. That means that an overflow cannot occur because of a very large String-value that wouldn't fit in an integer or long.

Feedback

Your feedback is welcome.