Class PropertiesFile


  • public class PropertiesFile
    extends java.lang.Object
    A properties object backed by a specified file without throwing exceptions. The properties are automatically loaded from the file when the first property is requested, but the save method must be called before changes are saved to the file.
    Version:
    $Revision: 1.4 $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Properties delegate
      actual properties
      private boolean dirty  
      private java.lang.String header
      the header string
      private long lastStore
      time of last modification, lazy loaded on getProperty
      private LockableFile lockableFile
      the file to save to
      private static Logger LOG  
    • Constructor Summary

      Constructors 
      Constructor Description
      PropertiesFile​(java.io.File file)
      Create a properties object backed by the specified file.
      PropertiesFile​(java.io.File file, java.lang.String header)
      Create a properties object backed by the specified file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsPropertyKey​(java.lang.String key)  
      boolean containsPropertyValue​(java.lang.String value)  
      java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.String>> entrySet()  
      java.lang.String getProperty​(java.lang.String key)  
      java.lang.String getProperty​(java.lang.String key, java.lang.String defaultValue)  
      java.io.File getStoreFile()  
      boolean isHeldByCurrentThread()  
      boolean load()
      Ensures that the file backing these properties has been loaded; call this method before calling any method defined by a superclass.
      void lock()  
      java.lang.String remove​(java.lang.String key)
      Removes the key (and its corresponding value) from the properties.
      java.lang.Object setProperty​(java.lang.String key, java.lang.String value)
      Sets the value for the specified key.
      void store()
      Saves the properties to the file.
      boolean tryLock()  
      void unlock()
      Unlocks the file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final Logger LOG
      • delegate

        private final java.util.Properties delegate
        actual properties
      • lockableFile

        private final LockableFile lockableFile
        the file to save to
      • header

        private final java.lang.String header
        the header string
      • lastStore

        private long lastStore
        time of last modification, lazy loaded on getProperty
      • dirty

        private boolean dirty
    • Constructor Detail

      • PropertiesFile

        public PropertiesFile​(java.io.File file)
        Create a properties object backed by the specified file.
        Parameters:
        file - the file to save and load to
      • PropertiesFile

        public PropertiesFile​(java.io.File file,
                              java.lang.String header)
        Create a properties object backed by the specified file.
        Parameters:
        file - the file to save and load to
        header - the file header
    • Method Detail

      • getProperty

        public java.lang.String getProperty​(java.lang.String key)
        Returns:
        the value of the specified key, or null if the key does not exist.
      • getProperty

        public java.lang.String getProperty​(java.lang.String key,
                                            java.lang.String defaultValue)
        Returns:
        the value of the specified key, or the default value if the key does not exist.
      • setProperty

        public java.lang.Object setProperty​(java.lang.String key,
                                            java.lang.String value)
        Sets the value for the specified key.
        Returns:
        the previous value
      • containsPropertyKey

        public boolean containsPropertyKey​(java.lang.String key)
      • containsPropertyValue

        public boolean containsPropertyValue​(java.lang.String value)
      • clear

        public void clear()
      • remove

        public java.lang.String remove​(java.lang.String key)
        Removes the key (and its corresponding value) from the properties.
        Returns:
        the previous value
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.String>> entrySet()
      • getStoreFile

        public java.io.File getStoreFile()
        Returns:
        the file backing this properties object.
      • load

        public boolean load()
        Ensures that the file backing these properties has been loaded; call this method before calling any method defined by a superclass.
        Returns:
        true, if file was (re-)loaded false, if file was still current
      • store

        public void store()
        Saves the properties to the file.
      • lock

        public void lock()
      • tryLock

        public boolean tryLock()
      • unlock

        public void unlock()
        Unlocks the file. Does not do anything if not holding the lock.
      • isHeldByCurrentThread

        public boolean isHeldByCurrentThread()