views:

749

answers:

2

I have a generic class which bundles an Object and an order:

public class OrderedObject<T> {
    private int order;
    private T object;

    public OrderedObject(int order, T object) {
        this.order = order;
        this.object = object;
    }

    public int getOrder() {
        return order;
    }

    public T getObject() {
        return object;
    }
}

I developed a Set implementation which stores OrderedObject<T> instances and wants to produce an Iterator<T> enumerating in the order enforced by the built-in order:

public class OrderedObjectSet<T> extends AbstractSet<T> implements Set<T> {
    Set<OrderedObject<T>> s = new HashSet<OrderedObject<T>>();

    public boolean add(int order, T object) {
        return s.add(new OrderedObject<T>(order, object));
    }

    public Iterator<T> iterator() {
        return new OrderedObjectSetIterator<T>();
    }

    public int size() {
        return s.size();
    }

    private class OrderedObjectSetIterator<T> implements Iterator<T> {
        private int index;

        public boolean hasNext() {
            return index < s.size();
        }

        public T next() {
            T object = null;

            for (Iterator<OrderedObject<T>> it = s.iterator(); it.hasNext(); ) {
                OrderedObject<T> o = it.next();
                if (o.getOrder() == index) {
                    object = o.getObject();
                }
            }

            index++;
            return object;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}

The latter class does not compile because there seems to some confusion of types in the Iterator initialization at

for (Iterator<OrderedObject<T>> it = s.iterator(); it.hasNext(); ) {

What do I overlook?

A: 

Hmmm...

does adding a public no-arg constructor to OrderedObjectSetIterator solve the problem? I get suspicious when I don't see any explicit constructors.

Jason S
That shouldn't make a difference, because a) the constructor has nothing to do with the line that the error is happening at, and b) the default no-arg constructor is automatically added by the compiler anyway.
Michael Myers
+4  A: 

The confusion is because the inner class OrderedObjectSetIterator introduces a generic type called the same (T) as the outer class. Eclipse IDE shows a warning:

The type parameter T is hiding the type T

So I guess you don't need to introduce another parameter type, just use the same as the outer class defines.

Basically, the inner class would be defined as:

private class OrderedObjectSetIterator implements Iterator<T> {
....

And the iterator method as:

public Iterator<T> iterator() {
    return new OrderedObjectSetIterator();
}
Grzegorz Oledzki
This is exactly what was wrong. I do not need another parameter type.Thank you.Solved.I feel the suggested TreeSet approach is the more elegant solution to apply. Nevertheless happy that I did get insights on Generics by experimenting this homegrown Set.Bart
Bart Strubbe