Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvariyath committed Feb 7, 2025
2 parents e8fcc4a + 8d8a7af commit b0bff43
Show file tree
Hide file tree
Showing 30 changed files with 345 additions and 128 deletions.
5 changes: 5 additions & 0 deletions core-java-modules/core-java-arrays-convert-2/README.md
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
24 changes: 24 additions & 0 deletions core-java-modules/core-java-arrays-convert-2/pom.xml
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>
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());
}

}
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<>();
}
}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<module>core-java-arrays-guides</module>
<module>core-java-arrays-multidimensional</module>
<module>core-java-arrays-convert</module>
<module>core-java-arrays-convert-2</module>
<module>core-java-arrays-operations-basic</module>
<module>core-java-arrays-operations-basic-2</module>
<module>core-java-arrays-operations-basic-3</module>
Expand Down
4 changes: 1 addition & 3 deletions persistence-modules/java-mongodb-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

This module contains articles about MongoDB in Java.

- [Guide to Upsert in MongoDB](https://www.baeldung.com/mongodb-upsert)
- [Bulk Update of Documents in MongoDB](https://www.baeldung.com/mongodb-bulk-update-documents)
- [Case Insensitive Sorting in MongoDB](https://www.baeldung.com/java-mongodb-case-insensitive-sorting)
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
- [Push Operations in MongoDB](https://www.baeldung.com/mongodb-push-operations)
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
- [Retrieve a Value from MongoDB by Its Key Name](https://www.baeldung.com/mongodb-get-value-by-key-name)
- [Push and Set Operations in Same MongoDB Update](https://www.baeldung.com/java-mongodb-push-set)
- [Checking Connection to MongoDB](https://www.baeldung.com/mongodb-check-connection)
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
- More articles: [[<-- prev]](../java-mongodb)
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.baeldung;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MongoBsonExample
{
Expand Down

This file was deleted.

6 changes: 5 additions & 1 deletion persistence-modules/java-mongodb-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
- [Guide to Find in MongoDB](https://www.baeldung.com/mongodb-find)
- [Query Documents using Document ID in MongoDB](https://www.baeldung.com/mongodb-query-documents-id)
- [Insert Array Inside an Object in MongoDB](https://www.baeldung.com/java-mongodb-document-insert-array)
- [Guide to Filters in MongoDB](https://www.baeldung.com/java-mongodb-filters)
- [Add Field to an Existing MongoDB Bson Filter in Java](https://www.baeldung.com/java-mongodb-add-field-bson-filter)
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
- [Check Collection Existence in MongoDB](https://www.baeldung.com/java-check-collection-existence-mongodb)
- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id)
- More articles: [[<-- prev]](../java-mongodb-2)
6 changes: 6 additions & 0 deletions persistence-modules/java-mongodb-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>${morphia.version}</version>
</dependency>
</dependencies>

<properties>
<mongo.version>3.12.11</mongo.version>
<morphia.version>2.0.0</morphia.version>
</properties>

</project>
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;

Expand Down
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.baeldung.morphia.domain;
package com.baeldung.mongo.morphia.domain;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Field;
import dev.morphia.annotations.Id;
Expand Down
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;

Expand Down
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;

Expand Down
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.io.Closeable;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.mongo;
package com.baeldung.mongo.collectionexistence;

import static org.junit.Assert.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.morphia;
package com.baeldung.mongo.morphia;

import static dev.morphia.aggregation.Group.grouping;
import static dev.morphia.aggregation.Group.push;
Expand All @@ -17,9 +17,9 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.baeldung.morphia.domain.Author;
import com.baeldung.morphia.domain.Book;
import com.baeldung.morphia.domain.Publisher;
import com.baeldung.mongo.morphia.domain.Author;
import com.baeldung.mongo.morphia.domain.Book;
import com.baeldung.mongo.morphia.domain.Publisher;
import com.mongodb.client.MongoClients;
import com.mongodb.client.model.ReturnDocument;

Expand Down
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.io.IOException;
import java.util.Arrays;
Expand Down
11 changes: 5 additions & 6 deletions persistence-modules/java-mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ This module contains articles about MongoDB in Java.
### Relevant articles:

- [A Guide to MongoDB With Java](https://www.baeldung.com/java-mongodb)
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id)
- [Update Multiple Fields in a MongoDB Document](https://www.baeldung.com/mongodb-update-multiple-fields)
- [Update Documents in MongoDB](https://www.baeldung.com/mongodb-update-documents)
- [Check Collection Existence in MongoDB](https://www.baeldung.com/java-check-collection-existence-mongodb)
- More articles: [next -->](../java-mongodb-2)
- [Guide to Filters in MongoDB](https://www.baeldung.com/java-mongodb-filters)
- [Checking Connection to MongoDB](https://www.baeldung.com/mongodb-check-connection)
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
- [Guide to Upsert in MongoDB](https://www.baeldung.com/mongodb-upsert)
- More articles: [next -->](../java-mongodb-2)
Loading

0 comments on commit b0bff43

Please sign in to comment.