15740ad568a3958dcf078bba1036b036095e50fe
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / AbstractControlledComponent.java
1 /*
2  * Sonitus - AbstractControlledComponent.java - Copyright © 2013 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.sonitus.data;
19
20 import java.util.List;
21 import java.util.concurrent.atomic.AtomicReference;
22
23 import com.google.common.collect.Lists;
24
25 /**
26  * Abstract {@link ControlledComponent} implementation that takes care of
27  * managing {@link MetadataListener}s.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public abstract class AbstractControlledComponent implements ControlledComponent {
32
33         /** The name of this filter. */
34         private final String name;
35
36         /** The list of metadata listeners. */
37         private final List<MetadataListener> metadataListeners = Lists.newCopyOnWriteArrayList();
38
39         /** The current metadata. */
40         private AtomicReference<Metadata> metadata = new AtomicReference<Metadata>();
41
42         /**
43          * Creates a new abstract controlled component.
44          *
45          * @param name
46          *              The name of the component
47          */
48         protected AbstractControlledComponent(String name) {
49                 this.name = name;
50         }
51
52         //
53         // LISTENER MANAGEMENT
54         //
55
56         @Override
57         public void addMetadataListener(MetadataListener metadataListener) {
58                 metadataListeners.add(metadataListener);
59         }
60
61         @Override
62         public void removeMetadataListener(MetadataListener metadataListener) {
63                 metadataListeners.remove(metadataListener);
64         }
65
66         //
67         // CONTROLLEDCOMPONENT METHODS
68         //
69
70         @Override
71         public String name() {
72                 return name;
73         }
74
75         @Override
76         public Metadata metadata() {
77                 return metadata.get();
78         }
79
80         @Override
81         public void metadataUpdated(Metadata metadata) {
82                 if (metadata.equals(this.metadata.get())) {
83                         return;
84                 }
85                 this.metadata.set(metadata);
86                 fireMetadataUpdated(metadata);
87         }
88
89         //
90         // EVENT METHODS
91         //
92
93         /**
94          * Notifies all registered metadata listeners that the metadata has changed.
95          *
96          * @param metadata
97          *              The new metadata
98          */
99         protected void fireMetadataUpdated(Metadata metadata) {
100                 for (MetadataListener metadataListener : metadataListeners) {
101                         metadataListener.metadataUpdated(this, metadata);
102                 }
103         }
104
105 }