From: David ‘Bombe’ Roden Date: Wed, 18 Apr 2012 04:31:46 +0000 (+0200) Subject: Add attributes to base class. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6c850a66c258f3b59e62bd60c794137625bbc82a;p=demoscenemusic.git Add attributes to base class. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java b/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java index 2f7f3c7..4a35db1 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java @@ -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> attributes = new HashMap>(); + protected AbstractBase(String id) { this.id = id; } @@ -34,4 +39,38 @@ public class AbstractBase { return id; } + @SuppressWarnings({ "synthetic-access", "unchecked" }) + protected Value value(String name, Class clazz) { + if (!attributes.containsKey(name)) { + attributes.put(name, new Value()); + } + return (Value) attributes.get(name); + } + + protected static class Value { + + private T original; + + private T current; + + public T get() { + return current; + } + + public Value set(T value) { + current = value; + return this; + } + + public boolean dirty() { + return (original != null) ? !original.equals(current) : current != null; + } + + public Value commit() { + original = current; + return this; + } + + } + }