Skip to content

Commit

Permalink
BAEL-5610 Code for the clearing stringbuilder or stringbuffer article (
Browse files Browse the repository at this point in the history
…#12378)

Co-authored-by: thibault.faure <[email protected]>
  • Loading branch information
thibaultfaure and thibault.faure authored Jul 10, 2022
1 parent 7e44426 commit 91878d9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.*;

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

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

}

0 comments on commit 91878d9

Please sign in to comment.