On Thu, Nov 10, 2016 at 9:40 PM, Sven Meier <[email protected]> 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. >
+1 > > 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 >>> >>> >
