Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/callback/ChoiceCallback.java
38918 views
/*1* Copyright (c) 1999, 2013, 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* @see javax.security.auth.callback.CallbackHandler34*/35public class ChoiceCallback implements Callback, java.io.Serializable {3637private static final long serialVersionUID = -3975664071579892167L;3839/**40* @serial41* @since 1.442*/43private String prompt;44/**45* @serial the list of choices46* @since 1.447*/48private String[] choices;49/**50* @serial the choice to be used as the default choice51* @since 1.452*/53private int defaultChoice;54/**55* @serial whether multiple selections are allowed from the list of56* choices57* @since 1.458*/59private boolean multipleSelectionsAllowed;60/**61* @serial the selected choices, represented as indexes into the62* {@code choices} list.63* @since 1.464*/65private int[] selections;6667/**68* Construct a {@code ChoiceCallback} with a prompt,69* a list of choices, a default choice, and a boolean specifying70* whether or not multiple selections from the list of choices are allowed.71*72* <p>73*74* @param prompt the prompt used to describe the list of choices. <p>75*76* @param choices the list of choices. <p>77*78* @param defaultChoice the choice to be used as the default choice79* when the list of choices are displayed. This value80* is represented as an index into the81* {@code choices} array. <p>82*83* @param multipleSelectionsAllowed boolean specifying whether or84* not multiple selections can be made from the85* list of choices.86*87* @exception IllegalArgumentException if {@code prompt} is null,88* if {@code prompt} has a length of 0,89* if {@code choices} is null,90* if {@code choices} has a length of 0,91* if any element from {@code choices} is null,92* if any element from {@code choices}93* has a length of 0 or if {@code defaultChoice}94* does not fall within the array boundaries of95* {@code choices}.96*/97public ChoiceCallback(String prompt, String[] choices,98int defaultChoice, boolean multipleSelectionsAllowed) {99100if (prompt == null || prompt.length() == 0 ||101choices == null || choices.length == 0 ||102defaultChoice < 0 || defaultChoice >= choices.length)103throw new IllegalArgumentException();104105for (int i = 0; i < choices.length; i++) {106if (choices[i] == null || choices[i].length() == 0)107throw new IllegalArgumentException();108}109110this.prompt = prompt;111this.choices = choices;112this.defaultChoice = defaultChoice;113this.multipleSelectionsAllowed = multipleSelectionsAllowed;114}115116/**117* Get the prompt.118*119* <p>120*121* @return the prompt.122*/123public String getPrompt() {124return prompt;125}126127/**128* Get the list of choices.129*130* <p>131*132* @return the list of choices.133*/134public String[] getChoices() {135return choices;136}137138/**139* Get the defaultChoice.140*141* <p>142*143* @return the defaultChoice, represented as an index into144* the {@code choices} list.145*/146public int getDefaultChoice() {147return defaultChoice;148}149150/**151* Get the boolean determining whether multiple selections from152* the {@code choices} list are allowed.153*154* <p>155*156* @return whether multiple selections are allowed.157*/158public boolean allowMultipleSelections() {159return multipleSelectionsAllowed;160}161162/**163* Set the selected choice.164*165* <p>166*167* @param selection the selection represented as an index into the168* {@code choices} list.169*170* @see #getSelectedIndexes171*/172public void setSelectedIndex(int selection) {173this.selections = new int[1];174this.selections[0] = selection;175}176177/**178* Set the selected choices.179*180* <p>181*182* @param selections the selections represented as indexes into the183* {@code choices} list.184*185* @exception UnsupportedOperationException if multiple selections are186* not allowed, as determined by187* {@code allowMultipleSelections}.188*189* @see #getSelectedIndexes190*/191public void setSelectedIndexes(int[] selections) {192if (!multipleSelectionsAllowed)193throw new UnsupportedOperationException();194this.selections = selections;195}196197/**198* Get the selected choices.199*200* <p>201*202* @return the selected choices, represented as indexes into the203* {@code choices} list.204*205* @see #setSelectedIndexes206*/207public int[] getSelectedIndexes() {208return selections;209}210}211212213