001package components.simplereader;
002
003/**
004 * Layered implementations of secondary methods for {@code SimpleReader}.
005 */
006public abstract class SimpleReaderSecondary implements SimpleReader {
007
008    /*
009     * Public members ---------------------------------------------------------
010     */
011
012    /*
013     * Common methods (from Object) -------------------------------------------
014     */
015
016    /*
017     * The Object versions of equals and hashCode are not overridden because two
018     * SimpleReader objects are (known to be) equal iff they are the same
019     * object; there is no way to know what the "content" components of the
020     * mathematical models are. On the other hand, toString is overridden
021     * because the other components of the mathematical model are known, though
022     * "content" is not.
023     */
024
025    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
026    @Override
027    public String toString() {
028        StringBuilder result = new StringBuilder("(");
029        result.append(this.isOpen());
030        result.append(",\"");
031        result.append(this.name());
032        result.append("\",[contents])");
033        return result.toString();
034    }
035
036    /*
037     * Other non-kernel methods -----------------------------------------------
038     */
039
040    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
041    @Override
042    public String nextLine() {
043        assert this.isOpen() : "Violation of: this is open";
044        assert !this.atEOS() : "Violation of: this is not at end-of-stream";
045        StringBuilder str = new StringBuilder();
046        while (!this.atEOS()) {
047            char c = this.read();
048            if ((c == '\r') || (c == '\n')) {
049                if (c == '\r') {
050                    // check if next character is line feed
051                    if (!this.atEOS() && (this.peek() == '\n')) {
052                        /* c = */this.read();
053                    }
054                }
055                break;
056            } else {
057                str.append(c);
058            }
059        }
060        return str.toString();
061    }
062
063    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
064    @Override
065    public int nextInteger() {
066        assert this.isOpen() : "Violation of: this is open";
067        assert !this.atEOS() : "Violation of: this is not at end-of-stream";
068        String str = this.nextLine();
069        try {
070            int result = Integer.parseInt(str);
071            return result;
072        } catch (NumberFormatException e) {
073            throw new AssertionError("Violation of: input is in int format");
074        }
075    }
076
077    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
078    @Override
079    public long nextLong() {
080        assert this.isOpen() : "Violation of: this is open";
081        assert !this.atEOS() : "Violation of: this is not at end-of-stream";
082        String str = this.nextLine();
083        try {
084            long result = Long.parseLong(str);
085            return result;
086        } catch (NumberFormatException e) {
087            throw new AssertionError("Violation of: input is in long format");
088        }
089    }
090
091    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
092    @Override
093    public boolean nextBoolean() {
094        assert this.isOpen() : "Violation of: this is open";
095        assert !this.atEOS() : "Violation of: this is not at end-of-stream";
096        String str = this.nextLine();
097        assert str.equals("true") || str.equals("false")
098                : "Violation of: input is in bool format";
099        boolean result = Boolean.valueOf(str);
100        return result;
101    }
102
103    // CHECKSTYLE: ALLOW THIS METHOD TO BE OVERRIDDEN
104    @Override
105    public double nextDouble() {
106        assert this.isOpen() : "Violation of: this is open";
107        assert !this.atEOS() : "Violation of: this is not at end-of-stream";
108        String str = this.nextLine();
109        try {
110            double result = Double.parseDouble(str);
111            return result;
112        } catch (NumberFormatException e) {
113            throw new AssertionError("Violation of: input is in double format");
114        }
115    }
116
117}