83f3cee794e8b92b227066349ca577eff1bc9db0
[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         public String id() {
40                 return id;
41         }
42
43         protected boolean hasValue(String name) {
44                 return attributes.containsKey(name);
45         }
46
47         @SuppressWarnings({ "synthetic-access", "unchecked" })
48         protected <T> Value<T> value(String name, Class<T> clazz) {
49                 if (!attributes.containsKey(name)) {
50                         attributes.put(name, new Value<T>());
51                 }
52                 return (Value<T>) attributes.get(name);
53         }
54
55         protected boolean dirty() {
56                 for (Value<?> value : attributes.values()) {
57                         if (value.dirty()) {
58                                 return true;
59                         }
60                 }
61                 return false;
62         }
63
64         //
65         // OBJECT METHODS
66         //
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         public int hashCode() {
73                 return id().hashCode();
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public boolean equals(Object obj) {
81                 if (!(obj instanceof Base)) {
82                         return false;
83                 }
84                 return id().equals(((Base) obj).id());
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         public String toString() {
92                 StringBuilder stringBuilder = new StringBuilder();
93                 stringBuilder.append(getClass().getName());
94                 stringBuilder.append('[').append("id=").append(id());
95                 for (Entry<String, Value<?>> attributeEntry : attributes.entrySet()) {
96                         stringBuilder.append(',').append(attributeEntry.getKey()).append('=').append(attributeEntry.getValue().get());
97                 }
98                 stringBuilder.append(']');
99                 return stringBuilder.toString();
100         }
101
102         protected static class Value<T> {
103
104                 private T original;
105
106                 private boolean originalSet;
107
108                 private T current;
109
110                 public T get() {
111                         return current;
112                 }
113
114                 public Value<T> set(T value) {
115                         if (!originalSet) {
116                                 original = value;
117                                 originalSet = true;
118                         }
119                         current = value;
120                         return this;
121                 }
122
123                 public boolean dirty() {
124                         return (original != null) ? !original.equals(current) : current != null;
125                 }
126
127                 public Value<T> commit() {
128                         original = current;
129                         return this;
130                 }
131
132         }
133
134 }