Don’t require overriding valueSet() anymore, a do-nothing default is fine.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / controller / AbstractController.java
1 /*
2  * Sonitus - AbstractController.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.controller;
19
20 import net.pterodactylus.sonitus.data.Controller;
21
22 /**
23  * Base {@link Controller} implementation that does housekeeping for the common
24  * values. Additional functionality (or an arbitrary mapping for the values of a
25  * controller) can be added in subclasses.
26  * <p/>
27  * This implementation is not thread-safe.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public abstract class AbstractController<V extends Comparable<V>> implements Controller<V> {
32
33         /** The name of this controller. */
34         private final String name;
35
36         /** The minimum value of this controller. */
37         private final V minimum;
38
39         /** The maximum value of this controller. */
40         private final V maximum;
41
42         /** Whether this controller has a “center” position. */
43         private final boolean centered;
44
45         /** The current value of this controller. */
46         private V value;
47
48         /**
49          * Creates a new abstract controller.
50          *
51          * @param name
52          *              The name of the controller
53          * @param minimum
54          *              The minimum value of the controller
55          * @param maximum
56          *              The maximum value of the controller
57          * @param centered
58          *              {@code true} if this controller has a “center” position, {@code false}
59          *              otherwise
60          * @param currentValue
61          */
62         public AbstractController(String name, V minimum, V maximum, boolean centered, V currentValue) {
63                 this.name = name;
64                 this.minimum = minimum;
65                 this.maximum = maximum;
66                 this.centered = centered;
67                 value = currentValue;
68         }
69
70         //
71         // CONTROLLER METHODS
72         //
73
74         @Override
75         public String name() {
76                 return name;
77         }
78
79         @Override
80         public V minimum() {
81                 return minimum;
82         }
83
84         @Override
85         public V maximum() {
86                 return maximum;
87         }
88
89         @Override
90         public boolean centered() {
91                 return centered;
92         }
93
94         @Override
95         public V value() {
96                 return value;
97         }
98
99         @Override
100         public void value(V value) {
101                 V newValue = (value.compareTo(minimum) < 0) ? minimum : ((value.compareTo(maximum) > 0) ? maximum : value);
102                 if (newValue.compareTo(this.value) != 0) {
103                         this.value = newValue;
104                         valueSet(newValue);
105                 }
106         }
107
108         //
109         // SUBCLASS METHODS
110         //
111
112         /**
113          * Adjusts the controller. This method is called from {@link
114          * #value(Comparable)} if the new value is different from the current value.
115          * Also, the value is clamped to fit within the range of this controller.
116          * <p/>
117          * This implementation does nothing.
118          *
119          * @param value
120          *              The new value
121          */
122         protected void valueSet(V value) {
123                 /* do nothing. */
124         }
125
126 }