Update generics in JPA Map storage to avoid casting and compiler warnings

Closes #10060
This commit is contained in:
Alexander Schwartz 2022-02-03 14:47:39 +01:00 committed by Hynek Mlnařík
parent d1b64f47fa
commit 45df1adba9
2 changed files with 16 additions and 22 deletions

View file

@ -36,7 +36,7 @@ import org.keycloak.storage.SearchableModelField;
* @param <M> Model
* @param <Self> specific implementation of this class
*/
public abstract class JpaModelCriteriaBuilder<E, M, Self extends ModelCriteriaBuilder<M, Self>> implements ModelCriteriaBuilder<M, Self> {
public abstract class JpaModelCriteriaBuilder<E, M, Self extends JpaModelCriteriaBuilder<E, M, Self>> implements ModelCriteriaBuilder<M, Self> {
private final Function<BiFunction<CriteriaBuilder, Root<E>, Predicate>, Self> instantiator;
private BiFunction<CriteriaBuilder, Root<E>, Predicate> predicateFunc = null;
@ -51,7 +51,7 @@ public abstract class JpaModelCriteriaBuilder<E, M, Self extends ModelCriteriaBu
this.predicateFunc = predicateFunc;
}
protected void validateValue(Object[] value, SearchableModelField field, ModelCriteriaBuilder.Operator op, Class<?>... expectedTypes) {
protected void validateValue(Object[] value, SearchableModelField<? super M> field, ModelCriteriaBuilder.Operator op, Class<?>... expectedTypes) {
if (value == null || expectedTypes == null || value.length != expectedTypes.length) {
throw new CriterionNotSupportedException(field, op, "Invalid argument: " + Arrays.toString(value));
}
@ -71,29 +71,21 @@ public abstract class JpaModelCriteriaBuilder<E, M, Self extends ModelCriteriaBu
}
}
@SafeVarargs
@Override
public Self and(Self... builders) {
return instantiator.apply((cb, root) -> cb.and(Stream.of(builders).map((Self b) -> {
if ( !(b instanceof JpaModelCriteriaBuilder)) throw new IllegalStateException("Invalid type of ModelCriteriaBuilder.");
return ((JpaModelCriteriaBuilder) b).getPredicateFunc().apply(cb, root);
}).toArray(Predicate[]::new)));
public final Self and(Self... builders) {
return instantiator.apply((cb, root) -> cb.and(Stream.of(builders).map((Self b) -> b.getPredicateFunc().apply(cb, root)).toArray(Predicate[]::new)));
}
@SafeVarargs
@Override
public Self or(Self... builders) {
return instantiator.apply((cb, root) -> cb.or(Stream.of(builders).map((Self b) -> {
if ( !(b instanceof JpaModelCriteriaBuilder)) throw new IllegalStateException("Invalid type of ModelCriteriaBuilder.");
return ((JpaModelCriteriaBuilder) b).getPredicateFunc().apply(cb, root);
}).toArray(Predicate[]::new)));
public final Self or(Self... builders) {
return instantiator.apply((cb, root) -> cb.or(Stream.of(builders).map((Self b) -> (b).getPredicateFunc().apply(cb, root)).toArray(Predicate[]::new)));
}
@Override
public Self not(Self builder) {
return instantiator.apply((cb, root) -> {
if ( !(builder instanceof JpaModelCriteriaBuilder)) throw new IllegalStateException("Invalid type of ModelCriteriaBuilder.");
BiFunction<CriteriaBuilder, Root<E>, Predicate> predFunc = ((JpaModelCriteriaBuilder) builder).getPredicateFunc();
return cb.not(predFunc.apply(cb, root));
});
return instantiator.apply((cb, root) -> cb.not(builder.getPredicateFunc().apply(cb, root)));
}
public BiFunction<CriteriaBuilder, Root<E>, Predicate> getPredicateFunc() {

View file

@ -96,7 +96,7 @@ public interface ModelCriteriaBuilder<M, Self extends ModelCriteriaBuilder<M, Se
ILIKE,
/**
* Operator for belonging into a collection of values. Operand in {@code value}
* can be an array (via an implicit conversion of the vararg), a {@link Collection} or a {@link Stream}.
* can be an array (via an implicit conversion of the vararg), a {@link java.util.Collection} or a {@link java.util.stream.Stream}.
*/
IN,
/** Is not null and, in addition, in case of collection not empty */
@ -115,7 +115,7 @@ public interface ModelCriteriaBuilder<M, Self extends ModelCriteriaBuilder<M, Se
* @param op Operator
* @param value Additional operands of the operator.
* @return
* @throws CriterionNotSupported If the operator is not supported for the given field.
* @throws CriterionNotSupportedException If the operator is not supported for the given field.
*/
Self compare(SearchableModelField<? super M> modelField, Operator op, Object... value);
@ -134,8 +134,9 @@ public interface ModelCriteriaBuilder<M, Self extends ModelCriteriaBuilder<M, Se
* );
* </pre>
*
* @throws CriterionNotSupported If the operator is not supported for the given field.
* @throws CriterionNotSupportedException If the operator is not supported for the given field.
*/
@SuppressWarnings("unchecked")
Self and(Self... builders);
/**
@ -153,8 +154,9 @@ public interface ModelCriteriaBuilder<M, Self extends ModelCriteriaBuilder<M, Se
* );
* </pre>
*
* @throws CriterionNotSupported If the operator is not supported for the given field.
* @throws CriterionNotSupportedException If the operator is not supported for the given field.
*/
@SuppressWarnings("unchecked")
Self or(Self... builders);
/**
@ -166,7 +168,7 @@ public interface ModelCriteriaBuilder<M, Self extends ModelCriteriaBuilder<M, Se
*
* @param builder
* @return
* @throws CriterionNotSupported If the operator is not supported for the given field.
* @throws CriterionNotSupportedException If the operator is not supported for the given field.
*/
Self not(Self builder);