org.bbottema.javareflection.valueconverter.ValueFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-reflection Show documentation
Show all versions of java-reflection Show documentation
Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions
package org.bbottema.javareflection.valueconverter;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.bbottema.javareflection.util.Function;
/**
* Can be used to provide optional user converters. User converters also act as intermediate converters, ie. if a user converter can go to
* int
, double
is automatically supported as well as common conversion.
*/
public interface ValueFunction {
@NonNull Class getFromType();
@NonNull Class getTargetType();
@NonNull T convertValue(@NonNull F value);
/**
* Helper class to quickly define a {@link ValueFunction} from a {@link Function}.
*/
@Getter
@RequiredArgsConstructor
@ToString(onlyExplicitlyIncluded = true)
class ValueFunctionImpl implements ValueFunction {
@NonNull @ToString.Include protected final Class fromType;
@NonNull @ToString.Include protected final Class targetType;
@NonNull private final Function converter;
@NonNull @Override
public final T convertValue(@NonNull F value) {
return converter.apply(value);
}
}
}