You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by noobymatze <gi...@git.apache.org> on 2016/04/24 21:05:19 UTC

[GitHub] wicket pull request: idea for lazy flatMap

GitHub user noobymatze opened a pull request:

    https://github.com/apache/wicket/pull/168

    idea for lazy flatMap

    I fixed the formatting and provided an idea for implementing flatMap in a lazy fashion.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/noobymatze/wicket monad-model

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/wicket/pull/168.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #168
    
----
commit 095cb327c0b952bb734e1ce1bfa1da41abc69426
Author: Matthias Metzger <no...@yahoo.de>
Date:   2016-04-24T18:37:44Z

    fix formatting in IModel

commit 4348962480949f61574a5172ca6b7f7749ddc5ff
Author: Matthias Metzger <no...@yahoo.de>
Date:   2016-04-24T18:42:40Z

    implement idea for lazy flatMap in IModel
    
    This approach has two problems though:
    
    1. For every call of getObject, setObject and detach the whole
       model chain will be evaluated again.
    2. It's the same problem like with the implementation before.
       When the setObject is called and any value in the retrieval
       has been null, no object will be set, which is unlike the
       PropertyModel, which would initialize any values being null.

commit 3719da912cea48e2a9f8ec4de4672379a93954de
Author: Matthias Metzger <no...@yahoo.de>
Date:   2016-04-24T18:58:58Z

    fix wrong formatting

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] wicket pull request: idea for lazy flatMap

Posted by noobymatze <gi...@git.apache.org>.
Github user noobymatze commented on the pull request:

    https://github.com/apache/wicket/pull/168#issuecomment-214025607
  
    Awesome! Should hopefully be fine now!
    
    Thanks again for being so open to the ideas!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] wicket pull request: idea for lazy flatMap

Posted by martin-g <gi...@git.apache.org>.
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] wicket pull request: idea for lazy flatMap

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/wicket/pull/168


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] wicket pull request: idea for lazy flatMap

Posted by martin-g <gi...@git.apache.org>.
Github user martin-g commented on the pull request:

    https://github.com/apache/wicket/pull/168#issuecomment-214024735
  
    Thanks for the PR, Matthias!
    
    Other Wicket devs seems to liked the idea of having those methods in IModel, so thanks for formatting the code too! :-)
    
    This `#flatMap()` impl looks good to me!
    Please add the checks for null to set/getObject and I'll merge it!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---