Merge branch 'master' into readme-description-edits
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-13</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-13</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<release>13</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M3</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>13</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>13</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.switchExpression;
|
||||
|
||||
import static java.time.Month.AUGUST;
|
||||
import static java.time.Month.JUNE;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.Month;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SwitchExpressionsUnitTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJune_thenWillReturn3() {
|
||||
|
||||
var month = JUNE;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL -> 2;
|
||||
default -> 0;
|
||||
};
|
||||
|
||||
assertEquals(result, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn24() {
|
||||
var month = AUGUST;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL, AUGUST -> {
|
||||
int monthLength = month.toString().length();
|
||||
yield monthLength * 4;
|
||||
}
|
||||
default -> 0;
|
||||
};
|
||||
|
||||
assertEquals(24, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJanuary_thenWillReturn3() {
|
||||
|
||||
Function<Month, Integer> func = (month) -> {
|
||||
switch (month) {
|
||||
case JANUARY, JUNE, JULY -> { return 3; }
|
||||
default -> { return 0; }
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals(Integer.valueOf(3), func.apply(Month.JANUARY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn2() {
|
||||
var month = AUGUST;
|
||||
|
||||
var result = switch (month) {
|
||||
case JANUARY, JUNE, JULY -> 3;
|
||||
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
|
||||
case MARCH, MAY, APRIL, AUGUST -> 2;
|
||||
};
|
||||
|
||||
assertEquals(result, 2);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,6 @@ This module contains articles about advanced topics about multithreading with co
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Common Concurrency Pitfalls in Java](https://www.baeldung.com/java-common-concurrency-pitfalls)
|
||||
- [Guide to RejectedExecutionHandler](https://www.baeldung.com/java-rejectedexecutionhandler)
|
||||
[[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
## Core Date Operations
|
||||
This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Get the Current Date Prior to Java 8](https://www.baeldung.com/java-get-the-current-date-legacy)
|
||||
- [Skipping Weekends While Adding Days to LocalDate in Java 8](https://www.baeldung.com/java-localdate-add-days-skip-weekends)
|
||||
- [Checking If Two Java Dates Are on the Same Day](https://www.baeldung.com/java-check-two-dates-on-same-day)
|
||||
- [Converting Java Date to OffsetDateTime](https://www.baeldung.com/java-convert-date-to-offsetdatetime)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-date-operations</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-date-operations</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.offsetdatetime;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Date;
|
||||
|
||||
public class ConvertToOffsetDateTime {
|
||||
|
||||
public static OffsetDateTime convert(Date date) {
|
||||
return date.toInstant()
|
||||
.atOffset(ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
public static OffsetDateTime convert(Date date, int hour, int minute) {
|
||||
return date.toInstant()
|
||||
.atOffset(ZoneOffset.ofHoursMinutes(hour, minute));
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.datetime;
|
||||
package com.baeldung.skipweekends;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DateComparisonUtilsUnitTest {
|
||||
|
||||
|
||||
+2
-5
@@ -1,15 +1,12 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.CalendarUtils;
|
||||
import com.baeldung.datetime.DateUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CalendarUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
|
||||
+2
-4
@@ -1,14 +1,12 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.DateUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.offsetdatetime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConvertToOffsetDateTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDateIsNotNull_thenConvertToOffsetDateTime() {
|
||||
Date date = new Date();
|
||||
assertTrue(ConvertToOffsetDateTime.convert(date) instanceof OffsetDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenHasOffset_thenConvertWithOffset() {
|
||||
Date date = new Date();
|
||||
date.setHours(6);
|
||||
date.setMinutes(30);
|
||||
OffsetDateTime odt = ConvertToOffsetDateTime.convert(date, 3, 30);
|
||||
assertEquals(10, odt.getHour());
|
||||
assertEquals(0, odt.getMinute());
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
package com.baeldung.datetime;
|
||||
package com.baeldung.skipweekends;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.baeldung.skipweekends.AddSubtractDaysSkippingWeekendsUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -0,0 +1,5 @@
|
||||
## Core Java Exceptions 2
|
||||
|
||||
This module contains articles about core java exceptions
|
||||
|
||||
###
|
||||
@@ -0,0 +1,24 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-exceptions-2</artifactId>
|
||||
<name>core-java-exceptions-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<description> </description>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.rethrow;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.baeldung.rethrow.custom.InvalidDataException;
|
||||
|
||||
public class RethrowDifferentExceptionDemo {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(RethrowDifferentExceptionDemo.class.getName());
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String name = null;
|
||||
|
||||
try {
|
||||
|
||||
// Below line will throw NullPointerException
|
||||
if (name.equals("Joe")) {
|
||||
// Do blah blah..
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "So and so user is unable to cast vote because he is found uneligible");
|
||||
throw new InvalidDataException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.rethrow;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RethrowSameExceptionDemo {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(RethrowDifferentExceptionDemo.class.getName());
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String name = null;
|
||||
|
||||
try {
|
||||
|
||||
// Below line will throw NullPointerException
|
||||
if (name.equals("Joe")) {
|
||||
// Do blah blah..
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "Exception occurred due to invalid name");
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.rethrow.custom;
|
||||
|
||||
public class InvalidDataException extends Exception {
|
||||
|
||||
public InvalidDataException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.exitvshalt;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JvmExitAndHaltDemo {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(JvmExitAndHaltDemo.class);
|
||||
|
||||
static {
|
||||
Runtime.getRuntime()
|
||||
.addShutdownHook(new Thread(() -> {
|
||||
LOGGER.info("Shutdown hook initiated.");
|
||||
}));
|
||||
}
|
||||
|
||||
public void processAndExit() {
|
||||
process();
|
||||
LOGGER.info("Calling System.exit().");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public void processAndHalt() {
|
||||
process();
|
||||
LOGGER.info("Calling Runtime.getRuntime().halt().");
|
||||
Runtime.getRuntime()
|
||||
.halt(0);
|
||||
}
|
||||
|
||||
private void process() {
|
||||
LOGGER.info("Process started.");
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.exitvshalt;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JvmExitDemoUnitTest {
|
||||
|
||||
JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo();
|
||||
|
||||
@Test
|
||||
public void givenProcessComplete_whenExitCalled_thenTriggerShutdownHook() {
|
||||
jvmExitAndHaltDemo.processAndExit();
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.exitvshalt;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JvmHaltDemoUnitTest {
|
||||
|
||||
JvmExitAndHaltDemo jvmExitAndHaltDemo = new JvmExitAndHaltDemo();
|
||||
|
||||
@Test
|
||||
public void givenProcessComplete_whenHaltCalled_thenDoNotTriggerShutdownHook() {
|
||||
jvmExitAndHaltDemo.processAndHalt();
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.shutdownhook;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class ShutdownHookUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAHook_WhenShutsDown_ThenHookShouldBeExecuted() {
|
||||
Thread printingHook = new Thread(() -> System.out.println("In the middle of a shutdown"));
|
||||
Runtime.getRuntime().addShutdownHook(printingHook);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingAHook_WhenThreadAlreadyStarted_ThenThrowsAnException() {
|
||||
Thread longRunningHook = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException ignored) {}
|
||||
});
|
||||
longRunningHook.start();
|
||||
|
||||
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(longRunningHook))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Hook already running");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingAHook_WhenAlreadyExists_ThenAnExceptionWouldBeThrown() {
|
||||
Thread unfortunateHook = new Thread(() -> {});
|
||||
Runtime.getRuntime().addShutdownHook(unfortunateHook);
|
||||
|
||||
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(unfortunateHook))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Hook previously registered");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAHook_WhenItIsAlreadyRegistered_ThenWouldDeRegisterTheHook() {
|
||||
Thread willNotRun = new Thread(() -> System.out.println("Won't run!"));
|
||||
Runtime.getRuntime().addShutdownHook(willNotRun);
|
||||
|
||||
assertThat(Runtime.getRuntime().removeShutdownHook(willNotRun)).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user