// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package stdgen;

public class StdGen {
    // **** demo classes ****
    public static class P {
        public void mP() { }
    }
    public static class C extends P {
        public void mC() { }
    }
    public static class Container<T> {
        private T o;
        public void put(T o) { this.o = o; }
        public T get() { return o; }
    }
    // **** standard generic methods ****
    public static <T> void test(Container<T> co, T o) {
        //co.put(new P()); // no required relation between T and P
        //co.put(new C()); // no required relation between T and C
        co.put(o);
        //P pcopy = co.get(); // no required relation between T and P
        //pcopy.mP();
        //pcopy.mC();
        //C ccopy = co.get(); // no required relation between T and C
        //ccopy.mP();
        //ccopy.mC();
        T tcopy = co.get();
        //tcopy.mP();
        //tcopy.mC();
    }
    public static <T extends P> void testp(Container<T> co, T o) {
        //co.put(new P()); // problem if T is C or a subclass of C
        //co.put(new C()); // problem if T is a subclass of C
        co.put(o);
        P pcopy = co.get();
        pcopy.mP();
        //pcopy.mC();
        //C ccopy = co.get(); // problem if T is P
        //ccopy.mP();
        //ccopy.mC();
        T tcopy = co.get();
        tcopy.mP();
        //tcopy.mC();
    }
    public static <T extends C> void testc(Container<T> co, T o) {
        //co.put(new P()); // problem if T is C or a subclass of C
        //co.put(new C()); // problem if T is a subclass of C
        co.put(o);
        P pcopy = co.get();
        pcopy.mP();
        //pcopy.mC();
        C ccopy = co.get();
        ccopy.mP();
        ccopy.mC();
        T tcopy = co.get();
        tcopy.mP();
        tcopy.mC();
    }
    // ****
    public static void main(String[] args) {
        test(new Container<P>(), new P());
        test(new Container<C>(), new C());
        testp(new Container<P>(), new P());
        testp(new Container<C>(), new C());
        //testc(new Container<P>(), new P()); // P does not match "T extends C"
        testc(new Container<C>(), new C());
    }
}
