Path: blob/master/src/java.base/share/classes/javax/security/auth/callback/ChoiceCallback.java
41161 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.security.auth.callback;2627/**28* <p> Underlying security services instantiate and pass a29* {@code ChoiceCallback} to the {@code handle}30* method of a {@code CallbackHandler} to display a list of choices31* and to retrieve the selected choice(s).32*33* @since 1.434* @see javax.security.auth.callback.CallbackHandler35*/36public class ChoiceCallback implements Callback, java.io.Serializable {3738@java.io.Serial39private static final long serialVersionUID = -3975664071579892167L;4041/**42* @serial43* @since 1.444*/45private final String prompt;46/**47* @serial the list of choices48* @since 1.449*/50private final String[] choices;51/**52* @serial the choice to be used as the default choice53* @since 1.454*/55private final int defaultChoice;56/**57* @serial whether multiple selections are allowed from the list of58* choices59* @since 1.460*/61private final boolean multipleSelectionsAllowed;62/**63* @serial the selected choices, represented as indexes into the64* {@code choices} list.65* @since 1.466*/67private int[] selections;6869/**70* Construct a {@code ChoiceCallback} with a prompt,71* a list of choices, a default choice, and a boolean specifying72* whether or not multiple selections from the list of choices are allowed.73*74*75* @param prompt the prompt used to describe the list of choices.76*77* @param choices the list of choices. The array is cloned to protect78* against subsequent modification.79*80* @param defaultChoice the choice to be used as the default choice81* when the list of choices are displayed. This value82* is represented as an index into the83* {@code choices} array.84*85* @param multipleSelectionsAllowed boolean specifying whether or86* not multiple selections can be made from the87* list of choices.88*89* @exception IllegalArgumentException if {@code prompt} is null,90* if {@code prompt} has a length of 0,91* if {@code choices} is null,92* if {@code choices} has a length of 0,93* if any element from {@code choices} is null,94* if any element from {@code choices}95* has a length of 0 or if {@code defaultChoice}96* does not fall within the array boundaries of97* {@code choices}.98*/99public ChoiceCallback(String prompt, String[] choices,100int defaultChoice, boolean multipleSelectionsAllowed) {101102if (prompt == null || prompt.isEmpty() ||103choices == null || choices.length == 0 ||104defaultChoice < 0 || defaultChoice >= choices.length)105throw new IllegalArgumentException();106107for (int i = 0; i < choices.length; i++) {108if (choices[i] == null || choices[i].isEmpty())109throw new IllegalArgumentException();110}111112this.prompt = prompt;113this.choices = choices.clone();114this.defaultChoice = defaultChoice;115this.multipleSelectionsAllowed = multipleSelectionsAllowed;116}117118/**119* Get the prompt.120*121* @return the prompt.122*/123public String getPrompt() {124return prompt;125}126127/**128* Get the list of choices.129*130* @return a copy of the list of choices.131*/132public String[] getChoices() {133return choices.clone();134}135136/**137* Get the defaultChoice.138*139* @return the defaultChoice, represented as an index into140* the {@code choices} list.141*/142public int getDefaultChoice() {143return defaultChoice;144}145146/**147* Get the boolean determining whether multiple selections from148* the {@code choices} list are allowed.149*150* @return whether multiple selections are allowed.151*/152public boolean allowMultipleSelections() {153return multipleSelectionsAllowed;154}155156/**157* Set the selected choice.158*159* @param selection the selection represented as an index into the160* {@code choices} list.161*162* @see #getSelectedIndexes163*/164public void setSelectedIndex(int selection) {165this.selections = new int[1];166this.selections[0] = selection;167}168169/**170* Set the selected choices.171*172* @param selections the selections represented as indexes into the173* {@code choices} list. The array is cloned to protect174* against subsequent modification.175*176* @exception UnsupportedOperationException if multiple selections are177* not allowed, as determined by178* {@code allowMultipleSelections}.179*180* @see #getSelectedIndexes181*/182public void setSelectedIndexes(int[] selections) {183if (!multipleSelectionsAllowed)184throw new UnsupportedOperationException();185this.selections = selections == null ? null : selections.clone();186}187188/**189* Get the selected choices.190*191* @return a copy of the selected choices, represented as indexes into the192* {@code choices} list.193*194* @see #setSelectedIndexes195*/196public int[] getSelectedIndexes() {197return selections == null ? null : selections.clone();198}199}200201202