Fix @SuppressWarnings annotation.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DefaultBase.java
1 /*
2  * DemosceneMusic - Base.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.HashMap;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 /**
25  * TODO
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class DefaultBase implements Base {
30
31         private final String id;
32
33         private final Map<String, Value<?>> attributes = new HashMap<String, Value<?>>();
34
35         protected DefaultBase(String id) {
36                 this.id = id;
37         }
38
39         @Override
40         public String getId() {
41                 return id;
42         }
43
44         protected boolean hasValue(String name) {
45                 return attributes.containsKey(name);
46         }
47
48         @SuppressWarnings({ "unchecked" })
49         protected <T> Value<T> getValue(String name, Class<T> clazz) {
50                 if (!attributes.containsKey(name)) {
51                         attributes.put(name, new Value<T>());
52                 }
53                 return (Value<T>) attributes.get(name);
54         }
55
56         protected boolean isDirty() {
57                 for (Value<?> value : attributes.values()) {
58                         if (value.isDirty()) {
59                                 return true;
60                         }
61                 }
62                 return false;
63         }
64
65         //
66         // OBJECT METHODS
67         //
68
69         /**
70          * {@inheritDoc}
71          */
72         @Override
73         public int hashCode() {
74                 return getId().hashCode();
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         @Override
81         public boolean equals(Object obj) {
82                 if (!(obj instanceof Base)) {
83                         return false;
84                 }
85                 return getId().equals(((Base) obj).getId());
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         @Override
92         public String toString() {
93                 StringBuilder stringBuilder = new StringBuilder();
94                 stringBuilder.append(getClass().getName());
95                 stringBuilder.append('[').append("id=").append(getId());
96                 for (Entry<String, Value<?>> attributeEntry : attributes.entrySet()) {
97                         stringBuilder.append(',').append(attributeEntry.getKey()).append('=').append(attributeEntry.getValue().get());
98                 }
99                 stringBuilder.append(']');
100                 return stringBuilder.toString();
101         }
102
103         protected static class Value<T> {
104
105                 private T original;
106
107                 private boolean originalSet;
108
109                 private T current;
110
111                 public T get() {
112                         return current;
113                 }
114
115                 public Value<T> set(T value) {
116                         if (!originalSet) {
117                                 original = value;
118                                 originalSet = true;
119                         }
120                         current = value;
121                         return this;
122                 }
123
124                 public boolean isDirty() {
125                         return (original != null) ? !original.equals(current) : current != null;
126                 }
127
128                 public Value<T> commit() {
129                         original = current;
130                         return this;
131                 }
132
133         }
134
135 }