[JAVA] Road to REPL (?) Creation (2)

Introduction

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.

Console interface

An interface that provides access to the Windows cmd and the Eclipse console.

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 ...
}

Console service

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_;
        }
    }
}

Description

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.

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_;
    }
}

BaseREPL fix

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.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_;
}

Summary

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

Road to REPL (?) Creation (3)
Road to REPL (?) Creation (1)
Road to REPL (?) Creation (2)
The road to Web service creation (Part 2)
19 Corresponds to object creation
Road to Java SE 11 Silver acquisition
The road from JavaScript to Java
[Docker] Operation up to container creation # 2
to_ ○
The road to creating a music game 2
Java SE8 Silver ~ The Road to Pass ~
Introduction to kotlin for iOS developers ⑥-Kotlin creation
The road to creating a music game 3
The road to creating a music game 1