4

I know what is the meaning of PECS.

Producer Extends,Consumer Super.

the thing is how would I know if its a consumer or producer?

Also does this code follow the "PECS"

public class Tree<T> {

    //List of branches for this tree
    private List<Tree<? super T>> branch = new ArrayList<Tree<? super T>>();
    public Tree(T t){ this.t = t; }
    public void addBranch(Tree< ? super T> src){ branch.add(src); }
    public Tree<? extends T> getBranch(int branchNum){
        return (Tree<? extends T>) branch.get(branchNum);
    }
    private T t;
}
KyelJmD
  • 4,682
  • 9
  • 54
  • 77

3 Answers3

3
  • Producer refers to the return type of a method.
  • Consumer refers to the parameter type of a method.
David Grant
  • 13,929
  • 3
  • 57
  • 63
3

A nice mnemonic you can use is to imagine returns for extends and accepts for super.

So a Tree<? extends T> reads Tree<? returns T>, which means that you can call the methods in Tree that return T, but not the methods that accept T as an argument type.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I am kinda confused, but with regards with the code. Did I did it right? – KyelJmD Aug 30 '12 at 13:24
  • In the code you show there are no invocations made on the `Tree` argument. Put `Tree` everywhere, I think that's the only appropriate way for your case. – Marko Topolnik Aug 30 '12 at 13:55
  • in order for my code to comply with the PECS should I declare Tree as Tree extends T> ? – KyelJmD Aug 30 '12 at 14:05
  • With PECS it's simple: if you don't comply with it, the compiler won't let you call some methods. If it's alright with the compiler, then you don't have to worry about anything else. BTW you can't declare the class with a wildcard. That's only used for variable/parameter types. – Marko Topolnik Aug 30 '12 at 14:08
  • what do you mean by it is used for variable?? care to give an example? – KyelJmD Aug 30 '12 at 14:26
  • `Tree< ? super T> src` is an example. – Marko Topolnik Aug 30 '12 at 15:05
  • CAn it be done something like this? Tree < ? super Number > src = new Tree super Number>(2) ?? – KyelJmD Aug 30 '12 at 15:08
  • 1
    Yes, that's right. You cannot use type bounds with `new`. It's only legal, as I said, in variable/parameter type declaration. – Marko Topolnik Aug 30 '12 at 15:25
2

The following cleared things up a bit in my mind. Hope it helps you.

Imagine a list of elements of some unknown type X and there is some logic some where that scans the list as elements of type X explicitly (after the list is built). Lets call this logic Logic1.

If you are going to scan that list as objects of type T, there is a requirement that X be derived from T in some form. Otherwise, you will have problems. Hence your signature should be:

void scan(List<? extends T>);

If you are going to add objects of type T to the list, there is a requirement that T be derived from X. Otherwise Logic1 mentioned above will have problems. Hence your signature should be:

void add(List<? super T>);

Note that it is my understanding that both "scan" and "add" can actually remove entries from the list. I find PECS to be a little confusing. The "producer" and "consumer" comes from the perspective of the list. I don't like to look at list as active thing that can execute logic. I like SEAS (Scan Extends and Add Supers). Just my 2 cents.

Arjun
  • 31
  • 2