Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627/**28* An {@code EnumControl} provides control over a set of discrete possible29* values, each represented by an object. In a graphical user interface, such a30* control might be represented by a set of buttons, each of which chooses one31* value or setting. For example, a reverb control might provide several preset32* reverberation settings, instead of providing continuously adjustable33* parameters of the sort that would be represented by {@link FloatControl}34* objects.35* <p>36* Controls that provide a choice between only two settings can often be37* implemented instead as a {@link BooleanControl}, and controls that provide a38* set of values along some quantifiable dimension might be implemented instead39* as a {@code FloatControl} with a coarse resolution. However, a key feature of40* {@code EnumControl} is that the returned values are arbitrary objects, rather41* than numerical or boolean values. This means that each returned object can42* provide further information. As an example, the settings of a43* {@link EnumControl.Type#REVERB REVERB} control are instances of44* {@link ReverbType} that can be queried for the parameter values used for each45* setting.46*47* @author Kara Kytle48* @since 1.349*/50public abstract class EnumControl extends Control {5152/**53* The set of possible values.54*/55private final Object[] values;5657/**58* The current value.59*/60private Object value;6162/**63* Constructs a new enumerated control object with the given parameters.64*65* @param type the type of control represented this enumerated control66* object67* @param values the set of possible values for the control68* @param value the initial control value69*/70protected EnumControl(Type type, Object[] values, Object value) {71super(type);72this.values = values;73this.value = value;74}7576/**77* Sets the current value for the control. The default implementation simply78* sets the value as indicated. If the value indicated is not supported, an79* {@code IllegalArgumentException} is thrown. Some controls require that80* their line be open before they can be affected by setting a value.81*82* @param value the desired new value83* @throws IllegalArgumentException if the value indicated does not fall84* within the allowable range85*/86public void setValue(Object value) {87if (!isValueSupported(value)) {88throw new IllegalArgumentException("Requested value " + value + " is not supported.");89}9091this.value = value;92}9394/**95* Obtains this control's current value.96*97* @return the current value98*/99public Object getValue() {100return value;101}102103/**104* Returns the set of possible values for this control.105*106* @return the set of possible values107*/108public Object[] getValues() {109return values.clone();110}111112/**113* Indicates whether the value specified is supported.114*115* @param value the value for which support is queried116* @return {@code true} if the value is supported, otherwise {@code false}117*/118private boolean isValueSupported(Object value) {119120for (int i = 0; i < values.length; i++) {121//$$fb 2001-07-20: Fix for bug 4400392: setValue() in ReverbControl always throws Exception122//if (values.equals(values[i])) {123if (value.equals(values[i])) {124return true;125}126}127128return false;129}130131/**132* Returns a string representation of the enumerated control.133*134* @return a string representation of the enumerated control135*/136@Override137public String toString() {138return String.format("%s with current value: %s", super.toString(),139getValue());140}141142/**143* An instance of the {@code EnumControl.Type} inner class identifies one144* kind of enumerated control. Static instances are provided for the common145* types.146*147* @author Kara Kytle148* @see EnumControl149* @since 1.3150*/151public static class Type extends Control.Type {152153/**154* Represents a control over a set of possible reverberation settings.155* Each reverberation setting is described by an instance of the156* {@link ReverbType} class. (To access these settings, invoke157* {@link EnumControl#getValues} on an enumerated control of type158* {@code REVERB}.)159*/160public static final Type REVERB = new Type("Reverb");161162/**163* Constructs a new enumerated control type.164*165* @param name the name of the new enumerated control type166*/167protected Type(final String name) {168super(name);169}170}171}172173174