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 <[email protected]> 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 <[email protected]> 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 <[email protected]> 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.
