[BAEL-16640] - Fixed conflicts
This commit is contained in:
commit
5e26506b92
Binary file not shown.
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 10 KiB |
@ -0,0 +1,37 @@
|
||||
package com.baeldung.algorithms.smallestinteger;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SmallestMissingPositiveInteger {
|
||||
public static int searchInSortedArray(int[] input) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (i != input[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return input.length;
|
||||
}
|
||||
|
||||
public static int searchInUnsortedArraySortingFirst(int[] input) {
|
||||
Arrays.sort(input);
|
||||
return searchInSortedArray(input);
|
||||
}
|
||||
|
||||
public static int searchInUnsortedArrayBooleanArray(int[] input) {
|
||||
boolean[] flags = new boolean[input.length];
|
||||
for (int number : input) {
|
||||
if (number < flags.length) {
|
||||
flags[number] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < flags.length; i++) {
|
||||
if (!flags[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return flags.length;
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.smallestinteger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SmallestMissingPositiveIntegerUnitTest {
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInSortedArray_thenThree() {
|
||||
int[] input = new int[] {0, 1, 2, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInSortedArray_thenOne() {
|
||||
int[] input = new int[] {0, 2, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInSortedArray_thenArrayLength() {
|
||||
int[] input = new int[] {0, 1, 2, 3, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInUnsortedArraySortingFirst_thenThree() {
|
||||
int[] input = new int[] {1, 4, 0, 5, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArraySortingFirst_thenOne() {
|
||||
int[] input = new int[] {4, 2, 0, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArraySortingFirst_thenArrayLength() {
|
||||
int[] input = new int[] {4, 5, 1, 3, 0, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenThree() {
|
||||
int[] input = new int[] {1, 4, 0, 5, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenOne() {
|
||||
int[] input = new int[] {4, 2, 0, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArrayBooleanArray_thenArrayLength() {
|
||||
int[] input = new int[] {4, 5, 1, 3, 0, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.balancedbinarytree;
|
||||
|
||||
public class BalancedBinaryTree {
|
||||
|
||||
public static boolean isBalanced(Tree tree) {
|
||||
return isBalancedRecursive(tree, -1).isBalanced;
|
||||
}
|
||||
|
||||
private static Result isBalancedRecursive(Tree tree, int depth) {
|
||||
if (tree == null) {
|
||||
return new Result(true, -1);
|
||||
}
|
||||
|
||||
Result leftSubtreeResult = isBalancedRecursive(tree.left(), depth + 1);
|
||||
Result rightSubtreeResult = isBalancedRecursive(tree.right(), depth + 1);
|
||||
|
||||
boolean isBalanced = Math.abs(leftSubtreeResult.height - rightSubtreeResult.height) <= 1;
|
||||
boolean subtreesAreBalanced = leftSubtreeResult.isBalanced && rightSubtreeResult.isBalanced;
|
||||
int height = Math.max(leftSubtreeResult.height, rightSubtreeResult.height) + 1;
|
||||
|
||||
return new Result(isBalanced && subtreesAreBalanced, height);
|
||||
}
|
||||
|
||||
private static final class Result {
|
||||
private final boolean isBalanced;
|
||||
private final int height;
|
||||
|
||||
private Result(boolean isBalanced, int height) {
|
||||
this.isBalanced = isBalanced;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.balancedbinarytree;
|
||||
|
||||
public class Tree {
|
||||
private final int value;
|
||||
private final Tree left;
|
||||
private final Tree right;
|
||||
|
||||
public Tree(int value, Tree left, Tree right) {
|
||||
this.value = value;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public int value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Tree left() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public Tree right() {
|
||||
return right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("[%s, %s, %s]",
|
||||
value,
|
||||
left == null ? "null" : left.toString(),
|
||||
right == null ? "null" : right.toString()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.binarygap;
|
||||
|
||||
public class BinaryGap {
|
||||
static int calculateBinaryGap(int n) {
|
||||
return calculateBinaryGap(n >>> Integer.numberOfTrailingZeros(n), 0, 0);
|
||||
}
|
||||
|
||||
static int calculateBinaryGap(int n, int current, int maximum) {
|
||||
if (n == 0) {
|
||||
return maximum;
|
||||
} else if ((n & 1) == 0) {
|
||||
return calculateBinaryGap(n >>> 1, current + 1, maximum);
|
||||
} else {
|
||||
return calculateBinaryGap(n >>> 1, 0, Math.max(maximum, current));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.balancedbinarytree;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class BalancedBinaryTreeUnitTest extends BinaryTreeDataProvider {
|
||||
|
||||
@Test
|
||||
public void givenBalancedTrees_whenCallingIsBalanced_ShouldReturnTrue() {
|
||||
for (Tree tree : balancedTrees()) {
|
||||
assertTrue(toString(tree) + " should be balanced", BalancedBinaryTree.isBalanced(tree));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnbalancedTrees_whenCallingIsBalanced_ShouldReturnFalse() {
|
||||
for (Tree tree : unbalancedTrees()) {
|
||||
assertFalse(toString(tree) + " should not be balanced", BalancedBinaryTree.isBalanced(tree));
|
||||
}
|
||||
}
|
||||
|
||||
private String toString(Tree tree) {
|
||||
return tree != null ? tree.toString() : "null";
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.baeldung.algorithms.balancedbinarytree;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
class BinaryTreeDataProvider {
|
||||
|
||||
static Collection<Tree> balancedTrees() {
|
||||
return Arrays.asList(
|
||||
null,
|
||||
leaf(1),
|
||||
tree(1, leaf(2), leaf(3)),
|
||||
tree(
|
||||
1,
|
||||
leaf(2),
|
||||
tree(3, leaf(4), null)
|
||||
),
|
||||
tree(
|
||||
1,
|
||||
tree(
|
||||
2,
|
||||
tree(3, leaf(4), null),
|
||||
leaf(5)
|
||||
),
|
||||
tree(
|
||||
6,
|
||||
leaf(7),
|
||||
tree(8, null, leaf(9))
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
static Collection<Tree> unbalancedTrees() {
|
||||
return Arrays.asList(
|
||||
tree(
|
||||
1,
|
||||
tree(2, leaf(3), null),
|
||||
null
|
||||
),
|
||||
tree(
|
||||
1,
|
||||
tree(
|
||||
2,
|
||||
tree(3, leaf(4), leaf(5)),
|
||||
null
|
||||
),
|
||||
tree(6, leaf(7), null)
|
||||
),
|
||||
tree(
|
||||
1,
|
||||
tree(2, leaf(3), null),
|
||||
tree(
|
||||
4,
|
||||
tree(5, leaf(6), leaf(7)),
|
||||
null
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static Tree leaf(int value) {
|
||||
return new Tree(value, null, null);
|
||||
}
|
||||
|
||||
private static Tree tree(int value, Tree left, Tree right) {
|
||||
return new Tree(value, left, right);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.binarygap;
|
||||
|
||||
import static com.baeldung.algorithms.binarygap.BinaryGap.calculateBinaryGap;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BinaryGapUnitTest {
|
||||
|
||||
@Test public void givenNoOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
|
||||
|
||||
int result = calculateBinaryGap(63);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test public void givenTrailingZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
|
||||
|
||||
int result = calculateBinaryGap(40);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test public void givenSingleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
|
||||
|
||||
int result = calculateBinaryGap(9);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test public void givenMultipleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
|
||||
|
||||
int result = calculateBinaryGap(145);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
}
|
17
core-java-modules/core-java-9-improvements/README.md
Normal file
17
core-java-modules/core-java-9-improvements/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
## Core Java 9
|
||||
|
||||
This module contains articles about the improvements to core Java features introduced with Java 9.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
|
||||
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
|
||||
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
|
||||
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
|
||||
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
|
||||
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
|
||||
|
||||
#### Relevant articles not in this module:
|
||||
|
||||
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api) (see the [core-java-os](/core-java-os) module)
|
||||
|
73
core-java-modules/core-java-9-improvements/pom.xml
Normal file
73
core-java-modules/core-java-9-improvements/pom.xml
Normal file
@ -0,0 +1,73 @@
|
||||
<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-9</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-9</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>apache.snapshots</id>
|
||||
<url>http://repository.apache.org/snapshots/</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<guava.version>25.1-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
11
core-java-modules/core-java-9-jigsaw/README.md
Normal file
11
core-java-modules/core-java-9-jigsaw/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
## Core Java 9
|
||||
|
||||
This module contains articles about Project Jigsaw and the Java Platform Module System (JPMS), introduced with Java 9.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
|
||||
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
|
||||
- [Java 9 java.lang.Module API](https://www.baeldung.com/java-9-module-api)
|
||||
|
||||
|
35
core-java-modules/core-java-9-jigsaw/pom.xml
Normal file
35
core-java-modules/core-java-9-jigsaw/pom.xml
Normal file
@ -0,0 +1,35 @@
|
||||
<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-9-jigsaw</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-9-jigsaw</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
14
core-java-modules/core-java-9-new-features/README.md
Normal file
14
core-java-modules/core-java-9-new-features/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
## Core Java 9
|
||||
|
||||
This module contains articles about core Java features that have been introduced in Java 9.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
|
||||
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
|
||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
||||
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
|
||||
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
60
core-java-modules/core-java-9-new-features/pom.xml
Normal file
60
core-java-modules/core-java-9-new-features/pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
<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-9-new-features</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-9-new-features</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>apache.snapshots</id>
|
||||
<url>http://repository.apache.org/snapshots/</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
13
core-java-modules/core-java-9/.gitignore
vendored
13
core-java-modules/core-java-9/.gitignore
vendored
@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
@ -4,28 +4,13 @@ This module contains articles about Java 9 core features
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
|
||||
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
|
||||
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
|
||||
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
|
||||
- [Method Handles in Java](http://www.baeldung.com/java-method-handles)
|
||||
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
|
||||
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
|
||||
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
||||
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
|
||||
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
||||
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
|
||||
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
|
||||
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
|
||||
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
|
||||
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
|
||||
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
|
||||
- [Java 9 java.lang.Module API](https://www.baeldung.com/java-9-module-api)
|
||||
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
|
||||
- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional)
|
||||
|
||||
Note: also contains part of the code for the article
|
||||
[How to Filter a Collection in Java](https://www.baeldung.com/java-collection-filtering).
|
||||
|
@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
@ -1,2 +0,0 @@
|
||||
### Relevant Artiles:
|
||||
- [Filtering a Stream of Optionals in Java](http://www.baeldung.com/java-filter-stream-of-optional)
|
17
core-java-modules/core-java-date-operations/pom.xml
Normal file
17
core-java-modules/core-java-date-operations/pom.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?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>
|
||||
<artifactId>core-java-date-operations</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class CalendarUtils {
|
||||
|
||||
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.DAY_OF_YEAR, amount);
|
||||
return calendar;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
public static Date getDate(long millis) {
|
||||
return new Date(millis);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
return new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString) {
|
||||
return Date.valueOf(dateAsString);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Date(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimeUtils {
|
||||
|
||||
public static Time getNow() {
|
||||
return new Time(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Time getTime(String timeAsString) {
|
||||
return Time.valueOf(timeAsString);
|
||||
}
|
||||
|
||||
public static Time getTime(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Time(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimestampUtils {
|
||||
|
||||
public static Timestamp getNow() {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String timestampAsString) {
|
||||
return Timestamp.valueOf(timestampAsString);
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Timestamp(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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;
|
||||
|
||||
public class CalendarUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
|
||||
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
|
||||
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
|
||||
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
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;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTimeMillis_thenDateIsReturned() {
|
||||
Date now = DateUtils.getNow();
|
||||
assertEquals(DateUtils.getDate(now.getTime()), now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
|
||||
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.sql.DateUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentDate_thenTodayIsReturned() {
|
||||
assertEquals(DateUtils.getNow(), new Date());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
DateUtils.getDate("2020 01 01");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.sql.TimeUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentTime_thenNowIsReturned() {
|
||||
assertEquals(TimeUtils.getNow(), new Date());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimeUtils.getTime("10 11 12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.sql.TimestampUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimestampUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentTimestamp_thenNowIsReturned() {
|
||||
assertEquals(TimestampUtils.getNow()
|
||||
.getTime(), new Date().getTime());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimestampUtils.getTimestamp("2020/01/01 10:11-12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss"));
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user