From 6c850a66c258f3b59e62bd60c794137625bbc82a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 18 Apr 2012 06:31:46 +0200 Subject: [PATCH] Add attributes to base class. --- .../demoscenemusic/data/AbstractBase.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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; + } + + } + } -- 2.7.4