[jOOQ/jOOQ#7100] Add <U> Field<T>.convert(Binding<T, U>), convert(Converter<T, U>), convert(Class<U>, Function<T, U>, Function<U, T>)

This includes:

- [jOOQ/jOOQ#11810] RecordMapper<R, E> should extend Function<R, E>
This commit is contained in:
Lukas Eder 2021-04-28 20:58:15 +02:00
parent 2b28bc4ad4
commit 502f1cbaae
3 changed files with 82 additions and 1 deletions

View File

@ -78,6 +78,8 @@ import java.util.Map;
import java.util.function.Function;
import org.jooq.conf.Settings;
import org.jooq.exception.MappingException;
import org.jooq.impl.CustomField;
import org.jooq.impl.DSL;
import org.jooq.types.Interval;
// ...
@ -149,6 +151,58 @@ extends
// API
// ------------------------------------------------------------------------
/**
* Apply an ad-hoc data type {@link Binding} to this field expression.
* <p>
* Rather than attaching data type bindings at code generation time, or
* creating cumbersome expressions using
* {@link DataType#asConvertedDataType(Binding)}, this method allows for
* creating a derived field expression with an ad-hoc data type binding for
* single query usage.
*
* @param <U> The user type.
* @param binding The binding to be applied on any operations using this
* field.
* @return A derived field expression using a new binding.
*/
<U> Field<U> convert(Binding<T, U> binding);
/**
* Apply an ad-hoc data type {@link Converter} to this field expression.
* <p>
* Rather than attaching data type converters at code generation time, or
* creating cumbersome expressions using
* {@link DataType#asConvertedDataType(Converter)}, this method allows for
* creating a derived field expression with an ad-hoc data type converter for
* single query usage.
*
* @param <U> The user type.
* @param converter The converter to be applied on any operations using this
* field.
* @return A derived field expression using a new converter.
*/
<U> Field<U> convert(Converter<T, U> converter);
/**
* Apply an ad-hoc data type {@link Converter} to this field expression.
* <p>
* Rather than attaching data type converters at code generation time, or
* creating cumbersome expressions using
* {@link DataType#asConvertedDataType(Class, Function, Function)}, this
* method allows for creating a derived field expression with an ad-hoc data
* type converter for single query usage.
*
* @param <U> The user type.
* @param converter The converter to be applied on any operations using this
* field.
* @return A derived field expression using a new converter.
*/
<U> Field<U> convert(
Class<U> toType,
Function<? super T, ? extends U> from,
Function<? super U, ? extends T> to
);
/**
* The name of the field.
* <p>

View File

@ -37,6 +37,8 @@
*/
package org.jooq;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
/**
@ -61,7 +63,7 @@ import org.jetbrains.annotations.Nullable;
* @author Lukas Eder
*/
@FunctionalInterface
public interface RecordMapper<R extends Record, E> {
public interface RecordMapper<R extends Record, E> extends Function<R, E> {
/**
* Map a record into a POJO.
@ -72,4 +74,9 @@ public interface RecordMapper<R extends Record, E> {
*/
@Nullable
E map(R record);
@Override
default E apply(R record) {
return map(record);
}
}

View File

@ -85,6 +85,7 @@ import org.jooq.Comment;
import org.jooq.Comparator;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.DatePart;
import org.jooq.Field;
@ -185,6 +186,25 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
// XXX: API
// ------------------------------------------------------------------------
@Override
public final <U> Field<U> convert(Binding<T, U> binding) {
return CustomField.of(getQualifiedName(), getDataType().asConvertedDataType(binding), c -> c.visit(this));
}
@Override
public final <U> Field<U> convert(Converter<T, U> converter) {
return CustomField.of(getQualifiedName(), getDataType().asConvertedDataType(converter), c -> c.visit(this));
}
@Override
public final <U> Field<U> convert(
Class<U> toType,
Function<? super T, ? extends U> from,
Function<? super U, ? extends T> to
) {
return CustomField.of(getQualifiedName(), getDataType().asConvertedDataType(toType, from, to), c -> c.visit(this));
}
@Override
public final Field<T> as(String alias) {
return as(DSL.name(alias));