-
Notifications
You must be signed in to change notification settings - Fork 54.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5610 Code for the clearing stringbuilder or stringbuffer article (…
…#12378) Co-authored-by: thibault.faure <[email protected]>
- Loading branch information
1 parent
7e44426
commit 91878d9
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ing-apis/src/main/java/com/baeldung/clearstringbuilderorstringbuffer/BenchmarkRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.baeldung.clearstringbuilderorstringbuffer; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class BenchmarkRunner { | ||
|
||
public static void main(String[] args) throws Exception { | ||
org.openjdk.jmh.Main.main(args); | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
public static class MyState { | ||
final String HELLO = "Hello World"; | ||
final StringBuilder sb = new StringBuilder().append(HELLO); | ||
} | ||
|
||
@Benchmark | ||
public void evaluateSetLength(Blackhole blackhole, MyState state) { | ||
state.sb.setLength(0); | ||
blackhole.consume(state.sb.toString()); | ||
} | ||
|
||
@Benchmark | ||
public void evaluateDelete(Blackhole blackhole, MyState state) { | ||
state.sb.delete(0, state.sb.length()); | ||
blackhole.consume(state.sb.toString()); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...m/baeldung/clearstringbuilderorstringbuffer/ClearStringBuilderOrStringBufferUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.baeldung.clearstringbuilderorstringbuffer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ClearStringBuilderOrStringBufferUnitTest { | ||
|
||
@Test | ||
void whenSetLengthToZero_ThenStringBuilderIsCleared() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("Hello World"); | ||
int initialCapacity = stringBuilder.capacity(); | ||
stringBuilder.setLength(0); | ||
assertEquals("", stringBuilder.toString()); | ||
assertEquals(initialCapacity, stringBuilder.capacity()); | ||
} | ||
|
||
@Test | ||
void whenDeleteAll_ThenStringBuilderIsCleared() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("Hello World"); | ||
int initialCapacity = stringBuilder.capacity(); | ||
stringBuilder.delete(0, stringBuilder.length()); | ||
assertEquals("", stringBuilder.toString()); | ||
assertEquals(initialCapacity, stringBuilder.capacity()); | ||
} | ||
|
||
@Test | ||
void whenSetLengthToZero_ThenStringBufferIsCleared() { | ||
StringBuffer stringBuffer = new StringBuffer(); | ||
stringBuffer.append("Hello World"); | ||
int initialCapacity = stringBuffer.capacity(); | ||
stringBuffer.setLength(0); | ||
assertEquals("", stringBuffer.toString()); | ||
assertEquals(initialCapacity, stringBuffer.capacity()); | ||
} | ||
|
||
@Test | ||
void whenDeleteAll_ThenStringBufferIsCleared() { | ||
StringBuffer stringBuffer = new StringBuffer(); | ||
stringBuffer.append("Hello World"); | ||
int initialCapacity = stringBuffer.capacity(); | ||
stringBuffer.delete(0, stringBuffer.length()); | ||
assertEquals("", stringBuffer.toString()); | ||
assertEquals(initialCapacity, stringBuffer.capacity()); | ||
} | ||
|
||
// Note: It did not make the cut to the article, but here is another way to reset a StringBuilder | ||
@Test | ||
void whenAssignedToNewStringBuilder_ThenStringBuilderIsCleared() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("Hello World"); | ||
stringBuilder = new StringBuilder(); | ||
assertEquals("", stringBuilder.toString()); | ||
} | ||
|
||
@Test | ||
void whenAssignedToNewStringBuffer_ThenStringBufferIsCleared() { | ||
StringBuffer stringBuffer = new StringBuffer(); | ||
stringBuffer.append("Hello World"); | ||
stringBuffer = new StringBuffer(); | ||
assertEquals("", stringBuffer.toString()); | ||
} | ||
|
||
} |