Add “dirty” flag to properties.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 11:44:14 +0000 (13:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 11:44:14 +0000 (13:44 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/Properties.java

index dfdb836..3b72223 100644 (file)
@@ -33,6 +33,9 @@ public class Properties implements Iterable<Entry<String, String>> {
        /** The properties. */
        private final Map<String, String> properties = new HashMap<String, String>();
 
+       /** The “dirty” flag. */
+       private boolean dirty = false;
+
        //
        // ACCESSORS
        //
@@ -59,6 +62,9 @@ public class Properties implements Iterable<Entry<String, String>> {
         * @return These properties
         */
        public Properties set(String property, String value) {
+               if (!properties.containsKey(property) || (((value != null) && (!value.equals(properties.get(property))) || ((value == null) && (properties.get(property) != null))))) {
+                       dirty = true;
+               }
                properties.put(property, value);
                return this;
        }
@@ -71,6 +77,9 @@ public class Properties implements Iterable<Entry<String, String>> {
         * @return These properties
         */
        public Properties remove(String property) {
+               if (properties.containsKey(property)) {
+                       dirty = true;
+               }
                properties.remove(property);
                return this;
        }
@@ -81,10 +90,34 @@ public class Properties implements Iterable<Entry<String, String>> {
         * @return These properties
         */
        public Properties removeAll() {
+               if (!properties.isEmpty()) {
+                       dirty = true;
+               }
                properties.clear();
                return this;
        }
 
+       /**
+        * Returns whether these properties have been modified.
+        *
+        * @return {@code true} if the properties have been modified since they were
+        *         created or the last time {@link #resetDirty()} was called,
+        *         {@code false} if they have not been modified
+        */
+       public boolean isDirty() {
+               return dirty;
+       }
+
+       /**
+        * Resets the dirty flag.
+        *
+        * @return These properties
+        */
+       public Properties resetDirty() {
+               dirty = false;
+               return this;
+       }
+
        //
        // ITERABLE METHODS
        //