[logback-dev] svn commit: r1679 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/pattern test/java/ch/qos/logback/core/pattern

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat May 3 21:43:35 CEST 2008


Author: ceki
Date: Sat May  3 21:43:34 2008
New Revision: 1679

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/SpacePadder.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/SpacePadderTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/FormattingConverter.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/PackageTest.java

Log:
- refactoring space padding code

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/FormattingConverter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/FormattingConverter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/FormattingConverter.java	Sat May  3 21:43:34 2008
@@ -42,7 +42,7 @@
 
     if (s == null) {
       if (0 < min)
-        spacePad(buf, min);
+        SpacePadder.spacePad(buf, min);
       return;
     }
 
@@ -56,34 +56,12 @@
       }
     } else if (len < min) {
       if (formattingInfo.isLeftPad()) {
-        spacePad(buf, min - len);
-        buf.append(s);
+       SpacePadder.leftPad(buf, s, min);
       } else {
-        buf.append(s);
-        spacePad(buf, min - len);
+        SpacePadder.rightPad(buf, s, min);
       }
     } else {
       buf.append(s);
     }
   }
-
-  final static String[] SPACES = { " ", "  ", "    ", "        ", // 1,2,4,8 spaces
-      "                ", // 16 spaces
-      "                                " }; // 32 spaces
-
-  /**
-   * Fast space padding method.
-   */
-  static public void spacePad(StringBuffer sbuf, int length) {
-    while (length >= 32) {
-      sbuf.append(SPACES[5]);
-      length -= 32;
-    }
-
-    for (int i = 4; i >= 0; i--) {
-      if ((length & (1 << i)) != 0) {
-        sbuf.append(SPACES[i]);
-      }
-    }
-  }
 }

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/SpacePadder.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/SpacePadder.java	Sat May  3 21:43:34 2008
@@ -0,0 +1,60 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.pattern;
+
+public class SpacePadder {
+
+  final static String[] SPACES = { " ", "  ", "    ", "        ", // 1,2,4,8
+      // spaces
+      "                ", // 16 spaces
+      "                                " }; // 32 spaces
+
+  final static public void leftPad(StringBuffer buf, String s, int desiredLength) {
+    int actualLen = 0;
+    if (s != null) {
+      actualLen = s.length();
+    }
+    if (actualLen < desiredLength) {
+      spacePad(buf, desiredLength - actualLen);
+    }
+    if (s != null) {
+      buf.append(s);
+    }
+  }
+
+  final static public void rightPad(StringBuffer buf, String s, int desiredLength) {
+    int actualLen = 0;
+    if (s != null) {
+      actualLen = s.length();
+    }
+    if (s != null) {
+      buf.append(s);
+    }
+    if (actualLen < desiredLength) {
+      spacePad(buf, desiredLength - actualLen);
+    }
+  }
+  
+  /**
+   * Fast space padding method.
+   */
+  final static public void spacePad(StringBuffer sbuf, int length) {
+    while (length >= 32) {
+      sbuf.append(SPACES[5]);
+      length -= 32;
+    }
+
+    for (int i = 4; i >= 0; i--) {
+      if ((length & (1 << i)) != 0) {
+        sbuf.append(SPACES[i]);
+      }
+    }
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/PackageTest.java	Sat May  3 21:43:34 2008
@@ -1,5 +1,6 @@
 package ch.qos.logback.core.pattern;
 
+import junit.framework.JUnit4TestAdapter;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -8,6 +9,7 @@
 
   public static Test suite() {
     TestSuite suite = new TestSuite();
+    suite.addTest(new JUnit4TestAdapter(ch.qos.logback.core.pattern.SpacePadderTest.class));
     suite.addTest(ch.qos.logback.core.pattern.parser.PackageTest.suite());
     return suite;
   }

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/SpacePadderTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/SpacePadderTest.java	Sat May  3 21:43:34 2008
@@ -0,0 +1,94 @@
+package ch.qos.logback.core.pattern;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class SpacePadderTest {
+
+  @BeforeClass
+  public static void setUpBeforeClass() throws Exception {
+  }
+
+  @AfterClass
+  public static void tearDownAfterClass() throws Exception {
+  }
+
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void smoke() {
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "a";
+      SpacePadder.leftPad(buf, s, 4);
+      assertEquals("   a", buf.toString());
+    }
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "a";
+      SpacePadder.rightPad(buf, s, 4);
+      assertEquals("a   ", buf.toString());
+    }
+  }
+
+  @Test
+  public void nullString() {
+    String s = null;
+    {
+      StringBuffer buf = new StringBuffer();
+      SpacePadder.leftPad(buf, s, 2);
+      assertEquals("  ", buf.toString());
+    }
+    {
+      StringBuffer buf = new StringBuffer();
+      SpacePadder.rightPad(buf, s, 2);
+      assertEquals("  ", buf.toString());
+    }
+  }
+
+  @Test
+  public void longString() {
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "abc";
+      SpacePadder.leftPad(buf, s, 2);
+      assertEquals(s, buf.toString());
+    }
+
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "abc";
+      SpacePadder.rightPad(buf, s, 2);
+      assertEquals(s, buf.toString());
+    }
+  }
+  
+  @Test
+  public void lengthyPad() {
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "abc";
+      SpacePadder.leftPad(buf, s, 33);
+      assertEquals("                              abc", buf.toString());
+    }
+    {
+      StringBuffer buf = new StringBuffer();
+      String s = "abc";
+      SpacePadder.rightPad(buf, s, 33);
+      assertEquals("abc                              ", buf.toString());
+    }
+    
+  }
+
+}



More information about the logback-dev mailing list