StringUtil { public static String createString( String pattern, Object... parameters ) { CheckArg.isNotNull(pattern, "pattern"); if (parameters == null) parameters = EMPTY_STRING_ARRAY; Matcher matcher = PARAMETER_COUNT_PATTERN.matcher(pattern); StringBuffer text = new StringBuffer(); int requiredParameterCount = 0; boolean err = false; while (matcher.find()) { int ndx = Integer.valueOf(matcher.group(1)); if (requiredParameterCount <= ndx) { requiredParameterCount = ndx + 1; } if (ndx >= parameters.length) { err = true; matcher.appendReplacement(text, matcher.group()); } else { Object parameter = parameters[ndx]; if (parameter != null && parameter.getClass().isArray()) { if (parameter instanceof Object[]) { parameter = Arrays.asList((Object[])parameter); } else { int length = Array.getLength(parameter); List<Object> parameterAsList = new ArrayList<Object>(length); for (int i = 0; i < length; i++) { parameterAsList.add(Array.get(parameter, i)); } parameter = parameterAsList; } } matcher.appendReplacement(text, Matcher.quoteReplacement(parameter == null ? "null" : parameter.toString())); } } if (err || requiredParameterCount < parameters.length) { throw new IllegalArgumentException( CommonI18n.requiredToSuppliedParameterMismatch.text(parameters.length, parameters.length == 1 ? "" : "s", requiredParameterCount, requiredParameterCount == 1 ? "" : "s", pattern, text.toString())); } matcher.appendTail(text); return text.toString(); } private  StringUtil(); static String combineLines( String[] lines ); static String combineLines( String[] lines,
                                       char separator ); static List<String> splitLines( final String content ); static String join( Object[] values,
                               String delimiter ); static String join( Iterable<?> values,
                               String delimiter ); static String createString( String pattern,
                                       Object... parameters ); static String createString( final char charToRepeat,
                                       int numberOfRepeats ); static String setLength( String original,
                                    int length,
                                    char padChar ); static String justify( Justify justify,
                                  String str,
                                  final int width,
                                  char padWithChar ); static String justifyRight( String str,
                                       final int width,
                                       char padWithChar ); static String justifyLeft( String str,
                                      final int width,
                                      char padWithChar ); static String justifyCenter( String str,
                                        final int width,
                                        char padWithChar ); static String truncate( Object obj,
                                   int maxLength ); static String truncate( Object obj,
                                   int maxLength,
                                   String suffix ); static String read( Reader reader ); static String read( InputStream stream ); static void write( String content,
                              OutputStream stream ); static void write( String content,
                              Writer writer ); static String getStackTrace( Throwable throwable ); static String normalize( String text ); static String getHexString( byte[] bytes ); static byte[] fromHexString( String hexadecimal ); static boolean isHexString( String hexadecimal ); static boolean isHexCharacter( char c ); static boolean isBlank( String str ); static boolean notBlank( String str ); static boolean containsAnyOf( String str,
                                         char... chars ); static final String[] EMPTY_STRING_ARRAY; }
@Test( expected = IllegalArgumentException.class ) public void createStringShouldFailIfNoPatternSupplied() { StringUtil.createString(null, (Object[])null); }
@Test public void createStringShouldAllowNoParametersSupplied() { assertThat(StringUtil.createString("test", (Object[])null), is("test")); }
@Test public void createStringShouldCreateStringFromPattern() { String pattern = "This {0} is {1} should {2} not {3} last {4}"; assertEquals("This one is two should three not four last five", StringUtil.createString(pattern, "one", "two", "three", "four", "five")); }
@Test( expected = IllegalArgumentException.class ) public void createStringShouldFailIfTooFewArgumentsSupplied() { String pattern = "This {0} is {1} should {2} not {3} last {4}"; try { StringUtil.createString(pattern, "one", "two", "three", "four"); } catch (IllegalArgumentException err) { System.err.println(err); throw err; } }
@Test( expected = IllegalArgumentException.class ) public void createStringShouldFailIfTooManyArgumentsSupplied() { String pattern = "This {0} is {1} should {2} not {3} last {4}"; try { StringUtil.createString(pattern, "one", "two", "three", "four", "five", "six"); } catch (IllegalArgumentException err) { System.err.println(err); throw err; } }
@Test public void createStringExceptionMessageShouldbeGrammaticallyCorrect() { String pattern = "One = {0}"; try { StringUtil.createString(pattern); } catch (IllegalArgumentException err) { assertThat(err.getMessage().startsWith("0 parameters supplied, but 1 parameter required"), is(true)); } pattern = "One"; try { StringUtil.createString(pattern, "one"); } catch (IllegalArgumentException err) { assertThat(err.getMessage().startsWith("1 parameter supplied, but 0 parameters required"), is(true)); } pattern = "One = {0}, Two = {1}"; try { StringUtil.createString(pattern); } catch (IllegalArgumentException err) { assertThat(err.getMessage().startsWith("0 parameters supplied, but 2 parameters required"), is(true)); } }
@Test @FixFor( "MODE-2322" ) public void shouldSupportPrimitiveArrays() { String text = StringUtil.createString("Error converting \"{2}\" from {0} to a {1}", byte[].class.getSimpleName(), "BinaryValue", new byte[] {-1}); assertEquals("Error converting \"[-1]\" from byte[] to a BinaryValue", text); }