Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima
2022-07-09 20:18:44 -03:00
committed by GitHub
97 changed files with 1465 additions and 264 deletions
+9
View File
@@ -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, "[email protected]", null, null, "England", 20);
assertThat(john.firstName()).isEqualTo("John");
}
@Test
public void givenBuilderWithManyAttributes_firstNameShouldBeJohn() {
StudentBuilder john = StudentBuilder.builder()
.firstName("John")
.lastName("Doe")
.email("[email protected]")
.country("England")
.age(20)
.build();
assertThat(john.getFirstName()).isEqualTo("John");
}
}
-1
View File
@@ -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)
+1
View File
@@ -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()));
}
@@ -3,4 +3,5 @@
## Core Java 8 Cookbooks and Examples
### Relevant Articles:
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
- [Use Cases for Static Methods in Java](https://www.baeldung.com/java-static-methods-use-cases)
@@ -9,4 +9,5 @@ This module contains articles about core Java input/output(IO) conversions.
- [Converting a BufferedReader to a JSONObject](https://www.baeldung.com/java-bufferedreader-to-jsonobject)
- [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array)
- [How to Write to a CSV File in Java](https://www.baeldung.com/java-csv)
- [How to Convert InputStream to Base64 String](https://www.baeldung.com/java-inputstream-to-base64-string)
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)
@@ -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,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);
}
}
@@ -6,3 +6,4 @@ This module contains articles about types in Java
- [Convert an Array of Primitives to an Array of Objects](https://www.baeldung.com/java-primitive-array-to-object-array)
- [Check if an Enum Value Exists in Java](https://www.baeldung.com/java-search-enum-values)
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
@@ -7,7 +7,6 @@
- [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers)
- [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size)
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
- [Check if BigDecimal Value Is Zero](https://www.baeldung.com/java-bigdecimal-zero)
- More articles: [[<-- prev]](../core-java-numbers-3) [[next -->]](../core-java-numbers-5)
@@ -1,3 +1,4 @@
## Relevant Articles:
- [Count Occurrences Using Java groupingBy Collector](https://www.baeldung.com/java-groupingby-count)
- [How to Split a Stream into Multiple Streams](https://www.baeldung.com/java-split-stream)
@@ -0,0 +1,105 @@
package com.baeldung.truncate;
import java.util.Optional;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Splitter;
public class TruncateString {
private static final String EMPTY = "";
public static String usingSubstringMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
if (text.length() <= length) {
return text;
} else {
return text.substring(0, length);
}
}
public static String usingSplitMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
String[] results = text.split("(?<=\\G.{" + length + "})");
return results[0];
}
public static String usingPattern(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
Optional<String> result = Pattern.compile(".{1," + length + "}")
.matcher(text)
.results()
.map(MatchResult::group)
.findFirst();
return result.isPresent() ? result.get() : EMPTY;
}
public static String usingCodePointsMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
return text.codePoints()
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
public static String usingLeftMethod(String text, int length) {
return StringUtils.left(text, length);
}
public static String usingTruncateMethod(String text, int length) {
return StringUtils.truncate(text, length);
}
public static String usingSplitter(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
Iterable<String> parts = Splitter.fixedLength(length)
.split(text);
return parts.iterator()
.next();
}
}
@@ -0,0 +1,53 @@
package com.baeldung.truncate;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class TruncateStringUnitTest {
private static final String TEXT = "Welcome to baeldung.com";
@Test
public void givenStringAndLength_whenUsingSubstringMethod_thenTruncate() {
assertEquals(TruncateString.usingSubstringMethod(TEXT, 10), "Welcome to");
}
@Test
public void givenStringAndLength_whenUsingSplitMethod_thenTruncate() {
assertEquals(TruncateString.usingSplitMethod(TEXT, 13), "Welcome to ba");
}
@Test
public void givenStringAndLength_whenUsingPattern_thenTruncate() {
assertEquals(TruncateString.usingPattern(TEXT, 19), "Welcome to baeldung");
}
@Test
public void givenStringAndLength_whenUsingCodePointsMethod_thenTruncate() {
assertEquals(TruncateString.usingCodePointsMethod(TEXT, 6), "Welcom");
}
@Test
public void givenStringAndLength_whenUsingLeftMethod_thenTruncate() {
assertEquals(TruncateString.usingLeftMethod(TEXT, 15), "Welcome to bael");
}
@Test
public void givenStringAndLength_whenUsingTruncateMethod_thenTruncate() {
assertEquals(TruncateString.usingTruncateMethod(TEXT, 20), "Welcome to baeldung.");
}
@Test
public void givenStringAndLength_whenUsingSplitter_thenTruncate() {
assertEquals(TruncateString.usingSplitter(TEXT, 3), "Wel");
}
}
@@ -0,0 +1,3 @@
## Relevant Articles:
- [How to Pass Environment Variable Value into Dockerfile](https://www.baeldung.com/ops/dockerfile-env-variable)

Some files were not shown because too many files have changed in this diff Show More