Always keep the properties sorted.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / Properties.java
1 /*
2  * DemosceneMusic - Properties.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.data;
19
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.TreeMap;
24
25 /**
26  * Container for custom properties that can be attached to all data containers.
27  *
28  * @see Base#getProperties()
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class Properties implements Iterable<Entry<String, String>> {
32
33         /** The properties. */
34         private final Map<String, String> properties = new TreeMap<String, String>();
35
36         /** The “dirty” flag. */
37         private boolean dirty = false;
38
39         //
40         // ACCESSORS
41         //
42
43         /**
44          * Returns the value of the property with the given name.
45          *
46          * @param property
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
50          */
51         public String get(String property) {
52                 return properties.get(property);
53         }
54
55         /**
56          * Sets the value of the property with the given name.
57          *
58          * @param property
59          *            The name of the property
60          * @param value
61          *            The new value of the property
62          * @return These properties
63          */
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))))) {
66                         dirty = true;
67                 }
68                 properties.put(property, value);
69                 return this;
70         }
71
72         /**
73          * Removes the property with the given name.
74          *
75          * @param property
76          *            The name of the property
77          * @return These properties
78          */
79         public Properties remove(String property) {
80                 if (properties.containsKey(property)) {
81                         dirty = true;
82                 }
83                 properties.remove(property);
84                 return this;
85         }
86
87         /**
88          * Removes all properties.
89          *
90          * @return These properties
91          */
92         public Properties removeAll() {
93                 if (!properties.isEmpty()) {
94                         dirty = true;
95                 }
96                 properties.clear();
97                 return this;
98         }
99
100         /**
101          * Returns whether these properties have been modified.
102          *
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
106          */
107         public boolean isDirty() {
108                 return dirty;
109         }
110
111         /**
112          * Resets the dirty flag.
113          *
114          * @return These properties
115          */
116         public Properties resetDirty() {
117                 dirty = false;
118                 return this;
119         }
120
121         //
122         // ITERABLE METHODS
123         //
124
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         public Iterator<Entry<String, String>> iterator() {
130                 return properties.entrySet().iterator();
131         }
132
133 }