2 * DemosceneMusic - Properties.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.data;
20 import java.util.Iterator;
22 import java.util.Map.Entry;
23 import java.util.TreeMap;
26 * Container for custom properties that can be attached to all data containers.
28 * @see Base#getProperties()
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Properties implements Iterable<Entry<String, String>> {
33 /** The properties. */
34 private final Map<String, String> properties = new TreeMap<String, String>();
36 /** The “dirty” flag. */
37 private boolean dirty = false;
44 * Returns the value of the property with the given name.
47 * The name of the property
48 * @return The value of the property, or {@code null} if the property with
49 * the given name does not exist
51 public String get(String property) {
52 return properties.get(property);
56 * Sets the value of the property with the given name.
59 * The name of the property
61 * The new value of the property
62 * @return These properties
64 public Properties set(String property, String value) {
65 if (!properties.containsKey(property) || (((value != null) && (!value.equals(properties.get(property))) || ((value == null) && (properties.get(property) != null))))) {
68 properties.put(property, value);
73 * Removes the property with the given name.
76 * The name of the property
77 * @return These properties
79 public Properties remove(String property) {
80 if (properties.containsKey(property)) {
83 properties.remove(property);
88 * Removes all properties.
90 * @return These properties
92 public Properties removeAll() {
93 if (!properties.isEmpty()) {
101 * Returns whether these properties have been modified.
103 * @return {@code true} if the properties have been modified since they were
104 * created or the last time {@link #resetDirty()} was called,
105 * {@code false} if they have not been modified
107 public boolean isDirty() {
112 * Resets the dirty flag.
114 * @return These properties
116 public Properties resetDirty() {
129 public Iterator<Entry<String, String>> iterator() {
130 return properties.entrySet().iterator();