Merge branch 'master' into JAVA-13136

This commit is contained in:
freelansam
2022-07-15 10:01:48 +05:30
committed by GitHub
366 changed files with 1405 additions and 541 deletions
@@ -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();
}
}
+1
View File
@@ -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)
+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)
@@ -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);
+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()));
}
@@ -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)
+8 -1
View File
@@ -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);
}
}

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