nu.studer.java.util
Class OrderedProperties

java.lang.Object
  extended by nu.studer.java.util.OrderedProperties
All Implemented Interfaces:
java.io.Serializable

public final class OrderedProperties
extends java.lang.Object
implements java.io.Serializable

This class provides an alternative to the JDK's Properties class. It fixes the design flaw of using inheritance over composition, while keeping up the same APIs as the original class. Keys and values are guaranteed to be of type String.

This class is not synchronized, contrary to the original implementation.

As additional functionality, this class keeps its properties in a well-defined order. By default, the order is the one in which the individual properties have been added, either through explicit API calls or through reading them top-to-bottom from a properties file.

Also, an optional flag can be set to omit the comment that contains the current date when storing the properties to a properties file.

Currently, this class does not support the concept of default properties, contrary to the original implementation.

Note that this implementation is not synchronized. If multiple threads access ordered properties concurrently, and at least one of the threads modifies the ordered properties structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the properties.

Note that the actual (and quite complex) logic of parsing and storing properties from and to a stream is delegated to the Properties class from the JDK.

See Also:
Properties, Serialized Form

Nested Class Summary
static class OrderedProperties.OrderedPropertiesBuilder
          Builder for OrderedProperties instances.
 
Constructor Summary
OrderedProperties()
          Creates a new instance that will keep the properties in the order they have been added.
 
Method Summary
 boolean containsProperty(java.lang.String key)
          Returns true if there is a property with the specified key.
static OrderedProperties copyOf(OrderedProperties source)
          Creates a new instance that will have both the same property entries and the same behavior as the given source.
 java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrySet()
          See Hashtable.entrySet().
 boolean equals(java.lang.Object other)
           
 java.lang.String getProperty(java.lang.String key)
          See Properties.getProperty(String).
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
          See Properties.getProperty(String, String).
 int hashCode()
           
 boolean isEmpty()
          See Hashtable.isEmpty().
 void load(java.io.InputStream stream)
          See Properties.load(InputStream).
 void load(java.io.Reader reader)
          See Properties.load(Reader).
 void loadFromXML(java.io.InputStream stream)
          See Properties.loadFromXML(InputStream).
 java.util.Enumeration<java.lang.String> propertyNames()
          See Properties.propertyNames().
 java.lang.String removeProperty(java.lang.String key)
          Removes the property with the specified key, if it is present.
 java.lang.String setProperty(java.lang.String key, java.lang.String value)
          See Properties.setProperty(String, String).
 int size()
          See Hashtable.size().
 void store(java.io.OutputStream stream, java.lang.String comments)
          See Properties.store(OutputStream, String).
 void store(java.io.Writer writer, java.lang.String comments)
          See Properties.store(Writer, String).
 void storeToXML(java.io.OutputStream stream, java.lang.String comment)
          See Properties.storeToXML(OutputStream, String).
 void storeToXML(java.io.OutputStream stream, java.lang.String comment, java.lang.String encoding)
          See Properties.storeToXML(OutputStream, String, String).
 java.util.Set<java.lang.String> stringPropertyNames()
          See Properties.stringPropertyNames().
 java.util.Properties toJdkProperties()
          Convert this instance to a Properties instance.
 java.lang.String toString()
          See Hashtable.toString().
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OrderedProperties

public OrderedProperties()
Creates a new instance that will keep the properties in the order they have been added. Other than the ordering of the keys, this instance behaves like an instance of the Properties class.

Method Detail

getProperty

public java.lang.String getProperty(java.lang.String key)
See Properties.getProperty(String).


getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue)
See Properties.getProperty(String, String).


setProperty

public java.lang.String setProperty(java.lang.String key,
                                    java.lang.String value)
See Properties.setProperty(String, String).


removeProperty

public java.lang.String removeProperty(java.lang.String key)
Removes the property with the specified key, if it is present. Returns the value of the property, or null if there was no property with the specified key.

Parameters:
key - the key of the property to remove
Returns:
the previous value of the property, or null if there was no property with the specified key

containsProperty

public boolean containsProperty(java.lang.String key)
Returns true if there is a property with the specified key.

Parameters:
key - the key whose presence is to be tested

size

public int size()
See Hashtable.size().


isEmpty

public boolean isEmpty()
See Hashtable.isEmpty().


propertyNames

public java.util.Enumeration<java.lang.String> propertyNames()
See Properties.propertyNames().


stringPropertyNames

public java.util.Set<java.lang.String> stringPropertyNames()
See Properties.stringPropertyNames().


entrySet

public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrySet()
See Hashtable.entrySet().


load

public void load(java.io.InputStream stream)
          throws java.io.IOException
See Properties.load(InputStream).

Throws:
java.io.IOException

load

public void load(java.io.Reader reader)
          throws java.io.IOException
See Properties.load(Reader).

Throws:
java.io.IOException

loadFromXML

public void loadFromXML(java.io.InputStream stream)
                 throws java.io.IOException,
                        java.util.InvalidPropertiesFormatException
See Properties.loadFromXML(InputStream).

Throws:
java.io.IOException
java.util.InvalidPropertiesFormatException

store

public void store(java.io.OutputStream stream,
                  java.lang.String comments)
           throws java.io.IOException
See Properties.store(OutputStream, String).

Throws:
java.io.IOException

store

public void store(java.io.Writer writer,
                  java.lang.String comments)
           throws java.io.IOException
See Properties.store(Writer, String).

Throws:
java.io.IOException

storeToXML

public void storeToXML(java.io.OutputStream stream,
                       java.lang.String comment)
                throws java.io.IOException
See Properties.storeToXML(OutputStream, String).

Throws:
java.io.IOException

storeToXML

public void storeToXML(java.io.OutputStream stream,
                       java.lang.String comment,
                       java.lang.String encoding)
                throws java.io.IOException
See Properties.storeToXML(OutputStream, String, String).

Throws:
java.io.IOException

toJdkProperties

public java.util.Properties toJdkProperties()
Convert this instance to a Properties instance.

Returns:
the Properties instance

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
See Hashtable.toString().

Overrides:
toString in class java.lang.Object

copyOf

public static OrderedProperties copyOf(OrderedProperties source)
Creates a new instance that will have both the same property entries and the same behavior as the given source.

Note that the source instance and the copy instance will share the same comparator instance if a custom ordering had been configured on the source.

Parameters:
source - the source to copy from
Returns:
the copy