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 >
