[slf4j-dev] svn commit: r1087 - in slf4j/trunk/slf4j-api/src: main/java/org/slf4j/helpers test/java/org/slf4j/helpers

ceki at slf4j.org ceki at slf4j.org
Fri Aug 1 17:55:27 CEST 2008


Author: ceki
Date: Fri Aug  1 17:55:26 2008
New Revision: 1087

Modified:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java

Log:
- redone support for array parameters using JDK 1.4 functionality
  multi-dimensional array are now also supported

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java	Fri Aug  1 17:55:26 2008
@@ -99,6 +99,8 @@
     HashMap hashMap = (HashMap) inheritableThreadLocal.get();
     if (hashMap != null) {
       hashMap.clear();
+      // the InheritableThreadLocal.remove method was introduced in JDK 1.5
+      // Thus, invoking clear() on previous JDK's will fail
       inheritableThreadLocal.remove();
     }
   }

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java	Fri Aug  1 17:55:26 2008
@@ -24,10 +24,8 @@
 
 package org.slf4j.helpers;
 
-import java.util.Arrays;
-
-// contributors: lizongbo
-
+// contributors: lizongbo: proposed special treatment of array parameter values
+// Jörn Huxhorn: pointed out double[] omission, suggested deep array copy
 /**
  * Formats messages according to very simple substitution rules. Substitutions
  * can be made 1, 2 or more arguments.
@@ -181,7 +179,7 @@
             // itself escaped: "abc x:\\{}"
             // we have to consume one backward slash
             sbuf.append(messagePattern.substring(i, j - 1));
-            appendParameter(sbuf, argArray[L]);
+            deeplyAppendParameter(sbuf, argArray[L]);
             // sbuf.append(argArray[L]);
             i = j + 2;
           }
@@ -192,7 +190,7 @@
         } else {
           // normal case
           sbuf.append(messagePattern.substring(i, j));
-          appendParameter(sbuf, argArray[L]);
+          deeplyAppendParameter(sbuf, argArray[L]);
           i = j + 2;
         }
       }
@@ -226,29 +224,134 @@
   }
 
   // special treatment of array values was suggested by 'lizongbo'
-  private static void appendParameter(StringBuffer sbuf, Object o) {
-    if (o != null && o.getClass().isArray()) {
-      // check for primitive arrays because they unfortunately 
-      // cannot be cast to Object[]
+  private static void deeplyAppendParameter(StringBuffer sbuf, Object o) {
+    if (o == null) {
+      sbuf.append("null");
+      return;
+    }
+    if (!o.getClass().isArray()) {
+      sbuf.append(o);
+    } else {
+      // check for primitive array types because they
+      // unfortunately cannot be cast to Object[]
       if (o instanceof boolean[]) {
-        sbuf.append(Arrays.toString((boolean[]) o));
+        booleanArrayAppend(sbuf, (boolean[]) o);
       } else if (o instanceof byte[]) {
-        sbuf.append(Arrays.toString((byte[]) o));
+        byteArrayAppend(sbuf, (byte[]) o);
       } else if (o instanceof char[]) {
-        sbuf.append(Arrays.toString((char[]) o));
+        charArrayAppend(sbuf, (char[]) o);
       } else if (o instanceof short[]) {
-        sbuf.append(Arrays.toString((short[]) o));
+        shortArrayAppend(sbuf, (short[]) o);
       } else if (o instanceof int[]) {
-        sbuf.append(Arrays.toString((int[]) o));
+        intArrayAppend(sbuf, (int[]) o);
       } else if (o instanceof long[]) {
-        sbuf.append(Arrays.toString((long[]) o));
+        longArrayAppend(sbuf, (long[]) o);
       } else if (o instanceof float[]) {
-        sbuf.append(Arrays.toString((float[]) o));
+        floatArrayAppend(sbuf, (float[]) o);
+      } else if (o instanceof double[]) {
+        doubleArrayAppend(sbuf, (double[]) o);
       } else {
-        sbuf.append(Arrays.toString((Object[]) o));
+        objectArrayAppend(sbuf, (Object[]) o);
       }
-    } else {
-      sbuf.append(o);
     }
   }
+
+  private static void booleanArrayAppend(StringBuffer sbuf, boolean[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void byteArrayAppend(StringBuffer sbuf, byte[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void charArrayAppend(StringBuffer sbuf, char[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void shortArrayAppend(StringBuffer sbuf, short[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void intArrayAppend(StringBuffer sbuf, int[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void longArrayAppend(StringBuffer sbuf, long[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void floatArrayAppend(StringBuffer sbuf, float[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void doubleArrayAppend(StringBuffer sbuf, double[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      sbuf.append(a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
+
+  private static void objectArrayAppend(StringBuffer sbuf, Object[] a) {
+    sbuf.append('[');
+    final int len = a.length;
+    for (int i = 0; i < len; i++) {
+      deeplyAppendParameter(sbuf, a[i]);
+      if (i != len - 1)
+        sbuf.append(", ");
+    }
+    sbuf.append(']');
+  }
 }

Modified: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java	(original)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java	Fri Aug  1 17:55:26 2008
@@ -33,10 +33,6 @@
 
 package org.slf4j.helpers;
 
-import java.util.Arrays;
-
-import org.slf4j.helpers.MessageFormatter;
-
 import junit.framework.TestCase;
 
 /**
@@ -48,7 +44,9 @@
   Integer i1 = new Integer(1);
   Integer i2 = new Integer(2);
   Integer i3 = new Integer(3);
-
+  Integer[] ia0 = new Integer[] { i1, i2, i3 };
+  Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20), new Integer(30) };
+  
   public void testNull() {
     String result;
     result = MessageFormatter.format(null, i1);
@@ -182,54 +180,95 @@
   public void testArrayFormat() {
     String result;
 
-    Integer[] ia = new Integer[] { i1, i2, i3 };
-
     result = MessageFormatter.arrayFormat(
-        "Value {} is smaller than {} and {}.", ia);
+        "Value {} is smaller than {} and {}.", ia0);
     assertEquals("Value 1 is smaller than 2 and 3.", result);
 
-    result = MessageFormatter.arrayFormat("{}{}{}", ia);
+    result = MessageFormatter.arrayFormat("{}{}{}", ia0);
     assertEquals("123", result);
 
-    result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
+    result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0);
     assertEquals("Value 1 is smaller than 2.", result);
 
-    result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
+    result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0);
     assertEquals("Value 1 is smaller than 2", result);
 
-    result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
+    result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0);
     assertEquals("Val=1, {, Val={}", result);
 
-    result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
+    result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia0);
     assertEquals("Val=1, {, Val=2", result);
 
-    result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
+    result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0);
     assertEquals("Val1=1, Val2={", result);
   }
 
   public void testArrayValues() {
-
     String result;
-
     Integer p0 = i1;
     Integer[] p1 = new Integer[] { i2, i3 };
 
-    System.out.println("[" + Arrays.toString(new int[] { 1, 2 }) + "]");
-
     result = MessageFormatter.format("{}{}", p0, p1);
-    assertEquals(p0 + Arrays.toString(p1), result);
+    assertEquals("1[2, 3]", result);
 
-    {
-      Object[] pa = new Object[] { "a", p1 };
-      result = MessageFormatter.arrayFormat("{}{}", pa);
-      assertEquals("a" + Arrays.toString(p1), result);
-    }
-
-    {
-      Object[] pa = new Object[] { "a", new int[] { 1, 2 } };
-      result = MessageFormatter.arrayFormat("{}{}", pa);
-      assertEquals("a" + Arrays.toString(new int[] { 1, 2 }), result);
-    }
-  }
+    // Integer[]
+    result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 });
+    assertEquals("a[2, 3]", result);
+
+    // byte[]
+    result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
+        new byte[] { 1, 2 } });
+    assertEquals("a[1, 2]", result);
+    
+    // int[]
+    result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
+        new int[] { 1, 2 } });
+    assertEquals("a[1, 2]", result);
+
+    // float[]
+    result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
+        new float[] { 1, 2 } });
+    assertEquals("a[1.0, 2.0]", result);
+
+    // double[]
+    result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
+        new double[] { 1, 2 } });
+    assertEquals("a[1.0, 2.0]", result);
+    
+    
+  }
+
+  public void testMultiDimensionalArrayValues() {
+    String result;
+    
+    Integer[][] multiIntegerA = new Integer[][] {ia0, ia1};
+    result = MessageFormatter.arrayFormat("{}{}",
+        new Object[] { "a", multiIntegerA });
+    assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
+
+    
+    int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
+    result = MessageFormatter.arrayFormat("{}{}",
+        new Object[] { "a", multiIntA });
+    assertEquals("a[[1, 2], [10, 20]]", result);
+
+    float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
+    result = MessageFormatter.arrayFormat("{}{}",
+        new Object[] { "a", multiFloatA });
+    assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
+
+    
+    Object[][] multiOA = new Object[][] { ia0, ia1 };
+    result = MessageFormatter.arrayFormat("{}{}",
+        new Object[] { "a", multiOA });
+    assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
+
+    Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
+    result = MessageFormatter.arrayFormat("{}{}",
+        new Object[] { "a", _3DOA });
+    assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]", result);
 
+    
+    
+  }
 }



More information about the slf4j-dev mailing list