Skip to content

Commit

Permalink
[jOOQ#5398] Add converter constructor Converter.of(Class<T>, Class<U>…
Browse files Browse the repository at this point in the history
…, Function<? super T, ? extends U>, Function<? super U, ? extends T>)
  • Loading branch information
lukaseder committed Jul 6, 2016
1 parent 5df74b0 commit 6aba3ba
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions jOOQ/src/main/java/org/jooq/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package org.jooq;

import java.io.Serializable;
import java.util.function.Function;

import org.jooq.impl.SQLDataType;

Expand Down Expand Up @@ -124,5 +125,52 @@ default <X> Converter<T, X> andThen(Converter<? super U, X> converter) {
return Converters.of(this, converter);
}

/**
* Construct a new converter from functions.
*
* @param <T> the database type
* @param <U> the user type
* @param fromType The database type
* @param toType The user type
* @param from A function converting from T to U
* @param to A function converting from U to T
* @return The converter.
* @see Converter
*/
static <T, U> Converter<T, U> of(
Class<T> fromType,
Class<U> toType,
Function<? super T, ? extends U> from,
Function<? super U, ? extends T> to
) {
return new Converter<T, U>() {

/**
* Generated UID
*/
private static final long serialVersionUID = 8782437631959970693L;

@Override
public final U from(T t) {
return from.apply(t);
}

@Override
public final T to(U u) {
return to.apply(u);
}

@Override
public final Class<T> fromType() {
return fromType;
}

@Override
public final Class<U> toType() {
return toType;
}
};
}


}

0 comments on commit 6aba3ba

Please sign in to comment.