You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by Juergen Donnerstag <ju...@gmail.com> on 2009/04/15 08:36:56 UTC

Improvement to Component.getBehaviors()

I'll to suggest the following improvement (generics)

Today:
protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
Which leads to code like WindowClosedBehavior behavior =
(WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
Though we provide the type as parameter, we still have to cast the return value

That can improved as follows
protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
which than allows for (no more extra casting)
WindowClosedBehavior behavior = getBehaviors(WindowClosedBehavior.class).get(0);

Regarding the implemention nothing changes except for types
	@SuppressWarnings("unchecked")
	protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
	{
		List<IBehavior> behaviors = getBehaviorsRawList();
		if (behaviors == null)
		{
			return Collections.emptyList();
		}

		List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
		for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
		{
			Object behavior = i.next();
			if (behavior != null && (type == null ||
type.isAssignableFrom(behavior.getClass())))
			{
				subset.add((M)behavior);
			}
		}
		return Collections.unmodifiableList(subset);
	}

Making that change didn't require any other change to any other Wicket
code incl tests

Any objections?

Juergen

Re: Improvement to Component.getBehaviors()

Posted by Igor Vaynberg <ig...@gmail.com>.
+1

-igor

On Tue, Apr 14, 2009 at 11:36 PM, Juergen Donnerstag
<ju...@gmail.com> wrote:
> I'll to suggest the following improvement (generics)
>
> Today:
> protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
> Which leads to code like WindowClosedBehavior behavior =
> (WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
> Though we provide the type as parameter, we still have to cast the return value
>
> That can improved as follows
> protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
> which than allows for (no more extra casting)
> WindowClosedBehavior behavior = getBehaviors(WindowClosedBehavior.class).get(0);
>
> Regarding the implemention nothing changes except for types
>        @SuppressWarnings("unchecked")
>        protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>        {
>                List<IBehavior> behaviors = getBehaviorsRawList();
>                if (behaviors == null)
>                {
>                        return Collections.emptyList();
>                }
>
>                List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
>                for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
>                {
>                        Object behavior = i.next();
>                        if (behavior != null && (type == null ||
> type.isAssignableFrom(behavior.getClass())))
>                        {
>                                subset.add((M)behavior);
>                        }
>                }
>                return Collections.unmodifiableList(subset);
>        }
>
> Making that change didn't require any other change to any other Wicket
> code incl tests
>
> Any objections?
>
> Juergen
>

Re: Improvement to Component.getBehaviors()

Posted by Juergen Donnerstag <ju...@gmail.com>.
subset.add(type.cast(behavior));  unfortunately doesn't work because
type can be null. See javadoc for type parameter. I guess that can
only be changed in 1.5 though a convience method getBehaviors() -
without any parameters - already exists.

Juergen

On Wed, Apr 15, 2009 at 9:17 AM, Francis De Brabandere
<fr...@gmail.com> wrote:
> subset.add((M)behavior);
>
> can be replaced by
> subset.add(type.cast(behavior));
>
> and should help to get rid of that @SuppressWarnings, but then this
> might decrease performance (not sure about that)
>
> On Wed, Apr 15, 2009 at 9:05 AM, Johan Compagner <jc...@gmail.com> wrote:
>> Ohh wait, i didnt read the email fully, this should work find, i just
>> saw the supressed warning..
>>
>> But that change would be fine
>>
>> On 15/04/2009, Johan Compagner <jc...@gmail.com> wrote:
>>> Hmm dont know about that.
>>> Because this would mean that class cast exceptions can easily be get
>>> even when from the outside you use generics all the way.
>>> How does a user know that the first one is of that type? Maybe we
>>> could add our own first our self.. This is just bad code if you ask me
>>>
>>> On 15/04/2009, Juergen Donnerstag <ju...@gmail.com> wrote:
>>>> I'll to suggest the following improvement (generics)
>>>>
>>>> Today:
>>>> protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
>>>> Which leads to code like WindowClosedBehavior behavior =
>>>> (WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
>>>> Though we provide the type as parameter, we still have to cast the return
>>>> value
>>>>
>>>> That can improved as follows
>>>> protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>>>> which than allows for (no more extra casting)
>>>> WindowClosedBehavior behavior =
>>>> getBehaviors(WindowClosedBehavior.class).get(0);
>>>>
>>>> Regarding the implemention nothing changes except for types
>>>>      @SuppressWarnings("unchecked")
>>>>      protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>>>>      {
>>>>              List<IBehavior> behaviors = getBehaviorsRawList();
>>>>              if (behaviors == null)
>>>>              {
>>>>                      return Collections.emptyList();
>>>>              }
>>>>
>>>>              List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
>>>>              for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
>>>>              {
>>>>                      Object behavior = i.next();
>>>>                      if (behavior != null && (type == null ||
>>>> type.isAssignableFrom(behavior.getClass())))
>>>>                      {
>>>>                              subset.add((M)behavior);
>>>>                      }
>>>>              }
>>>>              return Collections.unmodifiableList(subset);
>>>>      }
>>>>
>>>> Making that change didn't require any other change to any other Wicket
>>>> code incl tests
>>>>
>>>> Any objections?
>>>>
>>>> Juergen
>>>>
>>>
>>
>
>
>
> --
> http://www.somatik.be
> Microsoft gives you windows, Linux gives you the whole house.
>

Re: Improvement to Component.getBehaviors()

Posted by Francis De Brabandere <fr...@gmail.com>.
subset.add((M)behavior);

can be replaced by
subset.add(type.cast(behavior));

and should help to get rid of that @SuppressWarnings, but then this
might decrease performance (not sure about that)

On Wed, Apr 15, 2009 at 9:05 AM, Johan Compagner <jc...@gmail.com> wrote:
> Ohh wait, i didnt read the email fully, this should work find, i just
> saw the supressed warning..
>
> But that change would be fine
>
> On 15/04/2009, Johan Compagner <jc...@gmail.com> wrote:
>> Hmm dont know about that.
>> Because this would mean that class cast exceptions can easily be get
>> even when from the outside you use generics all the way.
>> How does a user know that the first one is of that type? Maybe we
>> could add our own first our self.. This is just bad code if you ask me
>>
>> On 15/04/2009, Juergen Donnerstag <ju...@gmail.com> wrote:
>>> I'll to suggest the following improvement (generics)
>>>
>>> Today:
>>> protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
>>> Which leads to code like WindowClosedBehavior behavior =
>>> (WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
>>> Though we provide the type as parameter, we still have to cast the return
>>> value
>>>
>>> That can improved as follows
>>> protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>>> which than allows for (no more extra casting)
>>> WindowClosedBehavior behavior =
>>> getBehaviors(WindowClosedBehavior.class).get(0);
>>>
>>> Regarding the implemention nothing changes except for types
>>>      @SuppressWarnings("unchecked")
>>>      protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>>>      {
>>>              List<IBehavior> behaviors = getBehaviorsRawList();
>>>              if (behaviors == null)
>>>              {
>>>                      return Collections.emptyList();
>>>              }
>>>
>>>              List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
>>>              for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
>>>              {
>>>                      Object behavior = i.next();
>>>                      if (behavior != null && (type == null ||
>>> type.isAssignableFrom(behavior.getClass())))
>>>                      {
>>>                              subset.add((M)behavior);
>>>                      }
>>>              }
>>>              return Collections.unmodifiableList(subset);
>>>      }
>>>
>>> Making that change didn't require any other change to any other Wicket
>>> code incl tests
>>>
>>> Any objections?
>>>
>>> Juergen
>>>
>>
>



-- 
http://www.somatik.be
Microsoft gives you windows, Linux gives you the whole house.

Re: Improvement to Component.getBehaviors()

Posted by Johan Compagner <jc...@gmail.com>.
Ohh wait, i didnt read the email fully, this should work find, i just
saw the supressed warning..

But that change would be fine

On 15/04/2009, Johan Compagner <jc...@gmail.com> wrote:
> Hmm dont know about that.
> Because this would mean that class cast exceptions can easily be get
> even when from the outside you use generics all the way.
> How does a user know that the first one is of that type? Maybe we
> could add our own first our self.. This is just bad code if you ask me
>
> On 15/04/2009, Juergen Donnerstag <ju...@gmail.com> wrote:
>> I'll to suggest the following improvement (generics)
>>
>> Today:
>> protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
>> Which leads to code like WindowClosedBehavior behavior =
>> (WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
>> Though we provide the type as parameter, we still have to cast the return
>> value
>>
>> That can improved as follows
>> protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>> which than allows for (no more extra casting)
>> WindowClosedBehavior behavior =
>> getBehaviors(WindowClosedBehavior.class).get(0);
>>
>> Regarding the implemention nothing changes except for types
>> 	@SuppressWarnings("unchecked")
>> 	protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
>> 	{
>> 		List<IBehavior> behaviors = getBehaviorsRawList();
>> 		if (behaviors == null)
>> 		{
>> 			return Collections.emptyList();
>> 		}
>>
>> 		List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
>> 		for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
>> 		{
>> 			Object behavior = i.next();
>> 			if (behavior != null && (type == null ||
>> type.isAssignableFrom(behavior.getClass())))
>> 			{
>> 				subset.add((M)behavior);
>> 			}
>> 		}
>> 		return Collections.unmodifiableList(subset);
>> 	}
>>
>> Making that change didn't require any other change to any other Wicket
>> code incl tests
>>
>> Any objections?
>>
>> Juergen
>>
>

Re: Improvement to Component.getBehaviors()

Posted by Johan Compagner <jc...@gmail.com>.
Hmm dont know about that.
Because this would mean that class cast exceptions can easily be get
even when from the outside you use generics all the way.
How does a user know that the first one is of that type? Maybe we
could add our own first our self.. This is just bad code if you ask me

On 15/04/2009, Juergen Donnerstag <ju...@gmail.com> wrote:
> I'll to suggest the following improvement (generics)
>
> Today:
> protected List<IBehavior> getBehvavior(Class<? extends IBehavior> type)
> Which leads to code like WindowClosedBehavior behavior =
> (WindowClosedBehavior)getBehaviors(WindowClosedBehavior.class).get(0);
> Though we provide the type as parameter, we still have to cast the return
> value
>
> That can improved as follows
> protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
> which than allows for (no more extra casting)
> WindowClosedBehavior behavior =
> getBehaviors(WindowClosedBehavior.class).get(0);
>
> Regarding the implemention nothing changes except for types
> 	@SuppressWarnings("unchecked")
> 	protected <M extends IBehavior> List<M> getBehaviors(Class<M> type)
> 	{
> 		List<IBehavior> behaviors = getBehaviorsRawList();
> 		if (behaviors == null)
> 		{
> 			return Collections.emptyList();
> 		}
>
> 		List<M> subset = new ArrayList<M>(behaviors.size()); // avoid growing
> 		for (Iterator<IBehavior> i = behaviors.iterator(); i.hasNext();)
> 		{
> 			Object behavior = i.next();
> 			if (behavior != null && (type == null ||
> type.isAssignableFrom(behavior.getClass())))
> 			{
> 				subset.add((M)behavior);
> 			}
> 		}
> 		return Collections.unmodifiableList(subset);
> 	}
>
> Making that change didn't require any other change to any other Wicket
> code incl tests
>
> Any objections?
>
> Juergen
>