jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [2/2] jena git commit: Reformat and add some javadoc
Date Tue, 02 Aug 2016 08:07:26 GMT
Reformat and add some javadoc


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/61368dfa
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/61368dfa
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/61368dfa

Branch: refs/heads/master
Commit: 61368dfa07343b04fd0e2799837ef80e40c5c3c0
Parents: c98382f
Author: Andy Seaborne <andy@apache.org>
Authored: Tue Aug 2 08:59:20 2016 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Tue Aug 2 08:59:20 2016 +0100

----------------------------------------------------------------------
 .../apache/jena/atlas/io/IndentedWriter.java    | 192 +++++++++----------
 .../jena/atlas/io/TestIndentedWriter.java       |   3 +-
 2 files changed, 93 insertions(+), 102 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/61368dfa/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java
index ba4282d..a5ba784 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java
@@ -21,6 +21,7 @@ import static java.lang.String.format ;
 
 import java.io.IOException ;
 import java.io.OutputStream ;
+import java.io.PrintStream ;
 import java.io.Writer ;
 
 import org.apache.jena.atlas.lib.Closeable ;
@@ -40,6 +41,7 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable
         stdout.setFlushOnNewline(true) ;
         stderr.setFlushOnNewline(true) ;
     }
+    
     // Note cases:if (!flatMode) 
     // 1/ incIndent - decIndent with no output should not cause any padding
     // 2/ newline() then no text, then finish should not cause a line number.
@@ -66,13 +68,11 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable
     public IndentedWriter(OutputStream outStream) { this(outStream, false) ; }
     
     /** Construct a UTF8 IndentedWriter around an OutputStream */
-    public IndentedWriter(OutputStream outStream, boolean withLineNumbers)
-    {
+    public IndentedWriter(OutputStream outStream, boolean withLineNumbers) {
         this(makeWriter(outStream), withLineNumbers) ;
     }
-    
-    private static Writer makeWriter(OutputStream out)
-    {
+
+    private static Writer makeWriter(OutputStream out) {
         // return BufferingWriter.create(out) ;
         return IO.asBufferedUTF8(out) ;
     }
@@ -81,20 +81,17 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable
     protected IndentedWriter(Writer writer) { this(writer, false) ; }
     
     /** Using Writers directly is discouraged */
-    protected IndentedWriter(Writer writer, boolean withLineNumbers)
-    {
+    protected IndentedWriter(Writer writer, boolean withLineNumbers) {
         out = writer ;
         lineNumbers = withLineNumbers ;
         startingNewLine = true ;
     }
 
     @Override
-    public void print(String str) 
-    {
+    public void print(String str) {
         if ( str == null )
             str = "null" ;
-        if ( false )
-        {
+        if ( false ) {
             // Don't check for embedded newlines.
             write$(str) ;
             return ;
@@ -102,10 +99,9 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable
         for ( int i = 0 ; i < str.length() ; i++ )
             printOneChar(str.charAt(i)) ;
     }
-    
+
     @Override
-    public void printf(String formatStr, Object... args)
-    {
+    public void printf(String formatStr, Object... args) {
         print(format(formatStr, args)) ;
     }
     
@@ -120,45 +116,41 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
     public void println() { newline() ; }
     
     @Override
-    public void print(char[] cbuf)
-    {
-        for ( char aCbuf : cbuf )
-        {
-            printOneChar( aCbuf );
+    public void print(char[] cbuf) {
+        for ( char aCbuf : cbuf ) {
+            printOneChar(aCbuf) ;
         }
     }
 
     /** Print a string N times */
-    public void print(String s, int n)
-    {
-        for ( int i = 0 ; i < n ; i++ ) print(s) ;
+    public void print(String s, int n) {
+        for ( int i = 0 ; i < n ; i++ )
+            print(s) ;
     }
 
     /** Print a char N times */
-    public void print(char ch, int n)
-    {
+    public void print(char ch, int n) {
         lineStart() ;
-        for ( int i = 0 ; i < n ; i++ ) printOneChar(ch) ;
+        for ( int i = 0 ; i < n ; i++ )
+            printOneChar(ch) ;
     }
 
     private char lastChar = '\0' ;
+
     // Worker
-    private void printOneChar(char ch) 
-    {
+    private void printOneChar(char ch) {
         // Turn \r\n into a single newline call.
-        // Assumes we don't get \r\r\n etc 
-        if ( ch == '\n' && lastChar == '\r' )
-        {
+        // Assumes we don't get \r\r\n etc
+        if ( ch == '\n' && lastChar == '\r' ) {
             lastChar = ch ;
             return ;
         }
-        
-        lineStart() ; 
+
+        lineStart() ;
         lastChar = ch ;
-        
+
         // newline
-        if ( ch == '\n' || ch == '\r' )
-        { 
+        if ( ch == '\n' || ch == '\r' ) {
             newline() ;
             return ;
         }
@@ -172,27 +164,25 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
     private void write$(String s) 
     { try { out.write(s) ; } catch (IOException ex) { IO.exception(ex); } }
     
-    public void newline()
-    {
-        lineStart() ; 
+    public void newline() {
+        lineStart() ;
 
         if ( endOfLineMarker != null )
             print(endOfLineMarker) ;
-        if ( ! flatMode )
+        if ( !flatMode )
             write$('\n') ;
         startingNewLine = true ;
         row++ ;
         column = 0 ;
         // Note that PrintWriters do not autoflush by default
-        // so if layered over a PrintWriter, need to flush that as well.  
-        if (flushOnNewline) 
+        // so if layered over a PrintWriter, need to flush that as well.
+        if ( flushOnNewline )
             flush() ;
     }
     
     private boolean atStartOfLine() { return column <= currentIndent ; }
 
-    public void ensureStartOfLine()
-    {
+    public void ensureStartOfLine() {
         if ( !atStartOfLine() )
             newline() ;
     }
@@ -204,8 +194,7 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable
     public void flush() { IO.flush(out); }
     
     /** Pad to the indent (if we are before it) */
-    public void pad()
-    {
+    public void pad() {
         if ( startingNewLine && currentIndent > 0 )
             lineStart() ;
         padInternal() ;
@@ -222,34 +211,25 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
      * @param col Column number (first column is 1).
      * @param absoluteColumn Whether to include the indent
      */
-    public void pad(int col, boolean absoluteColumn )
-    {
+    public void pad(int col, boolean absoluteColumn) {
         // Make absolute
         if ( !absoluteColumn )
-            col = col+currentIndent ;
-        int spaces = col - column  ;
-        for ( int i = 0 ; i < spaces ; i++ )
-        {
+            col = col + currentIndent ;
+        int spaces = col - column ;
+        for ( int i = 0 ; i < spaces ; i++ ) {
             write$(' ') ;        // Always a space.
             column++ ;
         }
-    }
-    
+    }    
     
-    private void padInternal() 
-    {
-        if ( padString == null )
-        {
-            for ( int i = column ; i < currentIndent ; i++ )
-            {
+    private void padInternal() {
+        if ( padString == null ) {
+            for ( int i = column ; i < currentIndent ; i++ ) {
                 write$(padChar) ;
                 column++ ;
             }
-        }
-        else
-        {
-            for ( int i = column ; i < currentIndent ; i += padString.length() )
-            {
+        } else {
+            for ( int i = column ; i < currentIndent ; i += padString.length() ) {
                 write$(padString) ;
                 column += padString.length() ;
             }
@@ -273,23 +253,20 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
     /** Set indent from the left hand edge */ 
     public void setAbsoluteIndent(int x) { currentIndent = x ; }
 
-    /** Position past current indent */ 
-    public int getCurrentOffset()
-    { 
+    /** Position past current indent */
+    public int getCurrentOffset() {
         int x = getCol() - getAbsoluteIndent() ;
         if ( x >= 0 )
             return x ;
         // At start of line somehow.
         return 0 ;
     }
-    
-    public boolean hasLineNumbers()
-    {
+
+    public boolean hasLineNumbers() {
         return lineNumbers ;
     }
 
-    public void setLineNumbers(boolean lineNumbers)
-    {
+    public void setLineNumbers(boolean lineNumbers) {
         this.lineNumbers = lineNumbers ;
     }
     
@@ -298,32 +275,40 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
     /** Set the marker included at end of line - set to null for "none".  Usually used for
debugging. */ 
     public void setEndOfLineMarker(String marker)   { endOfLineMarker = marker ; }
     
-    /** Flat mode - print without NL, for a more compact representation - depends on caller
*/  
-    public boolean inFlatMode()                 { return flatMode ; }
-    public void setFlatMode(boolean flatMode)   { this.flatMode = flatMode ; }
+    /** Flat mode - print without NL, for a more compact representation*/  
+    public boolean inFlatMode()                     { return flatMode ; }
+    /** Flat mode - print without NL, for a more compact representation*/  
+    public void setFlatMode(boolean flatMode)       { this.flatMode = flatMode ; }
     
     /** Flush on newline **/
-    public boolean getFlushOnNewline()      { return flushOnNewline; }
+    public boolean getFlushOnNewline()              { return flushOnNewline; }
+    /** Flush on newline in this code.
+     * This is set for {@link IndentedWriter#stdout} and {@link IndentedWriter#stderr}
+     * but not by default otherwise. The underlying output, if it is a {@link PrintStream}
+     * may also have a flush on newline as well (e.g {@link System#out}). 
+     */
     public void setFlushOnNewline(boolean flushOnNewline) 
     { this.flushOnNewline = flushOnNewline; } 
     
-    public char getPadChar()                { return padChar ; }
-    public void setPadChar(char ch)         { this.padChar  = ch ; }
-    public String getPadString()            { return padString ; }
-    public void setPadString(String str)    { this.padString = str ; unitIndent = str.length();
}
-    /** */
-    public String getLinePrefix()           { return linePrefix  ; }
-    public void setLinePrefix(String str)   { this.linePrefix = str ; }
+    public char getPadChar()                        { return padChar ; }
+    public void setPadChar(char ch)                 { this.padChar  = ch ; }
+    public String getPadString()                    { return padString ; }
+    public void setPadString(String str)            { this.padString = str ; unitIndent =
str.length(); }
+    
+    /** Initial string printed at the start of each line : defaults to no string. */
+    public String getLinePrefix()                   { return linePrefix  ; }
+    /** Set the initial string printed at the start of each line. */
+    public void setLinePrefix(String str)           { this.linePrefix = str ; }
+
+    public void incIndent() { incIndent(unitIndent) ; }
 
-    public void incIndent()      { incIndent(unitIndent) ; }
-    public void incIndent(int x)
-    {
+    public void incIndent(int x) {
         currentIndent += x ;
     }
 
     public void decIndent() { decIndent(unitIndent) ; }
-    public void decIndent(int x) 
-    {
+
+    public void decIndent(int x) {
         currentIndent -= x ;
     }
     
@@ -332,19 +317,17 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
     public boolean atLineStart()        { return startingNewLine ; }
     
     // A line is prefix?number?content.
-    private void lineStart()
-    {
-        if ( flatMode )
-        {
+    private void lineStart() {
+        if ( flatMode ) {
             if ( startingNewLine && row > 1 )
                 // Space between each line.
                 write$(' ') ;
             startingNewLine = false ;
             return ;
         }
-        
+
         // Need to do its just before we append anything, not after a NL,
-        // so that a final blank does not cause a prefix or line number.  
+        // so that a final blank does not cause a prefix or line number.
         if ( startingNewLine ) {
             if ( linePrefix != null )
                 write$(linePrefix) ;
@@ -353,22 +336,29 @@ public class IndentedWriter extends AWriterBase implements AWriter,
Closeable
         padInternal() ;
         startingNewLine = false ;
     }
+
+    private int widthLineNumber = 3 ;
     
-    private static int WidthLineNumber = 3 ;
+    /** Width of the number field */
+    public int getNumberWidth() { return widthLineNumber ; }
     
-    private void insertLineNumber()
-    {
-        if ( ! lineNumbers )
+    /** Set the width of the number field.
+     * There is also a singel space after the number not included in this setting.  
+     */
+    public void setNumberWidth(int widthOfNumbers) { widthLineNumber = widthOfNumbers ; }
+
+    private void insertLineNumber() {
+        if ( !lineNumbers )
             return ;
         String s = Integer.toString(row) ;
-        for ( int i = 0 ; i < WidthLineNumber-s.length() ; i++ )
+        for ( int i = 0 ; i < widthLineNumber - s.length() ; i++ )
             write$(' ') ;
         write$(s) ;
         write$(' ') ;
     }
-    
+
     @Override
     public String toString() {
-        return String.format("Indent = %d : [%d, %d]", currentIndent, row, column) ;  
+        return String.format("Indent = %d : [%d, %d]", currentIndent, row, column) ;
     }
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/61368dfa/jena-base/src/test/java/org/apache/jena/atlas/io/TestIndentedWriter.java
----------------------------------------------------------------------
diff --git a/jena-base/src/test/java/org/apache/jena/atlas/io/TestIndentedWriter.java b/jena-base/src/test/java/org/apache/jena/atlas/io/TestIndentedWriter.java
index 05f6bce..31405bf 100644
--- a/jena-base/src/test/java/org/apache/jena/atlas/io/TestIndentedWriter.java
+++ b/jena-base/src/test/java/org/apache/jena/atlas/io/TestIndentedWriter.java
@@ -60,9 +60,10 @@ public class TestIndentedWriter {
     public void write04() {
         try (IndentedLineBuffer b = new IndentedLineBuffer()) {
             b.setLineNumbers(true);
+            b.setNumberWidth(5);
             b.println("ABCD") ;
             b.print("XYZ") ;
-            assertEquals("  1 ABCD\n  2 XYZ", b.asString()) ;
+            assertEquals("    1 ABCD\n    2 XYZ", b.asString()) ;
         }
     }
     


Mime
View raw message