BAEL-5610 Code for the clearing stringbuilder or stringbuffer article (#12378)

Co-authored-by: thibault.faure <thibault.faure@mimacom.com>
This commit is contained in:
thibaultfaure 2022-07-10 14:22:08 +02:00 committed by GitHub
parent 58c7385029
commit 4c1e18b15e
3 changed files with 98 additions and 1 deletions

View File

@ -9,7 +9,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*; import static org.junit.Assert.*;

View File

@ -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());
}
}

View File

@ -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());
}
}