Skip to content

Commit

Permalink
HSEARCH-3997 Add built-in container extractors for arrays of primitiv…
Browse files Browse the repository at this point in the history
…e types
  • Loading branch information
yrodiere authored and fax4ever committed Sep 23, 2020
1 parent 52cd285 commit 056a2ac
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 4 deletions.
Expand Up @@ -146,6 +146,16 @@
public class HibernateOrmPathFilterFactory implements PojoPathFilterFactory<Set<String>> {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private static final Set<String> PRIMITIVE_EXTRACTOR_NAMES = CollectionHelper.asImmutableSet(
BuiltinContainerExtractors.ARRAY_CHAR,
BuiltinContainerExtractors.ARRAY_BOOLEAN,
BuiltinContainerExtractors.ARRAY_BYTE,
BuiltinContainerExtractors.ARRAY_SHORT,
BuiltinContainerExtractors.ARRAY_INT,
BuiltinContainerExtractors.ARRAY_LONG,
BuiltinContainerExtractors.ARRAY_FLOAT,
BuiltinContainerExtractors.ARRAY_DOUBLE
);

private final PersistentClass persistentClass;

Expand Down Expand Up @@ -304,7 +314,12 @@ private Optional<Value> resolveExtractorPath(Set<String> pathsAsStrings, PojoMod
}

private Value resolveExtractor(org.hibernate.mapping.Collection collectionValue, String extractorName) {
if ( collectionValue instanceof org.hibernate.mapping.Array ) {
if ( collectionValue instanceof org.hibernate.mapping.PrimitiveArray ) {
if ( extractorName == null || PRIMITIVE_EXTRACTOR_NAMES.contains( extractorName ) ) {
return collectionValue.getElement();
}
}
else if ( collectionValue instanceof org.hibernate.mapping.Array ) {
if ( extractorName == null || BuiltinContainerExtractors.ARRAY_OBJECT.equals( extractorName ) ) {
return collectionValue.getElement();
}
Expand Down
Expand Up @@ -26,6 +26,38 @@ private BuiltinContainerExtractors() {
*/
@Deprecated
public static final String ARRAY = ARRAY_OBJECT;
/**
* The name of an extractor that extracts elements from an array of primitive chars ({@code char[]}).
*/
public static final String ARRAY_CHAR = "array-char";
/**
* The name of an extractor that extracts elements from an array of primitive booleans ({@code boolean[]}).
*/
public static final String ARRAY_BOOLEAN = "array-boolean";
/**
* The name of an extractor that extracts elements from an array of primitive bytes ({@code byte[]}).
*/
public static final String ARRAY_BYTE = "array-byte";
/**
* The name of an extractor that extracts elements from an array of primitive shorts ({@code short[]}).
*/
public static final String ARRAY_SHORT = "array-short";
/**
* The name of an extractor that extracts elements from an array of primitive integers ({@code int[]}).
*/
public static final String ARRAY_INT = "array-int";
/**
* The name of an extractor that extracts elements from an array of primitive longs ({@code long[]}).
*/
public static final String ARRAY_LONG = "array-long";
/**
* The name of an extractor that extracts elements from an array of primitive floats ({@code float[]}).
*/
public static final String ARRAY_FLOAT = "array-float";
/**
* The name of an extractor that extracts elements from an array of primitive double ({@code double[]}).
*/
public static final String ARRAY_DOUBLE = "array-double";
/**
* The name of an extractor that extracts elements from a {@link java.util.Collection}.
*/
Expand Down
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class BooleanArrayElementExtractor implements ContainerExtractor<boolean[], Boolean> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_BOOLEAN;
}

@Override
public void extract(boolean[] container, Consumer<Boolean> consumer) {
if ( container == null ) {
return;
}
for ( boolean element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class ByteArrayElementExtractor implements ContainerExtractor<byte[], Byte> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_BYTE;
}

@Override
public void extract(byte[] container, Consumer<Byte> consumer) {
if ( container == null ) {
return;
}
for ( byte element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class CharArrayElementExtractor implements ContainerExtractor<char[], Character> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_CHAR;
}

@Override
public void extract(char[] container, Consumer<Character> consumer) {
if ( container == null ) {
return;
}
for ( char element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class DoubleArrayElementExtractor implements ContainerExtractor<double[], Double> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_DOUBLE;
}

@Override
public void extract(double[] container, Consumer<Double> consumer) {
if ( container == null ) {
return;
}
for ( double element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class FloatArrayElementExtractor implements ContainerExtractor<float[], Float> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_FLOAT;
}

@Override
public void extract(float[] container, Consumer<Float> consumer) {
if ( container == null ) {
return;
}
for ( float element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class IntArrayElementExtractor implements ContainerExtractor<int[], Integer> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_INT;
}

@Override
public void extract(int[] container, Consumer<Integer> consumer) {
if ( container == null ) {
return;
}
for ( int element : container ) {
consumer.accept( element );
}
}
}
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class LongArrayElementExtractor implements ContainerExtractor<long[], Long> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_LONG;
}

@Override
public void extract(long[] container, Consumer<Long> consumer) {
if ( container == null ) {
return;
}
for ( long element : container ) {
consumer.accept( element );
}
}
}
Expand Up @@ -11,7 +11,7 @@
import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class ArrayElementExtractor<T> implements ContainerExtractor<T[], T> {
public class ObjectArrayElementExtractor<T> implements ContainerExtractor<T[], T> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_OBJECT;
Expand Down
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.extractor.builtin.impl;

import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;

public class ShortArrayElementExtractor implements ContainerExtractor<short[], Short> {
@Override
public String toString() {
return BuiltinContainerExtractors.ARRAY_SHORT;
}

@Override
public void extract(short[] container, Consumer<Short> consumer) {
if ( container == null ) {
return;
}
for ( short element : container ) {
consumer.accept( element );
}
}
}
Expand Up @@ -16,7 +16,14 @@
import org.hibernate.search.mapper.pojo.extractor.ContainerExtractor;
import org.hibernate.search.mapper.pojo.extractor.ContainerExtractorConfigurationContext;
import org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.ArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.BooleanArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.ByteArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.CharArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.DoubleArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.FloatArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.IntArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.LongArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.ObjectArrayElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.CollectionElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.IterableElementExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.MapKeyExtractor;
Expand All @@ -25,6 +32,7 @@
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.OptionalIntValueExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.OptionalLongValueExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.OptionalValueExtractor;
import org.hibernate.search.mapper.pojo.extractor.builtin.impl.ShortArrayElementExtractor;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

Expand All @@ -51,7 +59,15 @@ private ContainerExtractorRegistry(Map<String, Class<? extends ContainerExtracto
addDefaultExtractor( BuiltinContainerExtractors.OPTIONAL_INT, OptionalIntValueExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.OPTIONAL_LONG, OptionalLongValueExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.OPTIONAL_DOUBLE, OptionalDoubleValueExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_OBJECT, ArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_CHAR, CharArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_BOOLEAN, BooleanArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_BYTE, ByteArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_SHORT, ShortArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_INT, IntArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_LONG, LongArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_FLOAT, FloatArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_DOUBLE, DoubleArrayElementExtractor.class );
addDefaultExtractor( BuiltinContainerExtractors.ARRAY_OBJECT, ObjectArrayElementExtractor.class );

addNonDefaultExtractor( BuiltinContainerExtractors.MAP_KEY, MapKeyExtractor.class );
}
Expand Down

0 comments on commit 056a2ac

Please sign in to comment.