I'll use Emond's suggestion in LambdaModel. My instinct says folks will start looking there for inspiration of Java 8 lambda's and models.
Martijn On Fri, Nov 11, 2016 at 9:08 AM, Emond Papegaaij <[email protected]> wrote: > Can't this method be made like this (perhaps on IModel?): > /** > * Create a read-only {@link IModel}. Usage: > * <pre> > * {@code > * LambdaModel.of(person::getName) > * } > * </pre> > * Note that {@link IModel} is a {@code FunctionalInterface} and you can > * also use a lambda directly as a model. > * > * @param getter used to get value > * @return model > * > * @param <T> model object type > */ > public static <T> IModel<T> of(SerializableSupplier<T> getter) > { > return getter::get; > } > > Emond > > > On donderdag 10 november 2016 21:40:58 CET Sven Meier wrote: >> If Martijn needs this, I'd prefer just to add another factory method: >> >> /** >> * Create a read-only {@link LambdaModel}. Usage: >> * <pre> >> * {@code >> * LambdaModel.of(person::getName) >> * } >> * </pre> >> * >> * @param getter used to get value >> * @return model >> * >> * @param <T> model object type >> */ >> public static <T> IModel<T> of(SerializableSupplier<T> getter) >> { >> return new LambdaModel<>(getter, (t) -> { throw new >> UnsupportedOperationException(); }); >> } >> >> And the setter should throw the exception. >> >> Have fun >> Sven >> >> Am 10.11.2016 um 21:34 schrieb Martin Grigorov: >> > I think there is no need of this change. >> > But if others believe it's needed then #setObject should throw an >> > exception >> > as in IModel. >> > >> > On Nov 10, 2016 10:24 PM, "Martijn Dashorst" <[email protected]> >> > >> > wrote: >> >> So are we pro/against expanding LambdaModel for support of a read only >> >> variant? >> >> >> >> Reasons against: >> >> - not necessary with IModel as a functional interface >> >> - multiple ways to do things >> >> >> >> Reasons pro: >> >> - IModel version may not be as clear as a Lambda model >> >> - IModel version may not be as obvious a choice (years of experience >> >> >> >> with Java < 8 and Wicket < 8) >> >> >> >> Proposed patch: >> >> >> >> diff --git a/wicket-core/src/main/java/org/apache/wicket/model/ >> >> LambdaModel.java >> >> b/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java >> >> index 8fb9d6a..ece4fe5 100644 >> >> --- a/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java >> >> +++ b/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java >> >> @@ -53,6 +53,19 @@ public class LambdaModel<T> implements IModel<T> >> >> >> >> this.setter = Args.notNull(setter, "setter"); >> >> } >> >> >> >> + /** >> >> + * Construct the model, using the given supplier and consumer as >> >> + * implementation for getObject and setObject. >> >> + * >> >> + * @param getter Used for the getObject() method. >> >> + * @param setter Used for the setObject(T object) method. >> >> + */ >> >> + public LambdaModel(SerializableSupplier<T> getter) >> >> + { >> >> + this.getter = Args.notNull(getter, "getter"); >> >> + this.setter = null; >> >> + } >> >> + >> >> >> >> @Override >> >> public T getObject() >> >> { >> >> >> >> @@ -62,7 +75,8 @@ public class LambdaModel<T> implements IModel<T> >> >> >> >> @Override >> >> public void setObject(T t) >> >> { >> >> >> >> - setter.accept(t); >> >> + if(setter != null) >> >> + setter.accept(t); >> >> >> >> } >> >> >> >> @Override >> >> >> >> @@ -114,6 +128,24 @@ public class LambdaModel<T> implements IModel<T> >> >> >> >> } >> >> >> >> /** >> >> >> >> + * Create a read only {@link LambdaModel}. Usage: >> >> + * <pre> >> >> + * {@code >> >> + * LambdaModel.of(person::getName) >> >> + * } >> >> + * </pre> >> >> + * >> >> + * @param getter used to get value >> >> + * @return model >> >> + * >> >> + * @param <T> model object type >> >> + */ >> >> + public static <T> IModel<T> of(SerializableSupplier<T> getter) >> >> + { >> >> + return new LambdaModel<>(getter); >> >> + } >> >> + >> >> + /** >> >> >> >> * Create a {@link LambdaModel} for a given target. Usage: >> >> * <pre> >> >> * {@code > > -- Become a Wicket expert, learn from the best: http://wicketinaction.com
