forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
30 changed files
with
345 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Core Java Arrays - Conversions | ||
|
||
This module contains articles about arrays conversion in Java | ||
|
||
## Relevant Articles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?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-arrays-convert-2</artifactId> | ||
<packaging>jar</packaging> | ||
<name>core-java-arrays-convert-2</name> | ||
|
||
<parent> | ||
<artifactId>core-java-modules</artifactId> | ||
<groupId>com.baeldung.core-java-modules</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>${commons-lang3.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
96 changes: 96 additions & 0 deletions
96
...st/java/com/baeldung/array/conversions/classcastexception/ClassCastExceptionUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.baeldung.array.conversions.classcastexception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.lang.reflect.Array; | ||
import java.time.Instant; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ClassCastExceptionUnitTest { | ||
|
||
private static Logger LOG = LoggerFactory.getLogger(ClassCastExceptionUnitTest.class); | ||
|
||
Integer[] convertObjectArray() { | ||
Object[] objArray = new Object[3]; | ||
objArray[0] = 1; | ||
objArray[1] = 2; | ||
objArray[2] = 3; | ||
return (Integer[]) objArray; | ||
} | ||
|
||
Integer[] getIntegerArray() { | ||
Integer[] intArray = new Integer[3]; | ||
intArray[0] = 1; | ||
intArray[1] = 2; | ||
intArray[2] = 3; | ||
return intArray; | ||
} | ||
|
||
Integer[] objArrayToIntArray() { | ||
Object[] objArray = new Object[] { 1, 2, 3 }; | ||
Integer[] intArray = new Integer[objArray.length]; | ||
for (int i = 0; i < objArray.length; i++) { | ||
intArray[i] = (Integer) objArray[i]; | ||
} | ||
return intArray; | ||
} | ||
|
||
Integer[] objArrayToIntArrayByStream() { | ||
Object[] objArray = new Object[] { 1, 2, 3 }; | ||
Integer[] intArray = Stream.of(objArray) | ||
.toArray(Integer[]::new); | ||
return intArray; | ||
} | ||
|
||
<T> T[] convertFromObjectArray(Class<T> clazz, Object[] objArray) { | ||
T[] targetArray = (T[]) Array.newInstance(clazz, objArray.length); | ||
for (int i = 0; i < objArray.length; i++) { | ||
if (clazz.isInstance(objArray[i])) { | ||
targetArray[i] = clazz.cast(objArray[i]); | ||
} else { | ||
throw new ClassCastException("Element #" + i + ": Cannot cast " + objArray[i].getClass() | ||
.getName() + " to " + clazz.getName()); | ||
} | ||
} | ||
return targetArray; | ||
} | ||
|
||
@Test | ||
void whenCallingConvertObjectArray_thenClassCastException() { | ||
Exception ex = assertThrows(ClassCastException.class, this::convertObjectArray); | ||
LOG.error("The exception stacktrace:", ex); | ||
} | ||
|
||
@Test | ||
void whenCallingIntegerArray_thenCorrect() { | ||
assertArrayEquals(new Integer[] { 1, 2, 3 }, getIntegerArray()); | ||
} | ||
|
||
@Test | ||
void whenCallingObjArrayToIntArray_thenCorrect() { | ||
assertArrayEquals(new Integer[] { 1, 2, 3 }, objArrayToIntArray()); | ||
} | ||
|
||
@Test | ||
void whenCallingObjArrayToIntArrayByStream_thenCorrect() { | ||
assertArrayEquals(new Integer[] { 1, 2, 3 }, objArrayToIntArrayByStream()); | ||
} | ||
|
||
@Test | ||
void whenCallingSingleElementArray_thenCorrect() { | ||
assertArrayEquals(new Integer[] { 1, 2, 3 }, convertFromObjectArray(Integer.class, new Object[] { 1, 2, 3 })); | ||
assertArrayEquals(new String[] { "I'm Kai", "I'm Liam", "I'm Kevin" }, | ||
convertFromObjectArray(String.class, new Object[] { "I'm Kai", "I'm Liam", "I'm Kevin" })); | ||
//class cast ex: | ||
Exception ex = assertThrows(ClassCastException.class, | ||
() -> convertFromObjectArray(String.class, new Object[] { "I'm Kai", Instant.now(), "I'm Kevin" })); | ||
assertEquals("Element #1: Cannot cast java.time.Instant to java.lang.String", ex.getMessage()); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...s-8/src/test/java/com/baeldung/map/putnonnullvalueinmap/PutNonNullValueInMapUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.baeldung.map.putnonnullvalueinmap; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PutNonNullValueInMapUnitTest { | ||
|
||
private Map<Integer, String> map; | ||
private String value; | ||
|
||
@Test | ||
void givenMapWithNullValue_whenUsingStreamFilter_thenObtainMapWithNonNullValues() { | ||
final Map<Integer, String> nullValueMap = new HashMap<>(); | ||
nullValueMap.put(1, value); | ||
|
||
final Map<Integer, String> mapWithoutNullValues = nullValueMap.entrySet() | ||
.stream() | ||
.filter(entry -> Objects.nonNull(entry.getValue())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
assertThat(mapWithoutNullValues).isEmpty(); | ||
} | ||
|
||
@Test | ||
void givenNullValue_whenUsingIfStatementWithPutMethod_thenObtainMapWithNonNullValues() { | ||
if (value != null) { | ||
map.put(1, value); | ||
} | ||
assertThat(map).isEmpty(); | ||
} | ||
|
||
@Test | ||
void givenNullValue_whenUsingOptionalIfPresentWithPutMethod_thenObtainMapWithNonNullValues() { | ||
Optional.ofNullable(value) | ||
.ifPresent(nonNullValue -> map.put(1, nonNullValue)); | ||
assertThat(map).isEmpty(); | ||
} | ||
|
||
@BeforeEach | ||
void init() { | ||
value = null; | ||
map = new HashMap<>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
...n/java/com/baeldung/MongoBsonExample.java → ...n/java/com/baeldung/MongoBsonExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...m/baeldung/mongo/CollectionExistence.java → ...lectionexistence/CollectionExistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.mongo; | ||
package com.baeldung.mongo.collectionexistence; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a/com/baeldung/morphia/domain/Author.java → ...baeldung/mongo/morphia/domain/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.morphia.domain; | ||
package com.baeldung.mongo.morphia.domain; | ||
|
||
import java.util.List; | ||
|
||
|
3 changes: 1 addition & 2 deletions
3
...ava/com/baeldung/morphia/domain/Book.java → ...m/baeldung/mongo/morphia/domain/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...om/baeldung/morphia/domain/Publisher.java → ...ldung/mongo/morphia/domain/Publisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.morphia.domain; | ||
package com.baeldung.mongo.morphia.domain; | ||
|
||
import org.bson.types.ObjectId; | ||
|
||
|
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../main/java/com/baeldung/tagging/Post.java → ...java/com/baeldung/mongo/tagging/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.tagging; | ||
package com.baeldung.mongo.tagging; | ||
|
||
import java.util.List; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a/com/baeldung/tagging/TagRepository.java → ...baeldung/mongo/tagging/TagRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ng/mongo/CollectionExistenceLiveTest.java → ...xistence/CollectionExistenceLiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...com/baeldung/tagging/TaggingLiveTest.java → ...eldung/mongo/tagging/TaggingLiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.