LANG-836 StrSubstitutor does not support StringBuilder or CharSequence

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1487918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2013-05-30 16:34:20 +00:00
parent ae987e1770
commit 22c37d5307
3 changed files with 100 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
<action issue="LANG-836" type="fix" due-to="Arnaud Brunet">StrSubstitutor does not support StringBuilder or CharSequence</action>
<action issue="LANG-693" type="fix" due-to="Calvin Echols">Method createNumber from NumberUtils doesn't work for floating point numbers other than Float</action>
<action issue="LANG-887" type="fix">FastDateFormat does not use the locale specific cache correctly</action>
<action issue="LANG-884" type="update">Simplify FastDateFormat; eliminate boxing</action>

View File

@ -410,6 +410,45 @@ public String replace(final StringBuffer source, final int offset, final int len
substitute(buf, 0, length);
return buf.toString();
}
/**
* Replaces all the occurrences of variables with their matching values
* from the resolver using the given source as a template.
* The source is not altered by this method.
*
* @param source the buffer to use as a template, not changed, null returns null
* @return the result of the replace operation
* @since TODO
*/
public String replace(CharSequence source) {
if (source == null) {
return null;
}
return replace(source, 0, source.length());
}
/**
* Replaces all the occurrences of variables with their matching values
* from the resolver using the given source as a template.
* The source is not altered by this method.
* <p>
* Only the specified portion of the buffer will be processed.
* The rest of the buffer is not processed, and is not returned.
*
* @param source the buffer to use as a template, not changed, null returns null
* @param offset the start offset within the array, must be valid
* @param length the length within the array to be processed, must be valid
* @return the result of the replace operation
* @since TODO
*/
public String replace(CharSequence source, int offset, int length) {
if (source == null) {
return null;
}
StrBuilder buf = new StrBuilder(length).append(source, offset, length);
substitute(buf, 0, length);
return buf.toString();
}
//-----------------------------------------------------------------------
/**
@ -510,6 +549,49 @@ public boolean replaceIn(final StringBuffer source, final int offset, final int
return true;
}
//-----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables within the given source buffer
* with their matching values from the resolver.
* The buffer is updated with the result.
*
* @param source the buffer to replace in, updated, null returns zero
* @return true if altered
* @since TODO
*/
public boolean replaceIn(StringBuilder source) {
if (source == null) {
return false;
}
return replaceIn(source, 0, source.length());
}
/**
* Replaces all the occurrences of variables within the given source builder
* with their matching values from the resolver.
* The builder is updated with the result.
* <p>
* Only the specified portion of the buffer will be processed.
* The rest of the buffer is not processed, but it is not deleted.
*
* @param source the buffer to replace in, updated, null returns zero
* @param offset the start offset within the array, must be valid
* @param length the length within the buffer to be processed, must be valid
* @return true if altered
* @since TODO
*/
public boolean replaceIn(StringBuilder source, int offset, int length) {
if (source == null) {
return false;
}
StrBuilder buf = new StrBuilder(length).append(source, offset, length);
if (substitute(buf, 0, length) == false) {
return false;
}
source.replace(offset, offset + length, buf.toString());
return true;
}
//-----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables within the given source

View File

@ -547,6 +547,13 @@ private void doTestReplace(final String expectedResult, final String replaceTemp
assertEquals(expectedShortResult, sub.replace(buf, 1, buf.length() - 2));
}
// replace using StringBuilder
StringBuilder builder = new StringBuilder(replaceTemplate);
assertEquals(expectedResult, sub.replace(builder));
if (substring) {
assertEquals(expectedShortResult, sub.replace(builder, 1, builder.length() - 2));
}
// replace using StrBuilder
StrBuilder bld = new StrBuilder(replaceTemplate);
assertEquals(expectedResult, sub.replace(bld));
@ -568,6 +575,16 @@ private void doTestReplace(final String expectedResult, final String replaceTemp
assertEquals(expectedResult, buf.toString()); // expect full result as remainder is untouched
}
// replace in StringBuilder
builder = new StringBuilder(replaceTemplate);
assertTrue(sub.replaceIn(builder));
assertEquals(expectedResult, builder.toString());
if (substring) {
builder = new StringBuilder(replaceTemplate);
assertTrue(sub.replaceIn(builder, 1, builder.length() - 2));
assertEquals(expectedResult, builder.toString()); // expect full result as remainder is untouched
}
// replace in StrBuilder
bld = new StrBuilder(replaceTemplate);
assertTrue(sub.replaceIn(bld));