You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by Martin Grigorov <mg...@apache.org> on 2016/06/15 10:45:53 UTC

Bad usage of j.u.function.Consumer in Link#onClick() static method

Hi,

I've started refactoring wicket-examples to use more of the Java 8
functionalities in Wicket and
I've found a problem with Link#onClick(String, WicketConsumer<Void>) method.
See
https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e
Here I needed to use "(ignoreme)" as an input for the lambda to make it
compileable.
The problem is that Java 8 doesn't provide Consumer without an input.
Here is a discussion about this:
http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing

Shall we introduce our own Consumer0/Procedure/Command or use
java.lang.Runnable in the API ?


Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

Re: Bad usage of j.u.function.Consumer in Link#onClick() static method

Posted by Martijn Dashorst <ma...@gmail.com>.
Runnable sounds good. But that would be our own "SerializableRunner"
and then the benefit of using java.lang.Runnable is moot.

Isn't it better to introduce a couple of functional interfaces for this?

@FunctionalInterface
public interface OnAjaxEvent extends Serializable {
    public void onEvent(AjaxRequestTarget target);
}

@FunctionalInterface
public interface OnFallbackAjaxEvent extends Serializable {
    public void onEvent(Optional<AjaxRequestTarget> target);
}

@FunctionalInterface
public interface OnEvent extends Serializable {
    public void onEvent();
}

Martijn


On Wed, Jun 15, 2016 at 12:45 PM, Martin Grigorov <mg...@apache.org> wrote:
> Hi,
>
> I've started refactoring wicket-examples to use more of the Java 8
> functionalities in Wicket and
> I've found a problem with Link#onClick(String, WicketConsumer<Void>) method.
> See
> https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e
> Here I needed to use "(ignoreme)" as an input for the lambda to make it
> compileable.
> The problem is that Java 8 doesn't provide Consumer without an input.
> Here is a discussion about this:
> http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing
>
> Shall we introduce our own Consumer0/Procedure/Command or use
> java.lang.Runnable in the API ?
>
>
> Martin Grigorov
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov



-- 
Become a Wicket expert, learn from the best: http://wicketinaction.com

Re: Bad usage of j.u.function.Consumer in Link#onClick() static method

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Yesterday I've prepared branch 'special-functional-interfaces' with
Martijn's suggestion.
But my Internet connection in my hotel didn't allow to push it. Even my
email is still in the outbox.
I think the impl looks good!

I'll check Sven's work later and either propose to merge the ideas or I'll
drop my branch.

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Jun 16, 2016 at 8:19 AM, Sven Meier <sv...@meiers.net> wrote:

> Actually WorldClockPage shows a nice usage of lambdas where a single
> argument makes it easy to forward the click:
>
>         add(AjaxLink.onClick("stop", timer::stop));
>
> So I've added back a factory method for a (non-Bi)Consumer, so now we have
> two:
>
>     static <T> AjaxLink<T> onClick(String id,
> WicketConsumer<AjaxRequestTarget> onClick)
>     static <T> AjaxLink<T> onClick(String id,
> WicketBiConsumer<AjaxLink<T>, AjaxRequestTarget> onClick)
>
> Hopefully we don't have to add all sorts of argument combinations :/.
>
> Sven
>
>
>
> On 16.06.2016 08:06, Sven Meier wrote:
>
>> Hi all,
>>
>> IMHO all lambdas used for component creation would benefit from getting a
>> reference to the actual component, i.e.:
>>
>> static <T> AjaxLink<T> ajaxLink(String id, WicketBiConsumer<AjaxLink<T>,
>> AjaxRequestTarget> onClick)
>> static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton,
>> AjaxRequestTarget> onSubmit)
>> static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton,
>> AjaxRequestTarget> onSubmit, WicketBiConsumer<AjaxButton,
>> AjaxRequestTarget> onError)
>> static AjaxCheckBox ajaxCheckBox(String id,
>> WicketBiConsumer<AjaxCheckBox, AjaxRequestTarget> onUpdate)
>> static AjaxSubmitLink ajaxSubmitLink(String id,
>> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit)
>> static AjaxSubmitLink ajaxSubmitLink(String id,
>> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit,
>> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onError)
>> static <T> Link<T> link(String id, WicketConsumer<Link<T>> onClick)
>>
>> Their usage is still nice to read and (with the help of your IDE)
>> manageable to write:
>>
>>         add(Link.onClick("adminAnnotInternalLink", link ->
>> setResponsePage(new AdminAnnotationsInternalPage("bar"))));
>>
>> I've changed the factory methods, let me know if you don't like it.
>>
>> Sven
>>
>>
>>
>> On 15.06.2016 19:06, Sven Meier wrote:
>>
>>> Hi,
>>>
>>> for this 'corner case' we might just as well pass in the link as
>>> argument:
>>>
>>>     public static <T> Link<T> onClick(String id, WicketConsumer<Link<T>>
>>> onClick)
>>>
>>>     add(Link.onClick("adminAnnotInternalLink", link -> ...));
>>>
>>> This might be handy if you want to do something with the link (update,
>>> replace, etc.)
>>>
>>> Have fun
>>> Sven
>>>
>>>
>>> On 15.06.2016 12:45, Martin Grigorov wrote:
>>>
>>>> Hi,
>>>>
>>>> I've started refactoring wicket-examples to use more of the Java 8
>>>> functionalities in Wicket and
>>>> I've found a problem with Link#onClick(String, WicketConsumer<Void>)
>>>> method.
>>>> See
>>>>
>>>> https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e
>>>> Here I needed to use "(ignoreme)" as an input for the lambda to make it
>>>> compileable.
>>>> The problem is that Java 8 doesn't provide Consumer without an input.
>>>> Here is a discussion about this:
>>>>
>>>> http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing
>>>>
>>>> Shall we introduce our own Consumer0/Procedure/Command or use
>>>> java.lang.Runnable in the API ?
>>>>
>>>>
>>>> Martin Grigorov
>>>> Wicket Training and Consulting
>>>> https://twitter.com/mtgrigorov
>>>>
>>>>
>>>
>>
>

Re: Bad usage of j.u.function.Consumer in Link#onClick() static method

Posted by Sven Meier <sv...@meiers.net>.
Actually WorldClockPage shows a nice usage of lambdas where a single 
argument makes it easy to forward the click:

         add(AjaxLink.onClick("stop", timer::stop));

So I've added back a factory method for a (non-Bi)Consumer, so now we 
have two:

     static <T> AjaxLink<T> onClick(String id, 
WicketConsumer<AjaxRequestTarget> onClick)
     static <T> AjaxLink<T> onClick(String id, 
WicketBiConsumer<AjaxLink<T>, AjaxRequestTarget> onClick)

Hopefully we don't have to add all sorts of argument combinations :/.

Sven


On 16.06.2016 08:06, Sven Meier wrote:
> Hi all,
>
> IMHO all lambdas used for component creation would benefit from 
> getting a reference to the actual component, i.e.:
>
> static <T> AjaxLink<T> ajaxLink(String id, 
> WicketBiConsumer<AjaxLink<T>, AjaxRequestTarget> onClick)
> static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton, 
> AjaxRequestTarget> onSubmit)
> static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton, 
> AjaxRequestTarget> onSubmit, WicketBiConsumer<AjaxButton, 
> AjaxRequestTarget> onError)
> static AjaxCheckBox ajaxCheckBox(String id, 
> WicketBiConsumer<AjaxCheckBox, AjaxRequestTarget> onUpdate)
> static AjaxSubmitLink ajaxSubmitLink(String id, 
> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit)
> static AjaxSubmitLink ajaxSubmitLink(String id, 
> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit, 
> WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onError)
> static <T> Link<T> link(String id, WicketConsumer<Link<T>> onClick)
>
> Their usage is still nice to read and (with the help of your IDE) 
> manageable to write:
>
>         add(Link.onClick("adminAnnotInternalLink", link -> 
> setResponsePage(new AdminAnnotationsInternalPage("bar"))));
>
> I've changed the factory methods, let me know if you don't like it.
>
> Sven
>
>
>
> On 15.06.2016 19:06, Sven Meier wrote:
>> Hi,
>>
>> for this 'corner case' we might just as well pass in the link as 
>> argument:
>>
>>     public static <T> Link<T> onClick(String id, 
>> WicketConsumer<Link<T>> onClick)
>>
>>     add(Link.onClick("adminAnnotInternalLink", link -> ...));
>>
>> This might be handy if you want to do something with the link 
>> (update, replace, etc.)
>>
>> Have fun
>> Sven
>>
>>
>> On 15.06.2016 12:45, Martin Grigorov wrote:
>>> Hi,
>>>
>>> I've started refactoring wicket-examples to use more of the Java 8
>>> functionalities in Wicket and
>>> I've found a problem with Link#onClick(String, WicketConsumer<Void>) 
>>> method.
>>> See
>>> https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e 
>>>
>>> Here I needed to use "(ignoreme)" as an input for the lambda to make it
>>> compileable.
>>> The problem is that Java 8 doesn't provide Consumer without an input.
>>> Here is a discussion about this:
>>> http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing 
>>>
>>>
>>> Shall we introduce our own Consumer0/Procedure/Command or use
>>> java.lang.Runnable in the API ?
>>>
>>>
>>> Martin Grigorov
>>> Wicket Training and Consulting
>>> https://twitter.com/mtgrigorov
>>>
>>
>


Re: Bad usage of j.u.function.Consumer in Link#onClick() static method

Posted by Sven Meier <sv...@meiers.net>.
Hi all,

IMHO all lambdas used for component creation would benefit from getting 
a reference to the actual component, i.e.:

static <T> AjaxLink<T> ajaxLink(String id, WicketBiConsumer<AjaxLink<T>, 
AjaxRequestTarget> onClick)
static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton, 
AjaxRequestTarget> onSubmit)
static AjaxButton ajaxButton(String id, WicketBiConsumer<AjaxButton, 
AjaxRequestTarget> onSubmit, WicketBiConsumer<AjaxButton, 
AjaxRequestTarget> onError)
static AjaxCheckBox ajaxCheckBox(String id, 
WicketBiConsumer<AjaxCheckBox, AjaxRequestTarget> onUpdate)
static AjaxSubmitLink ajaxSubmitLink(String id, 
WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit)
static AjaxSubmitLink ajaxSubmitLink(String id, 
WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onSubmit, 
WicketBiConsumer<AjaxSubmitLink, AjaxRequestTarget> onError)
static <T> Link<T> link(String id, WicketConsumer<Link<T>> onClick)

Their usage is still nice to read and (with the help of your IDE) 
manageable to write:

         add(Link.onClick("adminAnnotInternalLink", link -> 
setResponsePage(new AdminAnnotationsInternalPage("bar"))));

I've changed the factory methods, let me know if you don't like it.

Sven



On 15.06.2016 19:06, Sven Meier wrote:
> Hi,
>
> for this 'corner case' we might just as well pass in the link as 
> argument:
>
>     public static <T> Link<T> onClick(String id, 
> WicketConsumer<Link<T>> onClick)
>
>     add(Link.onClick("adminAnnotInternalLink", link -> ...));
>
> This might be handy if you want to do something with the link (update, 
> replace, etc.)
>
> Have fun
> Sven
>
>
> On 15.06.2016 12:45, Martin Grigorov wrote:
>> Hi,
>>
>> I've started refactoring wicket-examples to use more of the Java 8
>> functionalities in Wicket and
>> I've found a problem with Link#onClick(String, WicketConsumer<Void>) 
>> method.
>> See
>> https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e 
>>
>> Here I needed to use "(ignoreme)" as an input for the lambda to make it
>> compileable.
>> The problem is that Java 8 doesn't provide Consumer without an input.
>> Here is a discussion about this:
>> http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing 
>>
>>
>> Shall we introduce our own Consumer0/Procedure/Command or use
>> java.lang.Runnable in the API ?
>>
>>
>> Martin Grigorov
>> Wicket Training and Consulting
>> https://twitter.com/mtgrigorov
>>
>


Re: Bad usage of j.u.function.Consumer in Link#onClick() static method

Posted by Sven Meier <sv...@meiers.net>.
Hi,

for this 'corner case' we might just as well pass in the link as argument:

     public static <T> Link<T> onClick(String id, 
WicketConsumer<Link<T>> onClick)

     add(Link.onClick("adminAnnotInternalLink", link -> ...));

This might be handy if you want to do something with the link (update, 
replace, etc.)

Have fun
Sven


On 15.06.2016 12:45, Martin Grigorov wrote:
> Hi,
>
> I've started refactoring wicket-examples to use more of the Java 8
> functionalities in Wicket and
> I've found a problem with Link#onClick(String, WicketConsumer<Void>) method.
> See
> https://github.com/apache/wicket/commit/5d87f50646d13d858eab69d7892075be8cdb1dbd#diff-0419829765d01bcc9911c318ea9bba4e
> Here I needed to use "(ignoreme)" as an input for the lambda to make it
> compileable.
> The problem is that Java 8 doesn't provide Consumer without an input.
> Here is a discussion about this:
> http://programmers.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing
>
> Shall we introduce our own Consumer0/Procedure/Command or use
> java.lang.Runnable in the API ?
>
>
> Martin Grigorov
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>