Github user martin-g commented on a diff in the pull request:

    https://github.com/apache/wicket/pull/168#discussion_r60850114
  
    --- Diff: wicket-core/src/main/java/org/apache/wicket/model/IModel.java ---
    @@ -73,187 +73,264 @@
         *
         * @param object
         *            The model object
    -     * @throws UnsupportedOperationException unless overridden
    +    * @throws UnsupportedOperationException
    +    *             unless overridden
         */
        default void setObject(final T object)
        {
    -           throw new UnsupportedOperationException("Override this method 
to support setObject(Object)");
    +           throw new UnsupportedOperationException(
    +                   "Override this method to support setObject(Object)");
        }
     
        @Override
    -   default void detach() {}
    +   default void detach()
    +   {
    +   }
     
        /**
    -    * Returns a IModel checking whether the predicate holds for the
    -    * contained object, if it is not null. If the predicate doesn't 
evaluate
    -    * to true, the contained object will be null.
    +    * Returns a IModel checking whether the predicate holds for the 
contained object, if it is not
    +    * null. If the predicate doesn't evaluate to true, the contained 
object will be null.
         *
    -    * @param predicate a predicate to be used for testing the contained 
object
    +    * @param predicate
    +    *            a predicate to be used for testing the contained object
         * @return a new IModel
         */
    -   default IModel<T> filter(WicketFunction<? super T, Boolean> predicate) {
    -           return (IModel<T>) () -> {
    +   default IModel<T> filter(WicketFunction<? super T, Boolean> predicate)
    +   {
    +           return (IModel<T>)() -> {
                        T object = IModel.this.getObject();
    -                   if (object != null && predicate.apply(object)) {
    +                   if (object != null && predicate.apply(object))
    +                   {
                                return object;
                        }
    -                   else {
    +                   else
    +                   {
                                return null;
                        }
                };
        }
     
        /**
    -    * Returns a IModel applying the given mapper to
    -    * the contained object, if it is not NULL.
    +    * Returns a IModel applying the given mapper to the contained object, 
if it is not NULL.
         *
    -    * @param <R> the new type of the contained object
    -    * @param mapper a mapper, to be applied to the contained object
    +    * @param <R>
    +    *            the new type of the contained object
    +    * @param mapper
    +    *            a mapper, to be applied to the contained object
         * @return a new IModel
         */
    -   default <R> IModel<R> map(WicketFunction<? super T, R> mapper) {
    -           return (IModel<R>) () -> {
    +   default <R> IModel<R> map(WicketFunction<? super T, R> mapper)
    +   {
    +           return (IModel<R>)() -> {
                        T object = IModel.this.getObject();
    -                   if (object == null) {
    +                   if (object == null)
    +                   {
                                return null;
                        }
    -                   else {
    +                   else
    +                   {
                                return mapper.apply(object);
                        }
                };
        }
     
        /**
    -    * Returns a IModel applying the given combining function to
    -    * the contained object of this and the given other model, if they are 
not null.
    +    * Returns a IModel applying the given combining function to the 
contained object of this and
    +    * the given other model, if they are not null.
         *
    -    * @param <R> the resulting type
    -    * @param <U> the other models type
    -    * @param combine a function combining this and the others object to
    -    * a result.
    -    * @param other another model to be combined with this one
    +    * @param <R>
    +    *            the resulting type
    +    * @param <U>
    +    *            the other models type
    +    * @param combine
    +    *            a function combining this and the others object to a 
result.
    +    * @param other
    +    *            another model to be combined with this one
         * @return a new IModel
         */
    -   default <R, U> IModel<R> mapWith(WicketBiFunction<? super T, ? super U, 
R> combine, IModel<U> other) {
    -           return (IModel<R>) () -> {
    +   default <R, U> IModel<R> mapWith(WicketBiFunction<? super T, ? super U, 
R> combine,
    +           IModel<U> other)
    +   {
    +           return (IModel<R>)() -> {
                        T t = IModel.this.getObject();
                        U u = other.getObject();
    -                   if (t != null && u != null) {
    +                   if (t != null && u != null)
    +                   {
                                return combine.apply(t, u);
                        }
    -                   else {
    +                   else
    +                   {
                                return null;
                        }
                };
        }
     
        /**
    -    * Returns a IModel applying the given mapper to the contained
    -    * object, if it is not NULL.
    +    * Returns a IModel applying the given mapper to the contained object, 
if it is not NULL.
         *
    -    * @param <R> the new type of the contained object
    -    * @param mapper a mapper, to be applied to the contained object
    +    * @param <R>
    +    *            the new type of the contained object
    +    * @param mapper
    +    *            a mapper, to be applied to the contained object
         * @return a new IModel
         */
    -   default <R> IModel<R> flatMap(WicketFunction<? super T, IModel<R>> 
mapper) {
    -           T object = IModel.this.getObject();
    -           return mapper.apply(object);
    +   default <R> IModel<R> flatMap(WicketFunction<? super T, IModel<R>> 
mapper)
    +   {
    +           return new IModel<R>()
    +           {
    +
    +                   @Override
    +                   public R getObject()
    +                   {
    +                           T object = IModel.this.getObject();
    +                           if (object != null)
    +                           {
    +                                   return mapper.apply(object).getObject();
    +                           }
    +                           else
    +                           {
    +                                   return null;
    +                           }
    +                   }
    +
    +                   @Override
    +                   public void setObject(R object)
    +                   {
    +                           T modelObject = IModel.this.getObject();
    +                           if (modelObject != null)
    +                           {
    +                                   
mapper.apply(modelObject).setObject(object);
    +                           }
    +                   }
    +
    +                   @Override
    +                   public void detach()
    +                   {
    +                           T object = IModel.this.getObject();
    +                           if (object != null)
    +                           {
    +                                   IModel<R> model = mapper.apply(object);
    --- End diff --
    
    The result of `mapper.apply()` is used without checks in the other methods. 
I think they should have it too.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to