-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #542 from eclipse/include-array-fields-supports
Include support to Arrays fields
- Loading branch information
Showing
36 changed files
with
925 additions
and
190 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
51 changes: 51 additions & 0 deletions
51
...apping-api-core/src/main/java/org/eclipse/jnosql/mapping/metadata/ArrayFieldMetadata.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 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.metadata; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* The ArrayFieldMetadata interface extends the {@link FieldMetadata} interface and provides | ||
* additional information about a parameter with a generic type for arrays. | ||
* | ||
* <p>This interface is used to represent parameters of generic types, where the type is an array | ||
* containing elements of a specific type.</p> | ||
* | ||
* @see FieldMetadata | ||
*/ | ||
public interface ArrayFieldMetadata extends FieldMetadata { | ||
|
||
/** | ||
* Returns true if the array element type has either Entity or Embeddable annotations. | ||
* | ||
* @return true if the element type has Entity or Embeddable annotations | ||
*/ | ||
boolean isEmbeddable(); | ||
|
||
/** | ||
* Returns the {@link Class} representing the type of elements in the array. | ||
* | ||
* @return the element type of the array parameter | ||
*/ | ||
Class<?> elementType(); | ||
|
||
/** | ||
* Converts the provided {@link Collection} into an array of the appropriate type. | ||
* | ||
* @param collection the collection to be converted into an array | ||
* @return an array containing the elements of the collection, as an Object | ||
*/ | ||
Object arrayInstance(Collection<?> collection); | ||
} |
45 changes: 45 additions & 0 deletions
45
...ng-api-core/src/main/java/org/eclipse/jnosql/mapping/metadata/ArrayParameterMetaData.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,45 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.metadata; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* The ArrayParameterMetaData interface extends the {@link ParameterMetaData} interface and provides | ||
* additional information about a parameter with a generic type for arrays. | ||
* | ||
* <p>This interface is used to represent parameters of generic types, where the type is an array | ||
* containing elements of a specific type.</p> | ||
* | ||
* @see ParameterMetaData | ||
*/ | ||
public interface ArrayParameterMetaData extends ParameterMetaData { | ||
|
||
/** | ||
* Returns the {@link Class} representing the type of elements in the array. | ||
* | ||
* @return the element type of the array parameter | ||
*/ | ||
Class<?> elementType(); | ||
|
||
/** | ||
* Converts the provided {@link Collection} into an array of the appropriate type. | ||
* | ||
* @param collection the collection to be converted into an array | ||
* @return an array containing the elements of the collection, as an Object | ||
*/ | ||
Object arrayInstance(Collection<?> collection); | ||
} | ||
|
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
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
113 changes: 113 additions & 0 deletions
113
...ection/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultArrayFieldMetadata.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,113 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.reflection; | ||
|
||
import jakarta.nosql.AttributeConverter; | ||
import jakarta.nosql.Embeddable; | ||
import jakarta.nosql.Entity; | ||
import org.eclipse.jnosql.communication.TypeReference; | ||
import org.eclipse.jnosql.communication.Value; | ||
import org.eclipse.jnosql.mapping.metadata.ArrayFieldMetadata; | ||
import org.eclipse.jnosql.mapping.metadata.MappingType; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Field; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
final class DefaultArrayFieldMetadata extends AbstractFieldMetadata implements ArrayFieldMetadata { | ||
|
||
public static final TypeReference<List<Object>> TYPE_SUPPLIER = new TypeReference<>() {}; | ||
private final Class<?> elementType; | ||
|
||
DefaultArrayFieldMetadata(MappingType type, Field field, String name, Class<?> elementType, | ||
Class<? extends AttributeConverter<?, ?>> converter, | ||
FieldReader reader, FieldWriter writer, String udt) { | ||
super(type, field, name, converter, reader, writer, udt); | ||
this.elementType = elementType; | ||
} | ||
|
||
@Override | ||
public Object value(Value value) { | ||
if(value.get() instanceof Iterable) { | ||
return value.get(TYPE_SUPPLIER).toArray(); | ||
} else { | ||
return Value.of(Collections.singletonList(value.get())).get(TYPE_SUPPLIER); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isId() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
DefaultArrayFieldMetadata that = (DefaultArrayFieldMetadata) o; | ||
return mappingType == that.mappingType && | ||
Objects.equals(field, that.field) && | ||
Objects.equals(elementType, that.elementType) && | ||
Objects.equals(name, that.name); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(mappingType, field, name, elementType); | ||
} | ||
|
||
@Override | ||
public boolean isEmbeddable() { | ||
return isEmbeddableField() || isEntityField(); | ||
} | ||
|
||
private boolean isEntityField() { | ||
return hasFieldAnnotation(Entity.class); | ||
} | ||
|
||
private boolean isEmbeddableField() { | ||
return hasFieldAnnotation(Embeddable.class); | ||
} | ||
|
||
private boolean hasFieldAnnotation(Class<? extends Annotation> annotation) { | ||
return elementType.getAnnotation(annotation) != null; | ||
} | ||
|
||
@Override | ||
public Class<?> elementType() { | ||
return elementType; | ||
} | ||
|
||
@Override | ||
public Object arrayInstance(Collection<?> collection) { | ||
var array = Array.newInstance(elementType, collection.size()); | ||
int index = 0; | ||
for (Object item : collection) { | ||
Array.set(array, index++, item); | ||
} | ||
return array; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...on/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultArrayParameterMetaData.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,52 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.eclipse.jnosql.mapping.reflection; | ||
|
||
import jakarta.nosql.AttributeConverter; | ||
import org.eclipse.jnosql.mapping.metadata.ArrayParameterMetaData; | ||
import org.eclipse.jnosql.mapping.metadata.MappingType; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Collection; | ||
|
||
class DefaultArrayParameterMetaData extends DefaultParameterMetaData implements ArrayParameterMetaData { | ||
|
||
|
||
private final Class<?> elementType; | ||
|
||
DefaultArrayParameterMetaData(String name, Class<?> type, boolean id, | ||
Class<? extends AttributeConverter<?, ?>> converter, | ||
MappingType mappingType, Class<?> elementType) { | ||
super(name, type, id, converter, mappingType); | ||
this.elementType = elementType; | ||
} | ||
|
||
@Override | ||
public Class<?> elementType() { | ||
return elementType; | ||
} | ||
|
||
@Override | ||
public Object arrayInstance(Collection<?> collection) { | ||
var array = Array.newInstance(elementType, collection.size()); | ||
int index = 0; | ||
for (Object item : collection) { | ||
Array.set(array, index++, item); | ||
} | ||
return array; | ||
} | ||
|
||
|
||
} |
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.