From: David ‘Bombe’ Roden Date: Thu, 26 Jul 2012 09:14:34 +0000 (+0200) Subject: Add custom properties. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6824ff7746ed9f1d6d7b68d24d3d02686f2218ac;p=demoscenemusic.git Add custom properties. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/Base.java b/src/main/java/net/pterodactylus/demoscenemusic/data/Base.java index e187041..56559ce 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/Base.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/Base.java @@ -31,4 +31,11 @@ public interface Base { */ public String getId(); + /** + * Returns the properties associated with this object. + * + * @return The properties of this object + */ + public Properties getProperties(); + } diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java index 345ebd2..56b37c4 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultBase.java @@ -35,6 +35,9 @@ public class DefaultBase implements Base { /** 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. * @@ -58,6 +61,14 @@ public class DefaultBase implements Base { } /** + * {@inheritDoc} + */ + @Override + public Properties getProperties() { + return properties; + } + + /** * Returns whether the data container contains an attribute with the given * name. * diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/Properties.java b/src/main/java/net/pterodactylus/demoscenemusic/data/Properties.java new file mode 100644 index 0000000..dfdb836 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/Properties.java @@ -0,0 +1,100 @@ +/* + * DemosceneMusic - Properties.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.demoscenemusic.data; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Container for custom properties that can be attached to all data containers. + * + * @see Base#getProperties() + * @author David ‘Bombe’ Roden + */ +public class Properties implements Iterable> { + + /** The properties. */ + private final Map properties = new HashMap(); + + // + // ACCESSORS + // + + /** + * Returns the value of the property with the given name. + * + * @param property + * The name of the property + * @return The value of the property, or {@code null} if the property with + * the given name does not exist + */ + public String get(String property) { + return properties.get(property); + } + + /** + * Sets the value of the property with the given name. + * + * @param property + * The name of the property + * @param value + * The new value of the property + * @return These properties + */ + public Properties set(String property, String value) { + properties.put(property, value); + return this; + } + + /** + * Removes the property with the given name. + * + * @param property + * The name of the property + * @return These properties + */ + public Properties remove(String property) { + properties.remove(property); + return this; + } + + /** + * Removes all properties. + * + * @return These properties + */ + public Properties removeAll() { + properties.clear(); + return this; + } + + // + // ITERABLE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public Iterator> iterator() { + return properties.entrySet().iterator(); + } + +}