X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fdata%2FAbstractBase.java;h=ad5cfd8f7e27a52da793e65b94a17e8a53114934;hb=5d19c11df7ca4a969fbfe7583ca934174fb348bf;hp=2f7f3c75be0fb425783fb59341e5ea1efbf7db41;hpb=a2983e36c9603ddb43f7016f16e445e1b26bd885;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java b/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java index 2f7f3c7..ad5cfd8 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/AbstractBase.java @@ -17,15 +17,21 @@ package net.pterodactylus.demoscenemusic.data; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + /** * TODO * * @author David ‘Bombe’ Roden */ -public class AbstractBase { +public abstract class AbstractBase implements Base { private final String id; + private final Map> attributes = new HashMap>(); + protected AbstractBase(String id) { this.id = id; } @@ -34,4 +40,91 @@ 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 boolean dirty() { + for (Value value : attributes.values()) { + if (value.dirty()) { + return true; + } + } + return false; + } + + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + return id().hashCode(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Base)) { + return false; + } + return id().equals(((Base) obj).id()); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(getClass().getName()); + stringBuilder.append('[').append("id=").append(id()); + for (Entry> attributeEntry : attributes.entrySet()) { + stringBuilder.append(',').append(attributeEntry.getKey()).append('=').append(attributeEntry.getValue().get()); + } + stringBuilder.append(']'); + return stringBuilder.toString(); + } + + protected static class Value { + + private T original; + + private boolean originalSet; + + private T current; + + public T get() { + return current; + } + + public Value set(T value) { + if (!originalSet) { + original = value; + originalSet = true; + } + current = value; + return this; + } + + public boolean dirty() { + return (original != null) ? !original.equals(current) : current != null; + } + + public Value commit() { + original = current; + return this; + } + + } + }