Add attributes to base class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 04:31:46 +0000 (06:31 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 04:31:46 +0000 (06:31 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java

index 2f7f3c7..4a35db1 100644 (file)
@@ -17,6 +17,9 @@
 
 package net.pterodactylus.demoscenemusic.data;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * TODO
  *
@@ -26,6 +29,8 @@ public class AbstractBase {
 
        private final String id;
 
+       private final Map<String, Value<?>> attributes = new HashMap<String, Value<?>>();
+
        protected AbstractBase(String id) {
                this.id = id;
        }
@@ -34,4 +39,38 @@ public class AbstractBase {
                return id;
        }
 
+       @SuppressWarnings({ "synthetic-access", "unchecked" })
+       protected <T> Value<T> value(String name, Class<T> clazz) {
+               if (!attributes.containsKey(name)) {
+                       attributes.put(name, new Value<T>());
+               }
+               return (Value<T>) attributes.get(name);
+       }
+
+       protected static class Value<T> {
+
+               private T original;
+
+               private T current;
+
+               public T get() {
+                       return current;
+               }
+
+               public Value<T> set(T value) {
+                       current = value;
+                       return this;
+               }
+
+               public boolean dirty() {
+                       return (original != null) ? !original.equals(current) : current != null;
+               }
+
+               public Value<T> commit() {
+                       original = current;
+                       return this;
+               }
+
+       }
+
 }