Make controller’s value’s type variable.
[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 minimum value of this controller. */
34         private final V minimum;
35
36         /** The maximum value of this controller. */
37         private final V maximum;
38
39         /** Whether this controller has a “center” position. */
40         private final boolean centered;
41
42         /** The current value of this controller. */
43         private V value;
44
45         /**
46          * Creates a new abstract controller.
47          *
48          * @param minimum
49          *              The minimum value of the controller
50          * @param maximum
51          *              The maximum value of the controller
52          * @param centered
53          *              {@code true} if this controller has a “center” position, {@code false}
54          *              otherwise
55          * @param currentValue
56          *              The current value of this controller
57          */
58         public AbstractController(V minimum, V maximum, boolean centered, V currentValue) {
59                 this.minimum = minimum;
60                 this.maximum = maximum;
61                 this.centered = centered;
62                 value = currentValue;
63         }
64
65         //
66         // CONTROLLER METHODS
67         //
68
69         @Override
70         public V minimum() {
71                 return minimum;
72         }
73
74         @Override
75         public V maximum() {
76                 return maximum;
77         }
78
79         @Override
80         public boolean centered() {
81                 return centered;
82         }
83
84         @Override
85         public V value() {
86                 return value;
87         }
88
89         @Override
90         public void value(V value) {
91                 V newValue = (value.compareTo(minimum) < 0) ? minimum : ((value.compareTo(maximum) > 0) ? maximum : value);
92                 if (newValue.compareTo(this.value) != 0) {
93                         this.value = newValue;
94                         valueSet(newValue);
95                 }
96         }
97
98         //
99         // SUBCLASS METHODS
100         //
101
102         /**
103          * Adjusts the controller. This method is called from {@link
104          * #value(Comparable)} if the new value is different from the current value.
105          * Also, the value is clamped to fit within the range of this controller.
106          *
107          * @param value
108          *              The new value
109          */
110         protected abstract void valueSet(V value);
111
112 }