Refine search
/** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source builder as a template. * The builder is not altered by this method. * * @param source the builder to use as a template, not changed, null returns null * @return the result of the replace operation */ public String replace(final StrBuilder source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length()).append(source); substitute(buf, 0, buf.length()); return buf.toString(); }
/** * Checks if the specified variable is already in the stack (list) of variables. * * @param varName the variable name to check * @param priorVariables the list of prior variables */ private void checkCyclicSubstitution(final String varName, final List<String> priorVariables) { if (priorVariables.contains(varName) == false) { return; } final StrBuilder buf = new StrBuilder(256); buf.append("Infinite loop in property interpolation of "); buf.append(priorVariables.remove(0)); buf.append(": "); buf.appendWithSeparators(priorVariables, "->"); throw new IllegalStateException(buf.toString()); }
/** * List available algorithms. This is displayed to the user when no valid * algorithm is given in the program parameterization. * * @return usage string listing available algorithms */ private static String getAlgorithmsListing() { StrBuilder strBuilder = new StrBuilder(); strBuilder .appendNewLine() .appendln("Select an algorithm to view usage: flink run examples/flink-gelly-examples_<version>.jar --algorithm <algorithm>") .appendNewLine() .appendln("Available algorithms:"); for (Driver algorithm : driverFactory) { strBuilder.append(" ") .appendFixedWidthPadRight(algorithm.getName(), 30, ' ') .append(algorithm.getShortDescription()).appendNewLine(); } return strBuilder.toString(); }
/** * Appends a string buffer followed by a new line to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string buffer to append * @return this, to enable chaining * @since 2.3 */ public StrBuilder appendln(final StringBuffer str) { return append(str).appendNewLine(); }
/** * Appends a char value to the string builder. * * @param ch the value to append * @return this, to enable chaining * @since 3.0 */ @Override public StrBuilder append(final char ch) { final int len = length(); ensureCapacity(len + 1); buffer[size++] = ch; return this; }
private void doTestNoReplace(final String replaceTemplate) { final StrSubstitutor sub = new StrSubstitutor(values); if (replaceTemplate == null) { assertNull(sub.replace((String) null)); assertNull(sub.replace((String) null, 0, 100)); assertNull(sub.replace((char[]) null)); assertNull(sub.replace((char[]) null, 0, 100)); assertNull(sub.replace((StringBuffer) null)); assertNull(sub.replace((StringBuffer) null, 0, 100)); assertNull(sub.replace((StrBuilder) null)); assertNull(sub.replace((StrBuilder) null, 0, 100)); assertNull(sub.replace((Object) null)); assertFalse(sub.replaceIn((StringBuffer) null)); assertFalse(sub.replaceIn((StringBuffer) null, 0, 100)); assertFalse(sub.replaceIn((StrBuilder) null)); assertFalse(sub.replaceIn((StrBuilder) null, 0, 100)); } else { assertEquals(replaceTemplate, sub.replace(replaceTemplate)); final StrBuilder bld = new StrBuilder(replaceTemplate); assertFalse(sub.replaceIn(bld)); assertEquals(replaceTemplate, bld.toString()); } }
@Test public void testAppendSeparator_char() { final StrBuilder sb = new StrBuilder(); sb.appendSeparator(','); // no effect assertEquals("", sb.toString()); sb.append("foo"); assertEquals("foo", sb.toString()); sb.appendSeparator(','); assertEquals("foo,", sb.toString()); } @Test
@Test public void testAppendWithSeparatorsWithNullText() { final StrBuilder sb = new StrBuilder(); sb.setNullText("null"); sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); assertEquals("foo,null,baz", sb.toString()); sb.clear(); sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); assertEquals("foo,null,baz", sb.toString()); }
@Test public void testInsertWithNullText() { final StrBuilder sb = new StrBuilder(); sb.setNullText("null"); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); sb.insert(-1, FOO); fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { sb.insert(7, FOO); fail("insert(7, Object) expected StringIndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { sb.insert(0, (Object) null); assertEquals("nullbarbaz", sb.toString()); sb.insert(0, FOO); assertEquals("foonullbarbaz", sb.toString()); sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); sb.insert(-1, "foo"); fail("insert(-1, String) expected StringIndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { sb.insert(7, "foo");
@Test public void testAppendFixedWidthPadRight_int() { final StrBuilder sb = new StrBuilder(); sb.appendFixedWidthPadRight(123, -1, '-'); assertEquals("", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 0, '-'); assertEquals("", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 1, '-'); assertEquals("1", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 2, '-'); assertEquals("12", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 3, '-'); assertEquals("123", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 4, '-'); assertEquals("123-", sb.toString()); sb.clear(); sb.appendFixedWidthPadRight(123, 10, '-'); assertEquals(10, sb.length()); // 1234567890 assertEquals("123-------", sb.toString()); }
@Test public void testAppend_PrimitiveNumber() { final StrBuilder sb = new StrBuilder(); sb.append(0); assertEquals("0", sb.toString()); sb.append(1L); assertEquals("01", sb.toString()); sb.append(2.3f); assertEquals("012.3", sb.toString()); sb.append(4.5d); assertEquals("012.34.5", sb.toString()); }
@Test public void testAppendPadding() { final StrBuilder sb = new StrBuilder(); sb.append("foo"); assertEquals("foo", sb.toString()); sb.appendPadding(-1, '-'); assertEquals("foo", sb.toString()); sb.appendPadding(0, '-'); assertEquals("foo", sb.toString()); sb.appendPadding(1, '-'); assertEquals("foo-", sb.toString()); sb.appendPadding(16, '-'); assertEquals(20, sb.length()); // 12345678901234567890 assertEquals("foo-----------------", sb.toString()); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replace(null, "x", 0, sb.length(), -1); assertEquals("abcbccba", sb.toString()); sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1); assertEquals("xbcbccbx", sb.toString()); sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1); assertEquals("xbxcxx", sb.toString()); sb = new StrBuilder("A1-A2A3-A4"); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("***-******-***", sb.toString()); sb = new StrBuilder(); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("", sb.toString()); }
/** * Tests interpolation with system properties. */ @Test public void testStaticReplaceSystemProperties() { final StrBuilder buf = new StrBuilder(); buf.append("Hi ").append(System.getProperty("user.name")); buf.append(", you are working with "); buf.append(System.getProperty("os.name")); buf.append(", your home directory is "); buf.append(System.getProperty("user.home")).append('.'); assertEquals(buf.toString(), StrSubstitutor.replaceSystemProperties("Hi ${user.name}, you are " + "working with ${os.name}, your home " + "directory is ${user.home}.")); }
@Test public void testAppend_CharArray() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((char[]) null); assertEquals("NULL", sb.toString()); sb = new StrBuilder(); sb.append(new char[0]); assertEquals("", sb.toString()); sb.append(new char[]{'f', 'o', 'o'}); assertEquals("foo", sb.toString()); }
@Test public void testLang299() { final StrBuilder sb = new StrBuilder(1); sb.appendFixedWidthPadRight("foo", 1, '-'); assertEquals("f", sb.toString()); }
@Test public void testClear() { final StrBuilder sb = new StrBuilder(); sb.append("Hello"); sb.clear(); assertEquals(0, sb.length()); assertTrue(sb.buffer.length >= 5); }
@Override public String render(int width) { C renderCtx = this.getNewContext(); renderCtx.copySettings(this.ctx); renderCtx.inheritSettings(this.ctx); renderCtx.setWidth(renderCtx.getTextWidth(width + renderCtx.getCalculatedItemString().length())); return new StrBuilder().appendWithSeparators(this.renderer.renderAsCollection(this.getRawContent(), renderCtx), "\n").toString(); }
@Test public void testAppendNewLine() { StrBuilder sb = new StrBuilder("---"); sb.appendNewLine().append("+++"); assertEquals("---" + SEP + "+++", sb.toString()); sb = new StrBuilder("---"); sb.setNewLineText("#").appendNewLine().setNewLineText(null).appendNewLine(); assertEquals("---#" + SEP, sb.toString()); }