Jonas Michel
Jonas Michel

Reputation: 68

Generic Type as generic type parameter

I want to know if I can improve my class, by avoiding the second V in the declaration of my class. Somehow it looks awful.

public abstract class TreeElementAction<T extends TreeNode<V>, V> {

    protected abstract boolean actFor(@Nullable T element);

    public void forEachElementInTree(@Nonnull T rootNode) {
        requireNonNull(rootNode);
        actFor(rootNode);

        Collection<T> children = (Collection<T>) rootNode.getChildren();
        for (T treeNode : children) {
            forEachElementInTree(treeNode);
        }
    }
}

Upvotes: 0

Views: 31

Answers (1)

Andy Turner
Andy Turner

Reputation: 140309

Unless you require V in any of the concrete implementations of this class, you can just drop the type variable, as @PeterLawrey has suggested, and replace it with an unbounded wildcard in TreeNode<V>:

public abstract class TreeElementAction<T extends TreeNode<?>> {

Upvotes: 1

Related Questions