Skip to content

Commit

Permalink
Chaining style for AWriter/IndentedWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Nov 2, 2023
1 parent f42f4dc commit 08c59df
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 56 deletions.
26 changes: 13 additions & 13 deletions jena-base/src/main/java/org/apache/jena/atlas/io/AWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@

/** Simple abstraction of a string/character output stream */

public interface AWriter extends Closeable, AutoCloseable
public interface AWriter extends Closeable, AutoCloseable
{
public void write(char ch) ;
public void write(char[] cbuf) ;
public void write(String string) ;

public void print(char ch) ;
public void print(char[] cbuf) ;
public void print(String string) ;
public void printf(String fmt, Object ...arg) ;
public void println(String object) ;
public void println() ;
public void flush() ;
public AWriter write(char ch) ;
public AWriter write(char[] cbuf) ;
public AWriter write(String string) ;

public AWriter print(char ch) ;
public AWriter print(char[] cbuf) ;
public AWriter print(String string) ;
public AWriter printf(String fmt, Object ...arg) ;
public AWriter println(String object) ;
public AWriter println() ;

public AWriter flush() ;
@Override
public void close() ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
public abstract class AWriterBase implements AWriter
{
@Override
public final void write(char ch) { print(ch) ; }
public final AWriter write(char ch) { return print(ch) ; }
@Override
public final void write(char[] cbuf) { print(cbuf) ; }
public final AWriter write(char[] cbuf) { return print(cbuf) ; }
@Override
public final void write(String string) { print(string) ; }
public final AWriter write(String string) { return print(string) ; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@

public class IndentedLineBuffer extends IndentedWriter
{
StringWriter sw ;
protected final StringWriter sw ;
public IndentedLineBuffer() { this(false) ; }

public IndentedLineBuffer(boolean withLineNumbers)
{
public IndentedLineBuffer(boolean withLineNumbers) {
super(new StringWriter(), withLineNumbers) ;
sw = (StringWriter)super.out ;
}
Expand All @@ -43,8 +42,8 @@ public IndentedLineBuffer(boolean withLineNumbers)
public String toString() { return asString() ; }

// Names more usually used for a buffer.
public void append(String fmt, Object... args) { printf(fmt, args) ; }
public void append(char ch) { print(ch) ;}
public IndentedLineBuffer append(String fmt, Object... args) { printf(fmt, args) ; return this; }
public IndentedLineBuffer append(char ch) { print(ch) ;return this; }

public void clear() { sw.getBuffer().setLength(0) ; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,53 +118,58 @@ protected IndentedWriter(Writer writer, boolean withLineNumbers) {
protected IndentedWriter rtnObject() { return this; }

@Override
public void print(String str) {
public IndentedWriter print(String str) {
if ( str == null )
str = "null";
if ( false ) {
// Don't check for embedded newlines.
write$(str);
return;
return this;
}
for ( int i = 0; i < str.length(); i++ )
printOneChar(str.charAt(i));
return this;
}

@Override
public void printf(String formatStr, Object... args) {
public IndentedWriter printf(String formatStr, Object... args) {
print(format(formatStr, args));
return this;
}

@Override
public void print(char ch) { printOneChar(ch); }
public void print(Object obj) { print(String.valueOf(obj)); }
public IndentedWriter print(char ch) { printOneChar(ch); return this; }
public IndentedWriter print(Object obj) { print(String.valueOf(obj)); return this; }

@Override
public void println(String str) { print(str); newline(); }
public void println(char ch) { print(ch); newline(); }
public void println(Object obj) { print(String.valueOf(obj)); newline(); }
public IndentedWriter println(String str) { print(str); newline(); return this; }
public IndentedWriter println(char ch) { print(ch); newline(); return this; }
public IndentedWriter println(Object obj) { print(String.valueOf(obj)); newline(); return this; }

@Override
public void println() { newline(); }
public IndentedWriter println() { newline(); return this; }

@Override
public void print(char[] cbuf) {
public IndentedWriter print(char[] cbuf) {
for ( char aCbuf : cbuf ) {
printOneChar(aCbuf);
}
return this;
}

/** Print a string N times */
public void print(String s, int n) {
public IndentedWriter print(String s, int n) {
for ( int i = 0; i < n; i++ )
print(s);
return this;
}

/** Print a char N times */
public void print(char ch, int n) {
public IndentedWriter print(char ch, int n) {
lineStart();
for ( int i = 0; i < n; i++ )
printOneChar(ch);
return this;
}

private char lastChar = '\0';
Expand Down Expand Up @@ -196,7 +201,7 @@ private void printOneChar(char ch) {
private void write$(String s)
{ try { out.write(s); } catch (IOException ex) { IO.exception(ex); } }

public void newline() {
public IndentedWriter newline() {
lineStart();

if ( endOfLineMarker != null )
Expand All @@ -210,13 +215,15 @@ public void newline() {
// so if layered over a PrintWriter, need to flush that as well.
if ( flushOnNewline )
flush();
return this;
}

private boolean atStartOfLine() { return column <= currentIndent; }

public void ensureStartOfLine() {
public IndentedWriter ensureStartOfLine() {
if ( !atStartOfLine() )
newline();
return this;
}

public boolean atLineStart() { return startingNewLine; }
Expand Down Expand Up @@ -246,27 +253,28 @@ private void lineStart() {
public void close() { IO.close(out); }

@Override
public void flush() { IO.flush(out); }
public IndentedWriter flush() { IO.flush(out); return this; }

/** Pad to the indent (if we are before it) */
public void pad() {
public IndentedWriter pad() {
if ( startingNewLine && currentIndent > 0 )
lineStart();
padInternal();
return this;
}

/** Pad to a given number of columns EXCLUDING the indent.
*
* @param col Column number (first column is 1).
*/
public void pad(int col) { pad(col, false); }
public IndentedWriter pad(int col) { return pad(col, false); }

/** Pad to a given number of columns maybe including the indent.
*
* @param col Column number (first column is 1).
* @param absoluteColumn Whether to include the indent
*/
public void pad(int col, boolean absoluteColumn) {
public IndentedWriter pad(int col, boolean absoluteColumn) {
// Make absolute
if ( !absoluteColumn )
col = col + currentIndent;
Expand All @@ -275,6 +283,7 @@ public void pad(int col, boolean absoluteColumn) {
write$(' '); // Always a space.
column++;
}
return this;
}

private void padInternal() {
Expand Down Expand Up @@ -303,16 +312,18 @@ public int getCol() {
return column;
}

public void incIndent() { incIndent(unitIndent); }
public IndentedWriter incIndent() { incIndent(unitIndent); return this; }

public void incIndent(int x) {
public IndentedWriter incIndent(int x) {
currentIndent += x;
return this;
}

public void decIndent() { decIndent(unitIndent); }
public IndentedWriter decIndent() { decIndent(unitIndent); return this; }

public void decIndent(int x) {
public IndentedWriter decIndent(int x) {
currentIndent -= x;
return this;
}

/** Position past current indent */
Expand Down Expand Up @@ -394,7 +405,7 @@ public IndentedWriter setLinePrefix(String str) {
return rtnObject();
}

public int getUnitIndent() { return unitIndent; }
public int getUnitIndent() { return unitIndent; }

public IndentedWriter setUnitIndent(int x) {
unitIndent = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

import java.io.StringWriter ;

public class StringWriterI extends Writer2
{
public StringWriterI()
{
super(new StringWriter()) ;
public class StringWriterI extends Writer2 {
public StringWriterI() {
super(new StringWriter());
}
}

21 changes: 14 additions & 7 deletions jena-base/src/main/java/org/apache/jena/atlas/io/Writer2.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,43 @@ protected Writer2(Writer writer) {
}

@Override
public void print(char ch) {
public AWriter print(char ch) {
try {
writer.write(ch);
} catch (IOException ex) {
IO.exception(ex);
}
return this;
}

@Override
public void print(String string) {
public AWriter print(String string) {
try {
writer.write(string);
} catch (IOException ex) {
IO.exception(ex);
}
return this;
}

@Override
public void print(char[] cbuf) {
public AWriter print(char[] cbuf) {
try {
writer.write(cbuf);
} catch (IOException ex) {
IO.exception(ex);
}
return this;
}

@Override
public void flush() {
public AWriter flush() {
try {
writer.flush();
} catch (IOException ex) {
IO.exception(ex);
}
return this;
}

@Override
Expand All @@ -94,19 +98,22 @@ public void close() {
}

@Override
public void printf(String fmt, Object...args) {
public AWriter printf(String fmt, Object...args) {
print(String.format(fmt, args));
return this;
}

@Override
public void println(String obj) {
public AWriter println(String obj) {
print(obj);
print("\n");
return this;
}

@Override
public void println() {
public AWriter println() {
print("\n");
return this;
}

@Override
Expand Down

0 comments on commit 08c59df

Please sign in to comment.