Debian Codegraph @

maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/YesNoQuestion.java

raw
1 /*
2 * Copyright 2013 Emmanuel Bourg
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.debian.maven.packager.interaction;
18
19 /**
20 * A yes/no question. The question is asked again if the answer is not 'y', 'n',
21 * 'yes' or 'no'. The answer is case insensitive. A blank response returns
22 * the default choice.
23 *
24 * @author Emmanuel Bourg
25 */
26 public class YesNoQuestion extends Question<Boolean> {
27
28 private boolean defaultChoice;
29
30 public YesNoQuestion(String question, boolean defaultChoice) {
31 super(question);
32 this.defaultChoice = defaultChoice;
33 }
34
35 @Override
36 public Boolean ask() {
37 Boolean choice = null;
38
39 // keep asking the question until a valid choice is entered
40 while (choice == null) {
41 println(question);
42 print("[");
43 print(defaultChoice ? "Y" : "y");
44 print("/");
45 print(defaultChoice ? "n" : "N");
46 print("]");
47 print(" > ");
48 String response = readLine();
49 if ("".equals(response.trim())) {
50 choice = defaultChoice;
51 } else if (response.equalsIgnoreCase("y") || response.equalsIgnoreCase("yes")) {
52 choice = true;
53 } else if (response.equalsIgnoreCase("n") || response.equalsIgnoreCase("no")) {
54 choice = false;
55 }
56 }
57
58 return choice;
59 }
60 }