com.databasesandlife.util.wicket.LambdaDisplayValueChoiceRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
The newest version!
package com.databasesandlife.util.wicket;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.danekja.java.util.function.serializable.SerializableFunction;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;
public class LambdaDisplayValueChoiceRenderer implements IChoiceRenderer {
public final @Nonnull SerializableFunction supplier;
public LambdaDisplayValueChoiceRenderer(@Nonnull SerializableFunction supplier) {
this.supplier = supplier;
}
@Override public String getDisplayValue(@CheckForNull T object) {
if (object == null) return null;
else return supplier.apply(object);
}
@Override public String getIdValue(T object, int index) {
return Integer.toString(index);
}
@Override public T getObject(String id, IModel extends List extends T>> choices) {
try {
return choices.getObject().get(Integer.parseInt(id));
}
catch (NumberFormatException e) {
// For example, user selected "Please choose" which returns id ""
return null;
}
}
}