MessageFormatter { static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray) { if (argArray == null || argArray.length == 0) { return new FormattingTuple(messagePattern, null); } int lastArrIdx = argArray.length - 1; Object lastEntry = argArray[lastArrIdx]; Throwable throwable = lastEntry instanceof Throwable? (Throwable) lastEntry : null; if (messagePattern == null) { return new FormattingTuple(null, throwable); } int j = messagePattern.indexOf(DELIM_STR); if (j == -1) { return new FormattingTuple(messagePattern, throwable); } StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50); int i = 0; int L = 0; do { boolean notEscaped = j == 0 || messagePattern.charAt(j - 1) != ESCAPE_CHAR; if (notEscaped) { sbuf.append(messagePattern, i, j); } else { sbuf.append(messagePattern, i, j - 1); notEscaped = j >= 2 && messagePattern.charAt(j - 2) == ESCAPE_CHAR; } i = j + 2; if (notEscaped) { deeplyAppendParameter(sbuf, argArray[L], null); L++; if (L > lastArrIdx) { break; } } else { sbuf.append(DELIM_STR); } j = messagePattern.indexOf(DELIM_STR, i); } while (j != -1); sbuf.append(messagePattern, i, messagePattern.length()); return new FormattingTuple(sbuf.toString(), L <= lastArrIdx? throwable : null); } private  MessageFormatter();   }
@Test public void testNullArray() { String msg0 = "msg0"; String msg1 = "msg1 {}"; String msg2 = "msg2 {} {}"; String msg3 = "msg3 {} {} {}"; Object[] args = null; String result = MessageFormatter.arrayFormat(msg0, args).getMessage(); assertEquals(msg0, result); result = MessageFormatter.arrayFormat(msg1, args).getMessage(); assertEquals(msg1, result); result = MessageFormatter.arrayFormat(msg2, args).getMessage(); assertEquals(msg2, result); result = MessageFormatter.arrayFormat(msg3, args).getMessage(); assertEquals(msg3, result); }
@Test public void testArrayFormat() { Integer[] ia0 = { 1, 2, 3 }; String result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia0).getMessage(); assertEquals("Value 1 is smaller than 2 and 3.", result); result = MessageFormatter.arrayFormat("{}{}{}", ia0).getMessage(); assertEquals("123", result); result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0).getMessage(); assertEquals("Value 1 is smaller than 2.", result); result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0).getMessage(); assertEquals("Value 1 is smaller than 2", result); result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage(); assertEquals("Val=1, {, Val=2", result); result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage(); assertEquals("Val=1, {, Val=2", result); result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0).getMessage(); assertEquals("Val1=1, Val2={", result); }
@Test public void testMultiDimensionalArrayValues() { Integer[] ia0 = { 1, 2, 3 }; Integer[] ia1 = { 10, 20, 30 }; Integer[][] multiIntegerA = { ia0, ia1 }; String result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntegerA }).getMessage(); assertEquals("a[[1, 2, 3], [10, 20, 30]]", result); int[][] multiIntA = { { 1, 2 }, { 10, 20 } }; result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntA }).getMessage(); assertEquals("a[[1, 2], [10, 20]]", result); float[][] multiFloatA = { { 1, 2 }, { 10, 20 } }; result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiFloatA }).getMessage(); assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result); Object[][] multiOA = { ia0, ia1 }; result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiOA }).getMessage(); assertEquals("a[[1, 2, 3], [10, 20, 30]]", result); Object[][][] _3DOA = { multiOA, multiOA }; result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA }).getMessage(); assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]", result); Byte[] ba0 = { 0, Byte.MAX_VALUE, Byte.MIN_VALUE }; Short[] sa0 = { 0, Short.MIN_VALUE, Short.MAX_VALUE }; result = MessageFormatter.arrayFormat("{}\\{}{}", new Object[] { new Object[] { ba0, sa0 }, ia1 }).getMessage(); assertEquals("[[0, 127, -128], [0, -32768, 32767]]{}[10, 20, 30]", result); }
@Test public void testCyclicArrays() { Object[] cyclicA = new Object[1]; cyclicA[0] = cyclicA; assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA).getMessage()); Object[] a = new Object[2]; a[0] = 1; Object[] c = { 3, a }; Object[] b = { 2, c }; a[1] = b; assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a).getMessage()); }
@Test public void testArrayThrowable() { FormattingTuple ft; Throwable t = new Throwable(); Object[] ia = { 1, 2, 3, t }; ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia); assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("{}{}{}", ia); assertEquals("123", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia); assertEquals("Value 1 is smaller than 2.", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia); assertEquals("Value 1 is smaller than 2", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia); assertEquals("Val=1, {, Val=2", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia); assertEquals("Val=1, \\{, Val=2", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Val1={}, Val2={", ia); assertEquals("Val1=1, Val2={", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia); assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage()); assertEquals(t, ft.getThrowable()); ft = MessageFormatter.arrayFormat("{}{}{}{}", ia); assertEquals("123java.lang.Throwable", ft.getMessage()); assertNull(ft.getThrowable()); }