pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From calathus <calat...@gmail.com>
Subject Re: why ListButton.getListData returns List<?> ?
Date Tue, 25 Jan 2011 19:28:46 GMT
Since generic version may give wrong impression that it is type safe, it may
be better to provide two version for getter, one is just return
List<object>, another will return List<T> taking class value as the
argument. also we may use generic method for setter. see following sample
code.


public class Main {

    static class A {
        List<?> ts;

        // new version
        public List<Object> getList() {
            return (List<Object>)ts;
        }
        public <T> List<T> getList(Class<T> cls) {
            return (List<T>)ts;
        }
        public <T> void setList(List<T> ts) {
            this.ts = ts;
        }

        // obsolete version
        public List<?> getList1() {
            return ts;
        }
        public void setList1(List<?> ts) {
            this.ts = ts;
        }
    }

    interface I {
        String getName();
    }

    static class B {
        List<? extends I> ts;

        public List<I> getList() {
            return (List<I>)ts;
        }
        // this is reasonably type safe. but not completely safe.
        public <T extends I> List<T> getList(Class<T> cls) {
            return (List<T>)ts;
        }
        public <T extends I> void setList(List<T> ts) {
            this.ts = ts;
        }

        // this is too loose. should provide more type error detection.
        public <T> List<T> getList0() {
            return (List<T>)ts;
        }
        // this is quevalent to setList, but
        public void setList1(List<? extends I> ts) {
            this.ts = ts;
        }
    }

    static class J implements I {
        String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    public static void main(String[] args) {
        {
            A a = new A();
            a.setList(new ArrayList<Integer>());
            List<Object> list = (List<Object>)a.getList();
            List<J> list1 = (List<J>)a.getList(J.class);
            List<Object> list2a = a.getList1(); // error
            List<Object> list2b = (List<Object>)a.getList1(); // OK
            Object list3a = a.getList();
            List<J> list3b = (List<J>)list3a;
            List<J> list4 = (List<J>)(Object)a.getList();
        }
        {
            B b = new B();
            b.setList(new ArrayList<J>());
            List<I> list1 = b.getList();// OK
            List<J> list2 = b.getList(); // compile error
            List<J> list2a = b.getList(J.class); // OK
            List<Integer> list2b = b.getList(Integer.class); // compile
error(expected)
            List<Object> list3 = b.getList(); // compile error
            List<Object> list3a = b.getList(Object.class); // compile
error(expected, but shuold be OK)
            b.getList().add(new Integer(4)); // compile error(expected)
            b.getList().add(new J());// OK
        }
        {
            B b = new B();
            b.setList(new ArrayList<J>());
            List<I> list1 = b.getList0();// OK
            List<J> list2 = b.getList0(); // OK(but not safe)
            List<Object> list3 = b.getList0(); // OK
            b.getList0().add(new Integer(4)); // OK(but not safe)
        }
    }

}



On Mon, Jan 24, 2011 at 5:14 PM, Greg Brown <gk_brown@verizon.net> wrote:

> The advantage over List<Object> is the ability to assigned the ability to
>> just any List<N>.
>> If we use List<Object> we need to copy another object just overcome this
>> issue. If we need to maintain object identity, this approach is not
>> appropriate. Then we will be stuck.
>>
>
> Right - that's why we return List<?> from getListData(). But as you noted,
> defining the method as follows doesn't really help ensure compile-time type
> safety:
>
> public <T> List<T> getListData() { ... }
>
> It simply helps to avoid casting the return type to List<Foo> in order to
> modify the list.
>
> G
>
>
>
>
>


-- 
Cheers,
calathus

Mime
View raw message