This is the second time to create REPL for Java 8. Please refer to the first article for the purpose and precautions.
Now let's get into the source description.
An interface that provides access to the Windows cmd and the Eclipse console.
Interface.Finalizable Interface to the methods of the interface. interface Interface { ... }It is not a mistake, but a dummy interface.
If you create a small interface as a single file, the file system becomes complicated, so we will add the interface in this.
#### **`Interface.java`**
```java
package console;
interface Interface {
/** console interface. */
interface Console extends Interface.Finalizable {
// methods for input string from console ======================================
default String input(String prompt);
// methods for output string to console =======================================
void print(String format, Object... args);
void print(String s);
// extends Inteeface.Finalizeable =============================================
void cleanup();
}
// ... more interface ...
}
This file is also packed ...
The concrete console class is simply created and passed by `Console.create ()`
.
Service.java
package console;
public interface Service {
class Console {
public static final String newLine = System.lineSeparator();
private static final String defaultEncoding = "UTF-8";
public static Interface.Console create(String encoding) {
if ( encoding == null || encoding.trim().isEmpty() ) {
encoding = defaultEncoding;
}
if ( !java.nio.charset.Charset.isSupported(encoding) ) {
encoding = defaultEncoding;
}
return (System.console() != null)
? new SystemConsole(System.console())
: new StandardConsole(encoding);
}
// ///////////////////////////////////////////////////////////////////////////////
/** console uses standard in/out class.
*/ // ///////////////////////////////////////////////////////////////////////////
private static class StandardConsole implements Interface.Console {
protected StandardConsole(String encoding) {
try {
java.io.InputStreamReader istream = new java.io.InputStreamReader(System.in, encoding);
reader_ = new java.io.BufferedReader(istream);
out_ = new java.io.PrintStream(System.out, true, encoding);
} catch (java.io.IOException e) {
System.out.println( e.getMessage() );
}
}
// methods for input string from console ===============================
public String input(String prompt) {
out_.printf(prompt);
String s = "";
try {
s = reader_.readLine();
} catch (java.io.IOException e) {
System.out.println( e.getMEssage() );
}
return s;
}
// methods for output string to console ================================
public void print(String format, Object... args) { out_.printf(format, args); }
public void print(String s) { out_.println(s); }
// implements Finalizable.cleanup() ====================================
public void cleanup() {
out_.close();
out_ = null;
try {
reader_.close();
reader_ = null;
} catch(java.io.IOException e) {
System.out.println( e.getMessage() );
}
}
// internal fields =====================================================
java.io.BufferedReader reader_;
java.io.PrintStream out_;
}
// ///////////////////////////////////////////////////////////////////////////////
/** console uses System.console() class.
*/ // ///////////////////////////////////////////////////////////////////////////
private static class SystemConsole implements Interface.Console {
protected SystemConsole(java.io.Console con) {
console_ = con;
}
// methods for input string from console ===============================
public String input(String prompt) { return console_.readLine(prompt); }
// methods for output string to console ================================
public void print(String format, Object... args) {
console_.printf(format, args);
}
public void print(String s) {
console_.printf(s + Service.Console.newLine);
}
// implements Finalizable.cleanup() ====================================
public void cleanup() { /* no operation */ }
// internal fields =====================================================
java.io.Console console_;
}
}
}
Service.Console Although it says service, it's actually a factory class in Console. It takes the console encoding name and creates and returns a concrete console class based on it. If an incorrect encoding name is passed, the default "UTF-8" will be set.
python
public interface Service {
class Console {
public static final String newLine = System.lineSeparator();
private static final String defaultEncoding = "UTF-8";
public static Interface.Console create(String encoding) {
if ( encoding == null || encoding.trim().isEmpty() ) {
encoding = defaultEncoding;
}
if ( !java.nio.charset.Charset.isSupported(encoding) ) {
encoding = defaultEncoding;
}
return (System.console() != null)
? new SystemConsole(System.console())
: new StandardConsole(encoding);
}
// ... inner class ...
}
SystemConsole This is a console class that uses System.Console. I don't think any explanation is necessary because the processing is simply delegated. In the above Service.Console, I am trying hard to check the encoding, but here I will not use it because the encoding actually used in the console (cmd.exe etc.) is initialized ( Apparently, there seems to be no way to change it).
python
public interface Service {
class Console {
// ...abridgement...
// ///////////////////////////////////////////////////////////////////////////////
/** console uses System.console() class.
*/ // ///////////////////////////////////////////////////////////////////////////
private static class SystemConsole implements Interface.Console {
protected SystemConsole(java.io.Console con) { console_ = con; }
// methods for input string from console ===============================
public String input(String prompt) { return console_.readLine(prompt); }
// methods for output string to console ================================
public void print(String format, Object... args) {
console_.printf(format, args);
}
public void print(String s) {
console_.printf(s + Service.Console.newLine);
}
// implements Finalizable.cleanup() ====================================
public void cleanup() { /* no operation */ }
// internal fields =====================================================
java.io.Console console_;
}
}
}
StandardinConsole This is a console class that uses standard I / O. It's just doing the same thing as the System Console, but thanks to the boilerplate it doubles the amount of code. Even so, the amount of description is reduced because the encoding check is performed at the factory.
input (String) `` `prompt and return the string received from
BufferdReader.readLine () `` `, but the code that should only take two lines is awkward Especially ... ~~ This kind of place is annoying and disgusting ~~python
public interface Service {
class Console {
// ...abridgement...
// ///////////////////////////////////////////////////////////////////////////////
/** console uses standard in/out class.
*/ // ///////////////////////////////////////////////////////////////////////////
private static class StandardConsole implements Interface.Console {
protected StandardConsole(String encoding) {
try {
java.io.InputStreamReader istream = new java.io.InputStreamReader(System.in, encoding);
reader_ = new java.io.BufferedReader(istream);
out_ = new java.io.PrintStream(System.out, true, encoding);
} catch (java.io.IOException e) {
System.out.println( e.getMessage() );
}
}
// methods for input string from console ===============================
public String input(String prompt) {
out_.printf(prompt);
String s = "";
try {
s = reader_.readLine();
} catch (java.io.IOException e) {
System.out.println( e.getMessage() );
}
return s;
}
// methods for output string to console ================================
public void print(String format, Object... args) { out_.printf(format, args); }
public void print(String s) { out_.println(s); }
// implements Finalizable.cleanup() ====================================
public void cleanup() {
out_.close();
out_ = null;
try {
reader_.close();
reader_ = null;
} catch(java.io.IOException e) {
System.out.println( e.getMessage() );
}
}
// internal fields =====================================================
java.io.BufferedReader reader_;
java.io.PrintStream out_;
}
}
The previous BaseREPL is also modified because you need to pass the encoding name. Here, only the corrected parts are described. See Previous article for the full code.
`BaseREPL ()`
constructor now receives the encoding name to pass to the console. BaseREPL.java
class BaseREPL {
BaseREPL(String encoding) {
if ( encoding == null || encoding.trim().isEmpty() ) { encoding = "UTF-8"; }
console_ = Service.Console.create(encoding);
}
// ...Omitted on the way...
// internal fields =============================================================
protected final Interface.Console console_;
}
This time, I explained the console used by REPL. Initially it was compact, but while I was fixing it, it became a decent volume.
A few more times, we'll continue to create classes to include in the REPL.
Recommended Posts