Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/security/auth/callback/ChoiceCallback.java
41161 views
1
/*
2
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.security.auth.callback;
27
28
/**
29
* <p> Underlying security services instantiate and pass a
30
* {@code ChoiceCallback} to the {@code handle}
31
* method of a {@code CallbackHandler} to display a list of choices
32
* and to retrieve the selected choice(s).
33
*
34
* @since 1.4
35
* @see javax.security.auth.callback.CallbackHandler
36
*/
37
public class ChoiceCallback implements Callback, java.io.Serializable {
38
39
@java.io.Serial
40
private static final long serialVersionUID = -3975664071579892167L;
41
42
/**
43
* @serial
44
* @since 1.4
45
*/
46
private final String prompt;
47
/**
48
* @serial the list of choices
49
* @since 1.4
50
*/
51
private final String[] choices;
52
/**
53
* @serial the choice to be used as the default choice
54
* @since 1.4
55
*/
56
private final int defaultChoice;
57
/**
58
* @serial whether multiple selections are allowed from the list of
59
* choices
60
* @since 1.4
61
*/
62
private final boolean multipleSelectionsAllowed;
63
/**
64
* @serial the selected choices, represented as indexes into the
65
* {@code choices} list.
66
* @since 1.4
67
*/
68
private int[] selections;
69
70
/**
71
* Construct a {@code ChoiceCallback} with a prompt,
72
* a list of choices, a default choice, and a boolean specifying
73
* whether or not multiple selections from the list of choices are allowed.
74
*
75
*
76
* @param prompt the prompt used to describe the list of choices.
77
*
78
* @param choices the list of choices. The array is cloned to protect
79
* against subsequent modification.
80
*
81
* @param defaultChoice the choice to be used as the default choice
82
* when the list of choices are displayed. This value
83
* is represented as an index into the
84
* {@code choices} array.
85
*
86
* @param multipleSelectionsAllowed boolean specifying whether or
87
* not multiple selections can be made from the
88
* list of choices.
89
*
90
* @exception IllegalArgumentException if {@code prompt} is null,
91
* if {@code prompt} has a length of 0,
92
* if {@code choices} is null,
93
* if {@code choices} has a length of 0,
94
* if any element from {@code choices} is null,
95
* if any element from {@code choices}
96
* has a length of 0 or if {@code defaultChoice}
97
* does not fall within the array boundaries of
98
* {@code choices}.
99
*/
100
public ChoiceCallback(String prompt, String[] choices,
101
int defaultChoice, boolean multipleSelectionsAllowed) {
102
103
if (prompt == null || prompt.isEmpty() ||
104
choices == null || choices.length == 0 ||
105
defaultChoice < 0 || defaultChoice >= choices.length)
106
throw new IllegalArgumentException();
107
108
for (int i = 0; i < choices.length; i++) {
109
if (choices[i] == null || choices[i].isEmpty())
110
throw new IllegalArgumentException();
111
}
112
113
this.prompt = prompt;
114
this.choices = choices.clone();
115
this.defaultChoice = defaultChoice;
116
this.multipleSelectionsAllowed = multipleSelectionsAllowed;
117
}
118
119
/**
120
* Get the prompt.
121
*
122
* @return the prompt.
123
*/
124
public String getPrompt() {
125
return prompt;
126
}
127
128
/**
129
* Get the list of choices.
130
*
131
* @return a copy of the list of choices.
132
*/
133
public String[] getChoices() {
134
return choices.clone();
135
}
136
137
/**
138
* Get the defaultChoice.
139
*
140
* @return the defaultChoice, represented as an index into
141
* the {@code choices} list.
142
*/
143
public int getDefaultChoice() {
144
return defaultChoice;
145
}
146
147
/**
148
* Get the boolean determining whether multiple selections from
149
* the {@code choices} list are allowed.
150
*
151
* @return whether multiple selections are allowed.
152
*/
153
public boolean allowMultipleSelections() {
154
return multipleSelectionsAllowed;
155
}
156
157
/**
158
* Set the selected choice.
159
*
160
* @param selection the selection represented as an index into the
161
* {@code choices} list.
162
*
163
* @see #getSelectedIndexes
164
*/
165
public void setSelectedIndex(int selection) {
166
this.selections = new int[1];
167
this.selections[0] = selection;
168
}
169
170
/**
171
* Set the selected choices.
172
*
173
* @param selections the selections represented as indexes into the
174
* {@code choices} list. The array is cloned to protect
175
* against subsequent modification.
176
*
177
* @exception UnsupportedOperationException if multiple selections are
178
* not allowed, as determined by
179
* {@code allowMultipleSelections}.
180
*
181
* @see #getSelectedIndexes
182
*/
183
public void setSelectedIndexes(int[] selections) {
184
if (!multipleSelectionsAllowed)
185
throw new UnsupportedOperationException();
186
this.selections = selections == null ? null : selections.clone();
187
}
188
189
/**
190
* Get the selected choices.
191
*
192
* @return a copy of the selected choices, represented as indexes into the
193
* {@code choices} list.
194
*
195
* @see #setSelectedIndexes
196
*/
197
public int[] getSelectedIndexes() {
198
return selections == null ? null : selections.clone();
199
}
200
}
201
202