| 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 |
import java.io.BufferedReader; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStreamReader; |
| 22 |
import java.io.PrintWriter; |
| 23 |
|
| 24 |
/** |
| 25 |
* A generic question. |
| 26 |
* |
| 27 |
* @author Emmanuel Bourg |
| 28 |
*/ |
| 29 |
public abstract class Question<T> { |
| 30 |
|
| 31 |
protected String question; |
| 32 |
private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
| 33 |
private PrintWriter out = new PrintWriter(System.out, true); |
| 34 |
|
| 35 |
protected Question(String question) { |
| 36 |
this.question = question; |
| 37 |
} |
| 38 |
|
| 39 |
void setInput(BufferedReader in) { |
| 40 |
this.in = in; |
| 41 |
} |
| 42 |
|
| 43 |
void setOutput(PrintWriter out) { |
| 44 |
this.out = out; |
| 45 |
} |
| 46 |
|
| 47 |
protected String readLine() { |
| 48 |
try { |
| 49 |
String line = in.readLine(); |
| 50 |
return line != null ? line.trim() : ""; |
| 51 |
} catch (IOException e) { |
| 52 |
e.printStackTrace(); |
| 53 |
return ""; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
protected void println(String text) { |
| 58 |
out.println(text); |
| 59 |
} |
| 60 |
|
| 61 |
protected void print(String text) { |
| 62 |
out.print(text); |
| 63 |
out.flush(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Asks the question and returns the response. |
| 68 |
*/ |
| 69 |
public abstract T ask(); |
| 70 |
} |