participate


Generics - How to convert List.add() from old code?
<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
MartinHilpert
Posts:3,418
Registered: 10/24/97
How to convert List.add() from old code?   
Feb 18, 2005 8:05 AM

 
I get a generics warning that I want to resolve:

//getTicks().add(new Tick(new Double(tickVal), tickLabel, x, y));
List ticks = getTicks();
Object o = new Tick(new Double(tickVal), tickLabel, x, y);
ticks.add(o);


The last line results in the warning: Type safety: The method add(Object) elongs to the raw type List. References to generic type List<E> should be parameterized.

As the third party API call "getTicks()" returns a List (still 1.4 code, so no List<Tick> result), the call of add() produces this warning. But how can I rewrite the code to remove this warning?
 
lord_pixel
Posts:252
Registered: 10/21/97
Re: How to convert List.add() from old code?   
Feb 18, 2005 8:55 AM (reply 1 of 9)  (In reply to original post )

 
As the third party API call "getTicks()" returns a
List (still 1.4 code, so no List<Tick> result), the
call of add() produces this warning. But how can I
rewrite the code to remove this warning?

I think you'll have to copy:

    public static void main(String[] argv) {
        List ticks = getTicks();
        List<Tick> t2 = new ArrayList<Tick>(ticks.size());
        convert(Tick.class, ticks, t2);
        t2.add(new Tick());
    }
    
    private static <T> void convert(Class<T> type, Collection raw, Collection<T> cooked) {
        for (Iterator i = raw.iterator(); i.hasNext();) {
            cooked.add(type.cast(i.next()));
        }
    }


Occurs to me that something like "convert" would be useful in java.util.Collections.

This is pretty much by design. If you mix 1.4 and 1.5 code you will get warnings.
There's an annotation @SupressWarnings, but it is not implemented in 1.5.0.
 
MartinHilpert
Posts:3,418
Registered: 10/24/97
Re: How to convert List.add() from old code?   
Feb 18, 2005 9:27 AM (reply 2 of 9)  (In reply to #1 )

 
Well done, lord! Thank you!
 
andrejn
Posts:77
Registered: 5/22/97
Re: How to convert List.add() from old code?   
Feb 19, 2005 9:55 AM (reply 3 of 9)  (In reply to original post )

 
Hi MartinHilpert

copying old structures into new generified structures as lord_pixel proposed is one
solution but you have to be aware that you are paying price in terms of performance. If
you had many libraries using this style your generified program would definitely
behave worse than ungenerified one.

I would like to propose another solution. First, I would suggest that instead of using
the old style as it is presented in the following code
/** Old style */
public void addNewTicks1() {
    List ticks = Library.getTicks();
    Object o = new Tick(new Double(0.), "x", 0, 0);
    ticks.add(o);
}

you begin to adopt new style:
- as far as your types are concerned try to be as specific as you can;
- try to remove all references to Objects from your programs.

The result could be the following:
 /** New style */
public void addNewTicks2() {
     List<Tick> l = Library.getTicks();
     Tick t = new Tick(new Double(0.), "abc", 0, 0);
     l.add(t);
}

This program will still produce warning but this time it will complain that the function
getTicks() produces unchecked conversion. Since you are "absolutely" sure that there
is nothing but Ticks in that list you can say to yourself that you could live with that
warning. Actuallly, there is no reason why not to trust your library provider. If you would
still want to eliminate that warning there are two possibilities:
1. You could modify your program and add @SuppressWarnings("unchecked") as it
is shown below.
/** New style, wait for Java 5.1, 6.0, ...*/
@SuppressWarnings("unchecked")
public void addNewTicks3() {
    List<Tick> l = Library.getTicks();
    Tick t = new Tick(new Double(0.), "abc", 0, 0);
    l.add(t);
}

Unfortunately this does not work yet.You might kindly ask Peter von der Ahe to
implement that feature as soon as possible. I would warn against using this tactic
beacuse you have to be a master of generics and be 100% sure what are you
doing. You know, don't shoot the messenger...

2. I think that better solution would be to ask your library provider to offer you a generified library:
/** Generified  library */
public static List<Tick> getTicks() {
    List<Tick> l = new ArrayList<Tick>();
    l.add(new Tick(new Double(0.), "def", 10, 10));
    return l;
}

If there are many developers who are still using previous versions of java the library
provider might be reluctant to do so - to offer generifed vesion of library -
because it will have to maintain two versions of library. And now we came back to java
developers. We should learn generics, see their benefits and start to use them as
soon as possible The conclusion is that we might benefit more if we ALL move to
new version of java as soon as possible...

Best regards,
Andrei
 
MartinHilpert
Posts:3,418
Registered: 10/24/97
Re: How to convert List.add() from old code?   
Feb 21, 2005 1:53 AM (reply 4 of 9)  (In reply to #3 )

 
Hm ... this means, that everyone that switches to 1.5 will get lots of warnings for their pre-1.5 code. I wonder if this is a good strategy of Sun to generate these warnings by default. I tried to do my best adjusting my code to 1.5 generics to eliminate all warnings, but I still have some warnings where I don't know how to "fix" them ... :-( E.g. the code from the Java Tutorial to implement a sortable table (TableSorter.java):

    public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) o1).compareTo(o2);
        }
    };


with the warning:

Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized
 
andrejn
Posts:77
Registered: 5/22/97
Re: How to convert List.add() from old code?   
Feb 21, 2005 11:43 AM (reply 5 of 9)  (In reply to #4 )

 
Hi,

suppose that you want to compare Double values then the above code
should be written as:
public static final Comparator<Double> COMPARABLE_COMAPRATOR = 
        new Comparator<Double>() {
    public int compare(Double d1, Double d2) {
        return d1.compareTo(d2);
    }
};

Regards,
Andrei
 
MartinHilpert
Posts:3,418
Registered: 10/24/97
Re: How to convert List.add() from old code?   
Feb 22, 2005 1:34 AM (reply 6 of 9)  (In reply to #5 )

 
Yes, but the table of course has arbitrary types and it should work for all Comparable types. I tried

    public static final Comparator<T> COMPARABLE_COMAPRATOR = new Comparator<T>() {
        public int compare(T o1, T o2) {
            return ((T) o1).compareTo(o2);
        }
    };


but that doesn't compile.
 
pholser
Posts:399
Registered: 4/8/99
Re: How to convert List.add() from old code?   
Feb 22, 2005 9:18 AM (reply 7 of 9)  (In reply to #6 )

 
How about this:
class Comparables {
    static <T extends Comparable<T>> Comparator<T> comparatorOfComparables() {
        return new Comparator<T>() {
            public int compare( final T first, final T second ) {
                return first.compareTo( second );
            }
        };
    }
 
MartinHilpert
Posts:3,418
Registered: 10/24/97
Re: How to convert List.add() from old code?   
Mar 4, 2005 6:39 AM (reply 8 of 9)  (In reply to #1 )

 
As the third party API call "getTicks()" returns a
List (still 1.4 code, so no List<Tick> result),
the
call of add() produces this warning. But how can I
rewrite the code to remove this warning?

I think you'll have to copy:

    public static void main(String[] argv) {
        List ticks = getTicks();
List<Tick> t2 = new
t2 = new ArrayList<Tick>(ticks.size());
        convert(Tick.class, ticks, t2);
        t2.add(new Tick());
    }
    
private static <T> void convert(Class<T> type,
ype, Collection raw, Collection<T> cooked) {
for (Iterator i = raw.iterator();
rator(); i.hasNext();) {
            cooked.add(type.cast(i.next()));
        }
    }


Occurs to me that something like "convert" would be
useful in java.util.Collections.

Argh! The code basically works, but it creates a new List object. Is there a way to conserve the original List object? (Unfortunately, this 3rd party library has no setTicks() method, just a getTicks() ... :-( )
 
nishar
Posts:1
Registered: 4/25/05
Re: How to convert string to array object of type E   
Apr 25, 2005 1:14 PM (reply 9 of 9)  (In reply to original post )

 
how can i convert a string to type E that is an array object?
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums