Merge branch 'master' into JAVA-13136
This commit is contained in:
commit
d7c38b05d6
|
@ -8,6 +8,8 @@ import java.util.List;
|
|||
|
||||
import org.apache.spark.sql.Dataset;
|
||||
import org.apache.spark.sql.Row;
|
||||
import org.apache.spark.sql.SparkSession;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CustomerToDataFrameConverterAppUnitTest {
|
||||
|
@ -59,4 +61,9 @@ class CustomerToDataFrameConverterAppUnitTest {
|
|||
assertEquals( "Male", row2.getAs("gender"));
|
||||
assertEquals( 1200, (int)row2.getAs("transaction_amount"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
SparkSession.getActiveSession().get().close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,3 +11,4 @@ This module contains articles about Java 14.
|
|||
- [Foreign Memory Access API in Java 14](https://www.baeldung.com/java-foreign-memory-access)
|
||||
- [Java 14 Record Keyword](https://www.baeldung.com/java-record-keyword)
|
||||
- [New Features in Java 14](https://www.baeldung.com/java-14-new-features)
|
||||
- [Java 14 Record vs. Lombok](https://www.baeldung.com/java-record-vs-lombok)
|
||||
|
|
|
@ -14,6 +14,15 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ColorData {
|
||||
|
||||
private int red;
|
||||
private int green;
|
||||
private int blue;
|
||||
|
||||
public String getHexString() {
|
||||
return String.format("#%02X%02X%02X", red, green, blue);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
public record ColorRecord(int red, int green, int blue) {
|
||||
|
||||
public String getHexString() {
|
||||
return String.format("#%02X%02X%02X", red, green, blue);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@Getter(AccessLevel.NONE)
|
||||
public class ColorValueObject {
|
||||
int red;
|
||||
int green;
|
||||
int blue;
|
||||
|
||||
public String getHexString() {
|
||||
return String.format("#%02X%02X%02X", red, green, blue);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
public class MonochromeColor extends ColorData {
|
||||
|
||||
public MonochromeColor(int grayScale) {
|
||||
super(grayScale, grayScale, grayScale);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class StudentBuilder {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Long studentId;
|
||||
private String email;
|
||||
private String phoneNumber;
|
||||
private String address;
|
||||
private String country;
|
||||
private int age;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
public record StudentRecord(String firstName, String lastName, Long studentId, String email, String phoneNumber, String address, String country, int age) {
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.java14.recordvslombok;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RecordVsLombokUntTest {
|
||||
|
||||
@Test
|
||||
public void givenAColorRecord_hexStringIsCorrect() {
|
||||
var red = new ColorRecord(255, 0, 0);
|
||||
|
||||
assertThat(red.getHexString()).isEqualTo("#FF0000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAColorValueObject_hexStringIsCorrect() {
|
||||
var red = new ColorValueObject(255, 0, 0);
|
||||
|
||||
assertThat(red.getHexString()).isEqualTo("#FF0000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithManyAttributes_firstNameShouldBeJohn() {
|
||||
StudentRecord john = new StudentRecord("John", "Doe", null, "john@doe.com", null, null, "England", 20);
|
||||
|
||||
assertThat(john.firstName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithManyAttributes_firstNameShouldBeJohn() {
|
||||
StudentBuilder john = StudentBuilder.builder()
|
||||
.firstName("John")
|
||||
.lastName("Doe")
|
||||
.email("john@doe.com")
|
||||
.country("England")
|
||||
.age(20)
|
||||
.build();
|
||||
|
||||
assertThat(john.getFirstName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
}
|
|
@ -5,4 +5,3 @@ This module contains articles about Java 15.
|
|||
### Relevant articles
|
||||
|
||||
- [Hidden Classes in Java 15](https://www.baeldung.com/java-hidden-classes)
|
||||
- [Sealed Classes and Interfaces in Java 15](https://www.baeldung.com/java-sealed-classes-interfaces)
|
||||
|
|
|
@ -4,15 +4,15 @@ import java.util.IntSummaryStatistics;
|
|||
|
||||
|
||||
public class BlogPost {
|
||||
|
||||
|
||||
private String title;
|
||||
private String author;
|
||||
private BlogPostType type;
|
||||
private int likes;
|
||||
record AuthPostTypesLikes(String author, BlogPostType type, int likes) {};
|
||||
record PostcountTitlesLikesStats(long postCount, String titles, IntSummaryStatistics likesStats){};
|
||||
record PostCountTitlesLikesStats(long postCount, String titles, IntSummaryStatistics likesStats){};
|
||||
record TitlesBoundedSumOfLikes(String titles, int boundedSumOfLikes) {};
|
||||
|
||||
|
||||
public BlogPost(String title, String author, BlogPostType type, int likes) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
|
|
|
@ -257,7 +257,7 @@ public class JavaGroupingByCollectorUnitTest {
|
|||
@Test
|
||||
public void givenListOfPosts_whenGroupedByAuthor_thenGetAMapUsingCollectingAndThen() {
|
||||
|
||||
Map<String, BlogPost.PostcountTitlesLikesStats> postsPerAuthor = posts.stream()
|
||||
Map<String, BlogPost.PostCountTitlesLikesStats> postsPerAuthor = posts.stream()
|
||||
.collect(groupingBy(BlogPost::getAuthor, collectingAndThen(toList(), list -> {
|
||||
long count = list.stream()
|
||||
.map(BlogPost::getTitle)
|
||||
|
@ -267,10 +267,10 @@ public class JavaGroupingByCollectorUnitTest {
|
|||
.collect(joining(" : "));
|
||||
IntSummaryStatistics summary = list.stream()
|
||||
.collect(summarizingInt(BlogPost::getLikes));
|
||||
return new BlogPost.PostcountTitlesLikesStats(count, titles, summary);
|
||||
return new BlogPost.PostCountTitlesLikesStats(count, titles, summary);
|
||||
})));
|
||||
|
||||
BlogPost.PostcountTitlesLikesStats result = postsPerAuthor.get("Author 1");
|
||||
BlogPost.PostCountTitlesLikesStats result = postsPerAuthor.get("Author 1");
|
||||
assertThat(result.postCount()).isEqualTo(3L);
|
||||
assertThat(result.titles()).isEqualTo("News item 1 : Programming guide : Tech review 2");
|
||||
assertThat(result.likesStats().getMax()).isEqualTo(20);
|
||||
|
@ -294,7 +294,7 @@ public class JavaGroupingByCollectorUnitTest {
|
|||
.toUpperCase(),
|
||||
u1.boundedSumOfLikes() + likes);
|
||||
}));
|
||||
|
||||
|
||||
BlogPost.TitlesBoundedSumOfLikes result = postsPerAuthor.get("Author 1");
|
||||
assertThat(result.titles()).isEqualTo("NEWS ITEM 1 : PROGRAMMING GUIDE : TECH REVIEW 2");
|
||||
assertThat(result.boundedSumOfLikes()).isEqualTo(47);
|
||||
|
|
|
@ -5,3 +5,4 @@
|
|||
- [Introduction to HexFormat in Java 17](https://www.baeldung.com/java-hexformat)
|
||||
- [New Features in Java 17](https://www.baeldung.com/java-17-new-features)
|
||||
- [Random Number Generators in Java 17](https://www.baeldung.com/java-17-random-number-generators)
|
||||
- [Sealed Classes and Interfaces in Java 17](https://www.baeldung.com/java-sealed-classes-interfaces)
|
||||
|
|
|
@ -21,7 +21,7 @@ public class VehicleUnitTest {
|
|||
public void givenCar_whenUsingReflectionAPI_thenSuperClassIsSealed() {
|
||||
Assertions.assertThat(car.getClass().isSealed()).isEqualTo(false);
|
||||
Assertions.assertThat(car.getClass().getSuperclass().isSealed()).isEqualTo(true);
|
||||
Assertions.assertThat(car.getClass().getSuperclass().permittedSubclasses())
|
||||
Assertions.assertThat(car.getClass().getSuperclass().getPermittedSubclasses())
|
||||
.contains(ClassDesc.of(car.getClass().getCanonicalName()));
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ public class VehicleUnitTest {
|
|||
public void givenTruck_whenUsingReflectionAPI_thenSuperClassIsSealed() {
|
||||
Assertions.assertThat(truck.getClass().isSealed()).isEqualTo(false);
|
||||
Assertions.assertThat(truck.getClass().getSuperclass().isSealed()).isEqualTo(true);
|
||||
Assertions.assertThat(truck.getClass().getSuperclass().permittedSubclasses())
|
||||
Assertions.assertThat(truck.getClass().getSuperclass().getPermittedSubclasses())
|
||||
.contains(ClassDesc.of(truck.getClass().getCanonicalName()));
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ public class InputStreamToOutputStreamUnitTest {
|
|||
void copy(InputStream source, OutputStream target) throws IOException {
|
||||
byte[] buf = new byte[8192];
|
||||
int length;
|
||||
while ((length = source.read(buf)) > 0) {
|
||||
while ((length = source.read(buf)) != -1) {
|
||||
target.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,3 +10,5 @@ This module contains articles about JAR files
|
|||
- [Get Names of Classes Inside a JAR File](https://www.baeldung.com/jar-file-get-class-names)
|
||||
- [Find All Jars Containing Given Class](https://baeldung.com/find-all-jars-containing-given-class/)
|
||||
- [Creating JAR Files Programmatically](https://www.baeldung.com/jar-create-programatically)
|
||||
- [Guide to Creating Jar Executables and Windows Executables from Java](https://www.baeldung.com/jar-windows-executables)
|
||||
- [Get the Full Path of a JAR File From a Class](https://www.baeldung.com/java-full-path-of-jar-from-class)
|
||||
|
|
|
@ -54,6 +54,12 @@
|
|||
<artifactId>moneta</artifactId>
|
||||
<version>${javamoney.moneta.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -266,6 +272,7 @@
|
|||
<!-- util -->
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<mockito.version>4.6.1</mockito.version>
|
||||
<!-- maven plugins -->
|
||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
|
@ -279,4 +286,4 @@
|
|||
<target.version>1.8</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.jar;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.awt.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class MySampleGUIAppn extends JFrame {
|
||||
public MySampleGUIAppn() {
|
||||
if (!GraphicsEnvironment.isHeadless()) {
|
||||
setSize(300,300);
|
||||
setTitle("MySampleGUIAppn");
|
||||
Button b = new Button("Click Me!");
|
||||
b.setBounds(30,100,80,30);
|
||||
add(b);
|
||||
setVisible(true);
|
||||
addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
dispose();
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
MySampleGUIAppn app=new MySampleGUIAppn();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.jarfile;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class JarFilePathResolver {
|
||||
|
||||
public String getJarFilePath(Class clazz) {
|
||||
try {
|
||||
return byGetProtectionDomain(clazz);
|
||||
} catch (Exception e) {
|
||||
// cannot get jar file path using byGetProtectionDomain
|
||||
// Exception handling omitted
|
||||
}
|
||||
return byGetResource(clazz);
|
||||
}
|
||||
|
||||
String byGetProtectionDomain(Class clazz) throws URISyntaxException {
|
||||
URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
|
||||
return Paths.get(url.toURI()).toString();
|
||||
}
|
||||
|
||||
String byGetResource(Class clazz) {
|
||||
final URL classResource = clazz.getResource(clazz.getSimpleName() + ".class");
|
||||
if (classResource == null) {
|
||||
throw new RuntimeException("class resource is null");
|
||||
}
|
||||
|
||||
final String url = classResource.toString();
|
||||
if (url.startsWith("jar:file:")) {
|
||||
// extract 'file:......jarName.jar' part from the url string
|
||||
String path = url.replaceAll("^jar:(file:.*[.]jar)!/.*", "$1");
|
||||
try {
|
||||
return Paths.get(new URL(path).toURI()).toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Invalid Jar File URL String");
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Invalid Jar File URL String");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.jar;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MySampleGUIAppnUnitTest {
|
||||
@Test
|
||||
void testMain() throws IOException {
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
String [] args = null;
|
||||
System.exit(0);
|
||||
MySampleGUIAppn.main(args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.jarfile;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.common.base.Ascii;
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class JarFilePathResolverUnitTest {
|
||||
@Spy
|
||||
JarFilePathResolver jarFilePathResolver;
|
||||
|
||||
@Test
|
||||
void givenClassObjectWhenCallingByGetProtectionDomainShouldGetExpectedPath() throws Exception {
|
||||
String jarPath = jarFilePathResolver.byGetProtectionDomain(Ascii.class);
|
||||
assertThat(jarPath).endsWith(".jar").contains("guava");
|
||||
assertThat(new File(jarPath)).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenClassObjectWhenCallingByGetResourceShouldGetExpectedPath() {
|
||||
String jarPath = jarFilePathResolver.byGetResource(Ascii.class);
|
||||
assertThat(jarPath).endsWith(".jar").contains("guava");
|
||||
assertThat(new File(jarPath)).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenClassObjectWhenNoSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException {
|
||||
String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class);
|
||||
assertThat(jarPath).endsWith(".jar").contains("guava");
|
||||
assertThat(new File(jarPath)).exists();
|
||||
verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class);
|
||||
verify(jarFilePathResolver, never()).byGetResource(Ascii.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenClassObjectWhenSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException {
|
||||
when(jarFilePathResolver.byGetProtectionDomain(Ascii.class)).thenThrow(new SecurityException("not allowed"));
|
||||
String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class);
|
||||
assertThat(jarPath).endsWith(".jar").contains("guava");
|
||||
assertThat(new File(jarPath)).exists();
|
||||
verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class);
|
||||
verify(jarFilePathResolver, times(1)).byGetResource(Ascii.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,3 +5,4 @@ This module contains articles about core features in the Java language
|
|||
### Relevant Articles:
|
||||
|
||||
- [Difference Between == and equals() in Java](https://www.baeldung.com/java-equals-method-operator-difference)
|
||||
- [Advantages and Disadvantages of Using Java Wildcard Imports](https://www.baeldung.com/java-wildcard-imports)
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-5</finalName>
|
||||
<resources>
|
||||
|
@ -23,5 +22,4 @@
|
|||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.imports.specific;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Book {
|
||||
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Date datePublished;
|
||||
|
||||
private List<String> authors;
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.imports.wildcard;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Book {
|
||||
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Date datePublished;
|
||||
|
||||
private List<String> authors;
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.imports.wildcard;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
public class BookView extends Frame {
|
||||
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Date datePublished;
|
||||
|
||||
private List<String> authors;
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.imports.wildcard;
|
||||
|
||||
import java.util.*;
|
||||
import java.sql.*;
|
||||
import java.sql.Date;
|
||||
|
||||
public class Library {
|
||||
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Time openingTime;
|
||||
|
||||
private Time closingTime;
|
||||
|
||||
private List<Date> datesClosed;
|
||||
|
||||
}
|
|
@ -9,7 +9,6 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
|
|
@ -11,3 +11,4 @@ This module contains articles about string APIs.
|
|||
- [StringBuilder vs StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
||||
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
|
||||
- [Getting a Character by Index From a String in Java](https://www.baeldung.com/java-character-at-position)
|
||||
- [Clearing a StringBuilder or StringBuffer](https://www.baeldung.com/java-clear-stringbuilder-stringbuffer)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -7,4 +7,4 @@
|
|||
- [String equals() Vs contentEquals() in Java](https://www.baeldung.com/java-string-equals-vs-contentequals)
|
||||
- [Check if a String Ends with a Certain Pattern in Java](https://www.baeldung.com/java-string-ends-pattern)
|
||||
- [Check if a Character is a Vowel in Java](https://www.baeldung.com/java-check-character-vowel)
|
||||
|
||||
- [How to Truncate a String in Java](https://www.baeldung.com/java-truncating-strings)
|
||||
|
|
|
@ -96,7 +96,23 @@ public class Java8EncodeDecodeUnitTest {
|
|||
assertNotNull(decodedMime);
|
||||
}
|
||||
|
||||
//
|
||||
@Test
|
||||
public void whenEncodedStringHasValidCharacters_thenStringCanBeDecoded() {
|
||||
final String encodedString = "dGVzdCMkaW5wdXQ+";
|
||||
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
|
||||
final String decodedString = new String(decodedBytes);
|
||||
|
||||
assertNotNull(decodedString);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenEncodedStringHasInvalidCharacters_thenIllegalArgumentException() {
|
||||
final String encodedString = "dGVzdCMkaW5wdXQ#";
|
||||
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
|
||||
final String decodedString = new String(decodedBytes);
|
||||
|
||||
assertNotNull(decodedString);
|
||||
}
|
||||
|
||||
private static StringBuilder getMimeBuffer() {
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
FROM openjdk:11
|
||||
MAINTAINER baeldung.com
|
||||
COPY target/docker-java-jar-0.0.1-SNAPSHOT.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","/app.jar"]
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>docker-java-jar</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.HelloWorld</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung;
|
||||
|
||||
public class HelloWorld {
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println("Welcome to our application");
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
<module>docker-images</module>
|
||||
<module>docker-spring-boot</module>
|
||||
<module>docker-spring-boot-postgres</module>
|
||||
<module>docker-java-jar</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Gradle Modules
|
||||
|
||||
This module contains submodules of Gradle.
|
|
@ -1,4 +1,4 @@
|
|||
rootProject.name='gradle-5-articles'
|
||||
rootProject.name='gradle-5'
|
||||
include 'java-exec'
|
||||
include 'unused-dependencies'
|
||||
include 'source-sets'
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue