org.wicketstuff.lambda.model.LambdaModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wicketstuff-lambda Show documentation
Show all versions of wicketstuff-lambda Show documentation
WicketStuff Lambda is a collection of Java 8 functionality.
The newest version!
package org.wicketstuff.lambda.model;
import java.util.Objects;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.wicketstuff.lambda.SerializableBiConsumer;
import org.wicketstuff.lambda.SerializableBiFunction;
import org.wicketstuff.lambda.SerializableFunction;
/**
* {@link LoadableDetachableModel} that wraps another {@link IModel} and applies
* a {@link SerializableFunction} to the wrapped model to get the value for the
* {@link #load()} function.
*
* An optional {@link SerializableBiConsumer} can be provided that will be
* called with the object of the wrapped model and the new object value when the
* {@link #setObject()} method is called.
*
* @param
* - type of the wrapped {@link IModel}
* @param
* - type of the {@link LambdaModel}
* @deprecated Use {@link org.apache.wicket.model.LambdaModel} and {@link LoadableDetachableModel#of(org.danekja.java.util.function.serializable.SerializableSupplier)} instead
*/
public class LambdaModel extends LoadableDetachableModel {
private static final long serialVersionUID = 1L;
private IModel wrappedModel;
private SerializableFunction loadHandler;
private SerializableBiConsumer setObjectHandler;
/**
* @param wrappedModel
* {@link IModel} that wraps the value which will be applied to
* the {@code loadHandler} {@link SerializableFunction}
* @param loadHandler
* {@link SerializableFunction} that is invoked to provide the
* value for the {@code #load()} method
*/
public LambdaModel(IModel wrappedModel, SerializableFunction loadHandler) {
this(wrappedModel, loadHandler, (t, r) -> { throw new UnsupportedOperationException("No setter specified"); });
}
/**
* @param wrappedModel
* {@link IModel} that wraps the value which will be applied to
* the {@code loadHandler} {@link SerializableFunction}
* @param loadHandler
* {@link SerializableFunction} that is invoked to provide the
* value for the {@code #load()} method
* @param setObjectHandler
* {@link SerializableBiFunction} that is invoked when the
* {@code #setObject()} method is called
*/
public LambdaModel(IModel wrappedModel, SerializableFunction loaderFunction,
SerializableBiConsumer setObjectHandler) {
super();
Objects.requireNonNull(wrappedModel);
Objects.requireNonNull(loaderFunction);
Objects.requireNonNull(setObjectHandler);
this.wrappedModel = wrappedModel;
this.loadHandler = loaderFunction;
this.setObjectHandler = setObjectHandler;
}
@Override
protected R load() {
return loadHandler.apply(wrappedModel.getObject());
}
@Override
public void setObject(R r) {
super.setObject(r);
setObjectHandler.accept(wrappedModel.getObject(), r);
}
@Override
protected void onDetach() {
super.onDetach();
wrappedModel.detach();
}
}