2 * Sonitus - AbstractController.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data.controller;
20 import net.pterodactylus.sonitus.data.Controller;
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.
27 * This implementation is not thread-safe.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public abstract class AbstractController<V extends Comparable<V>> implements Controller<V> {
33 /** The minimum value of this controller. */
34 private final V minimum;
36 /** The maximum value of this controller. */
37 private final V maximum;
39 /** Whether this controller has a “center” position. */
40 private final boolean centered;
42 /** The current value of this controller. */
46 * Creates a new abstract controller.
49 * The minimum value of the controller
51 * The maximum value of the controller
53 * {@code true} if this controller has a “center” position, {@code false}
56 * The current value of this controller
58 public AbstractController(V minimum, V maximum, boolean centered, V currentValue) {
59 this.minimum = minimum;
60 this.maximum = maximum;
61 this.centered = centered;
80 public boolean centered() {
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;
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.
110 protected abstract void valueSet(V value);