[slf4j-dev] svn commit: r1095 - in slf4j/trunk/slf4j-api/src: main/java/org/slf4j/helpers test/java/org/slf4j/helpers
ceki at slf4j.org
ceki at slf4j.org
Tue Aug 5 22:03:49 CEST 2008
Author: ceki
Date: Tue Aug 5 22:03:49 2008
New Revision: 1095
Modified:
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:
- support for cyclical arrays
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 Tue Aug 5 22:03:49 2008
@@ -24,6 +24,9 @@
package org.slf4j.helpers;
+import java.util.HashMap;
+import java.util.Map;
+
// contributors: lizongbo: proposed special treatment of array parameter values
// Jörn Huxhorn: pointed out double[] omission, suggested deep array copy
/**
@@ -179,7 +182,7 @@
// itself escaped: "abc x:\\{}"
// we have to consume one backward slash
sbuf.append(messagePattern.substring(i, j - 1));
- deeplyAppendParameter(sbuf, argArray[L]);
+ deeplyAppendParameter(sbuf, argArray[L], new HashMap());
// sbuf.append(argArray[L]);
i = j + 2;
}
@@ -190,7 +193,7 @@
} else {
// normal case
sbuf.append(messagePattern.substring(i, j));
- deeplyAppendParameter(sbuf, argArray[L]);
+ deeplyAppendParameter(sbuf, argArray[L], new HashMap());
i = j + 2;
}
}
@@ -224,7 +227,8 @@
}
// special treatment of array values was suggested by 'lizongbo'
- private static void deeplyAppendParameter(StringBuffer sbuf, Object o) {
+ private static void deeplyAppendParameter(StringBuffer sbuf, Object o,
+ Map seenMap) {
if (o == null) {
sbuf.append("null");
return;
@@ -251,11 +255,30 @@
} else if (o instanceof double[]) {
doubleArrayAppend(sbuf, (double[]) o);
} else {
- objectArrayAppend(sbuf, (Object[]) o);
+ objectArrayAppend(sbuf, (Object[]) o, seenMap);
}
}
}
+ private static void objectArrayAppend(StringBuffer sbuf, Object[] a,
+ Map seenMap) {
+ sbuf.append('[');
+ if (!seenMap.containsKey(a)) {
+ seenMap.put(a, null);
+ final int len = a.length;
+ for (int i = 0; i < len; i++) {
+ deeplyAppendParameter(sbuf, a[i], seenMap);
+ if (i != len - 1)
+ sbuf.append(", ");
+ }
+ // allow repeats in siblings
+ seenMap.remove(a);
+ } else {
+ sbuf.append("...");
+ }
+ sbuf.append(']');
+ }
+
private static void booleanArrayAppend(StringBuffer sbuf, boolean[] a) {
sbuf.append('[');
final int len = a.length;
@@ -343,15 +366,4 @@
}
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 Tue Aug 5 22:03:49 2008
@@ -45,8 +45,9 @@
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) };
-
+ Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20),
+ new Integer(30) };
+
public void testNull() {
String result;
result = MessageFormatter.format(null, i1);
@@ -219,7 +220,7 @@
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 } });
@@ -234,41 +235,51 @@
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 });
+
+ 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 });
+ 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 });
+ 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);
-
-
-
+ result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA });
+ assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]",
+ result);
+ }
+
+ public void testCyclicArrays() {
+ {
+ Object[] cyclicA = new Object[1];
+ cyclicA[0] = cyclicA;
+ assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA));
+ }
+ {
+ Object[] a = new Object[2];
+ a[0] = i1;
+ Object[] c = new Object[] {i3, a};
+ Object[] b = new Object[] {i2, c};
+ a[1] = b;
+ assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a));
+ }
}
}
More information about the slf4j-dev
mailing list