X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fdata%2FDefaultBase.java;h=56b37c4d399f813cebe19f0c981c8980015026af;hb=fb73cc41eb8f9ef9bf2181f4295dc74581b25b53;hp=83f3cee794e8b92b227066349ca577eff1bc9db0;hpb=9dc33f50de736f6d98c988c555ed4b14cd47c423;p=demoscenemusic.git
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java
index 83f3cee..56b37c4 100644
--- a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java
+++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java
@@ -22,39 +22,94 @@ import java.util.Map;
import java.util.Map.Entry;
/**
- * TODO
+ * Base implementation of a data container. It stores the ID of a container and
+ * can contain arbitrary attributes.
*
* @author David âBombeâ Roden
*/
public class DefaultBase implements Base {
+ /** The ID of the data container. */
private final String id;
+ /** The attributes of the data container. */
private final Map> attributes = new HashMap>();
+ /** The properties of the data container. */
+ private final Properties properties = new Properties();
+
+ /**
+ * Creates a new data container with the given ID.
+ *
+ * @param id
+ * The ID of the data container
+ */
protected DefaultBase(String id) {
this.id = id;
}
- public String id() {
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getId() {
return id;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Properties getProperties() {
+ return properties;
+ }
+
+ /**
+ * Returns whether the data container contains an attribute with the given
+ * name.
+ *
+ * @param name
+ * The name of the attribute to check for
+ * @return {@code true} if this data container contains an attribute with
+ * the given name, {@code false} otherwise
+ */
protected boolean hasValue(String name) {
return attributes.containsKey(name);
}
- @SuppressWarnings({ "synthetic-access", "unchecked" })
- protected Value value(String name, Class clazz) {
+ /**
+ * Returns the value of the attribute with the given name. If no value for
+ * the given attribute exists, one will be created, stored, and returned.
+ *
+ * @param
+ * The type of the value to return
+ * @param name
+ * The name of the attribute to get
+ * @param clazz
+ * The class of the value
+ * @return The value of the attribute
+ */
+ @SuppressWarnings({ "unchecked" })
+ protected Value getValue(String name, Class clazz) {
if (!attributes.containsKey(name)) {
attributes.put(name, new Value());
}
return (Value) attributes.get(name);
}
- protected boolean dirty() {
+ /**
+ * Returns whether any of the attributes of this data container is dirty,
+ * i.e. it was modified after its initial inception.
+ *
+ * @return {@code true} if any attribute of this data container was modified
+ */
+ protected boolean isDirty() {
for (Value> value : attributes.values()) {
- if (value.dirty()) {
+ if (value.isDirty()) {
return true;
}
}
@@ -70,7 +125,7 @@ public class DefaultBase implements Base {
*/
@Override
public int hashCode() {
- return id().hashCode();
+ return getId().hashCode();
}
/**
@@ -81,7 +136,7 @@ public class DefaultBase implements Base {
if (!(obj instanceof Base)) {
return false;
}
- return id().equals(((Base) obj).id());
+ return getId().equals(((Base) obj).getId());
}
/**
@@ -91,7 +146,7 @@ public class DefaultBase implements Base {
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(getClass().getName());
- stringBuilder.append('[').append("id=").append(id());
+ stringBuilder.append('[').append("id=").append(getId());
for (Entry> attributeEntry : attributes.entrySet()) {
stringBuilder.append(',').append(attributeEntry.getKey()).append('=').append(attributeEntry.getValue().get());
}
@@ -99,18 +154,46 @@ public class DefaultBase implements Base {
return stringBuilder.toString();
}
+ /**
+ * Container for an attribute value stored in a data container.
+ *
+ * @see DefaultBase#attributes
+ * @param
+ * The type of the value
+ * @author David âBombeâ Roden
+ */
protected static class Value {
+ /** The original value. */
private T original;
+ /** Whether the original value has been set. */
private boolean originalSet;
+ /** The current value. */
private T current;
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the current value.
+ *
+ * @return The current value
+ */
public T get() {
return current;
}
+ /**
+ * Sets the current value. If no original value has yet been set, the
+ * given value is also stored as original value.
+ *
+ * @param value
+ * The value to set
+ * @return This value
+ */
public Value set(T value) {
if (!originalSet) {
original = value;
@@ -120,10 +203,24 @@ public class DefaultBase implements Base {
return this;
}
- public boolean dirty() {
+ /**
+ * Returns whether the value of this value does not equal its original
+ * value.
+ *
+ * @return {@code true} if the current value is not equal to the
+ * original value, {@code false} if it is equal to the original
+ * value
+ */
+ public boolean isDirty() {
return (original != null) ? !original.equals(current) : current != null;
}
+ /**
+ * Commits the current value, i.e. sets the original value to the
+ * current value.
+ *
+ * @return This value
+ */
public Value commit() {
original = current;
return this;