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 name of this controller. */
34 private final String name;
36 /** The minimum value of this controller. */
37 private final V minimum;
39 /** The maximum value of this controller. */
40 private final V maximum;
42 /** Whether this controller has a “center” position. */
43 private final boolean centered;
45 /** The current value of this controller. */
49 * Creates a new abstract controller.
52 * The name of the controller
54 * The minimum value of the controller
56 * The maximum value of the controller
58 * {@code true} if this controller has a “center” position, {@code false}
62 public AbstractController(String name, V minimum, V maximum, boolean centered, V currentValue) {
64 this.minimum = minimum;
65 this.maximum = maximum;
66 this.centered = centered;
75 public String name() {
90 public boolean centered() {
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;
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.
117 * This implementation does nothing.
122 protected void valueSet(V value) {