Skip to main content

Are generics the work of the Devil?

3 replies [Last post]
scottr
Offline
Joined: 2004-09-05
Points: 0

Oh great, I thought, I've got 1.5, now lets start playing around with generics. Especially to stop those annoying compiler warnings about the lack of type safety in my 'old-school' code.

Hmm, ok, add a here. Uh oh, compiler error, need a up top in class declaration. Uh oh, that didn't work, seems an interface I implemented wants a specific type. Ok, so change something different... and so on...

After half an hour stumbling around in the dark, I finally converted this (a comparator that compares lists only by an indexed element):

<br />
public class ListElementComparator implements Comparator<br />
{<br />
    private int compareIndex = 0;</p>
<p>    public int compare(Object o1, Object o2)<br />
    {<br />
        Object el1 = ((List) o1).get(compareIndex);<br />
        Object el2 = ((List) o2).get(compareIndex);</p>
<p>        return ((Comparable) e1).compareTo(el2);<br />
    }<br />
}<br />

into this...

<br />
public class ListElementComparator> implements Comparator>><br />
{<br />
    private int compareIndex = 0;</p>
<p>    public int compare(List> list1, List> list2)<br />
    {<br />
        Comparable el1 = list1.get(indexToCompare);<br />
        Comparable el2 = list2.get(indexToCompare);</p>
<p>        return el1.compareTo(el2);<br />
    }<br />
}<br />

The verdict?
+1 type saftey
-1 readability

Not really jdnc related but a warning to anyone jumping into the tiger's jaws.

Scott

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Thomas Hawtin

jdnc-interest@javadesktop.org wrote:
> Oh great, I thought, I've got 1.5, now lets start playing around with generics. [...]

"Are generics the work of the Devil?"

If unifying static and dynamic polymorphism is the Devil's work. You
didn't start with a particular easy, or common to implement, example.

> [code]
> public class ListElementComparator> implements Comparator>>

It's a bit pointless declaring the T generic parameter and then never
using it.

The Comparator you have decided to implemented is limited. The chances
of having a useful List> are limited. We want to be able
to handle, for some X, any Lists who's element types fit a
Comparable. Even Lists more restrictive about their element types,
such as List.

Here's my (untested) effort:

/** An inconsistent with equals comparator. */
public class ListElementComparator>
implements Comparator>
{
private final int index;
public ListElementComparator(int index) {
this.index = index;
}
public int compare(
List lhs, List rhs
) {
T l = lhs.get(index);
T r = rhs.get(index);

return l.compareTo(r);
}
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
ListElementComparator other = (ListElementComparator)obj;
// We can't test equality of generic arguments,
// unless we add a Class argument to the constructor.
return other.index == this.index;
}
}

The "? super" is somewhat superfluous. It only permits client code to
delcare a ListElementComparator with a more specific generic type.
Possibly useful if the client code is itself generic.

The generics are similar to >
Collections.sort(List list). In that case List is not
viable as the list must be written to as well as read, and would be
pointless anyway.

If you really wanted to, you could add an extra generic parameter to the
class, C extends List. Now implementing Comparator the
client has more control over the Comparator interface implemented. For
instance:

Comparator> comparator =
new ListElementComparator, Integer>(2);

Tom Hawtin

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

scottr
Offline
Joined: 2004-09-05
Points: 0

Yeah, this works. But still difficult to just look at a see what it is intended to perform.

I guess this generics stuff is going to take quite some upskilling in basic language features.

Scott

[code]
public class ListElementComparator> implements Comparator>
{
private int index;

public ListElementComparator(int index)
{
this.index = index;
}

public int compare(List lhs, List rhs)
{
T l = lhs.get(index);
T r = rhs.get(index);

return l.compareTo(r);
}
}
[/code]

Ricardo Lopes

This works and is more readable:

[code]
public class ListElementComparator
{
private int compareIndex = 0;

public int compare(List list1, List list2)
{
T el1 = list1.get(compareIndex);
T el2 = list2.get(compareIndex);

return el1.compareTo(el2);
}
}
[/code]

I think it would be more correct to have a instead of a , but i think is not posible...

Hope this helps.

The verdict?
+1 type saftey
+1 readability :)

Ricardo Lopes.

jdnc-interest@javadesktop.org wrote:

>Oh great, I thought, I've got 1.5, now lets start playing around with generics. Especially to stop those annoying compiler warnings about the lack of type safety in my 'old-school' code.
>
>Hmm, ok, add a here. Uh oh, compiler error, need a up top in class declaration. Uh oh, that didn't work, seems an interface I implemented wants a specific type. Ok, so change something different... and so on...
>
>After half an hour stumbling around in the dark, I finally converted this (a comparator that compares lists only by an indexed element):
>
>[code]
>public class ListElementComparator implements Comparator
>{
> private int compareIndex = 0;
>
> public int compare(Object o1, Object o2)
> {
> Object el1 = ((List) o1).get(compareIndex);
> Object el2 = ((List) o2).get(compareIndex);
>
> return ((Comparable) e1).compareTo(el2);
> }
>}
>[/code]
>
>into this...
>
>[code]
>public class ListElementComparator> implements Comparator>>
>{
> private int compareIndex = 0;
>
> public int compare(List> list1, List> list2)
> {
> Comparable el1 = list1.get(indexToCompare);
> Comparable el2 = list2.get(indexToCompare);
>
> return el1.compareTo(el2);
> }
>}
>[/code]
>
>The verdict?
>+1 type saftey
>-1 readability
>
>Not really jdnc related but a warning to anyone jumping into the tiger's jaws.
>
>Scott
>---
>[Message sent by forum member 'scottr' (Scott Russell)]
>
>http://www.javadesktop.org/forums/thread.jspa?messageID=50803왳
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
>For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net