F
Frank Fredstone
I want to expose an iterator over a collection, where the iterator
returns elements that are a super class of the actual
objects. Essentially, so I can implement interfaces.
Or, another way to do what I think looks like the intention of this
illegal code:
given: Collection<PrivateAye> c;
Iterator<Aye> it = c.iterator();
PrivateAye below is the internal implementation of the Aye interface
that also extends another class.
Is there a simpler way than what I have in the iterator method below:
public interface Aye {
String aye();
}
public class A {
private String a;
public A(String eh) { a = eh; }
String a() { return a; }
}
import java.util.Iterator;
import java.util.Vector;
public class X implements Iterable<Aye> {
private class PrivateAye extends A implements Aye {
private int code = 0;
public PrivateAye(String eh, int n) {
super(eh);
setCode(n);
}
public int getCode() { return code; }
public void setCode(int n) { code = n; }
public String aye() { return a(); }
}
private Vector<PrivateAye> ayes;
public static void main(String[] args) throws Exception {
X x = new X();
x.go();
}
public void go() throws Exception {
ayes = new Vector<PrivateAye>();
ayes.add(new PrivateAye("a", 0));
ayes.add(new PrivateAye("b", 1));
for (Aye a : this) {
System.out.println(a.aye());
}
}
public Iterator<Aye> iterator() {
return new Iterator<Aye>() {
private Vector<? extends Aye> vec = ayes;
private Iterator<? extends Aye> it = vec.iterator();
public boolean hasNext() { return it.hasNext(); }
public Aye next() { return it.next(); }
public void remove() { it.remove(); }
};
}
}
returns elements that are a super class of the actual
objects. Essentially, so I can implement interfaces.
Or, another way to do what I think looks like the intention of this
illegal code:
given: Collection<PrivateAye> c;
Iterator<Aye> it = c.iterator();
PrivateAye below is the internal implementation of the Aye interface
that also extends another class.
Is there a simpler way than what I have in the iterator method below:
public interface Aye {
String aye();
}
public class A {
private String a;
public A(String eh) { a = eh; }
String a() { return a; }
}
import java.util.Iterator;
import java.util.Vector;
public class X implements Iterable<Aye> {
private class PrivateAye extends A implements Aye {
private int code = 0;
public PrivateAye(String eh, int n) {
super(eh);
setCode(n);
}
public int getCode() { return code; }
public void setCode(int n) { code = n; }
public String aye() { return a(); }
}
private Vector<PrivateAye> ayes;
public static void main(String[] args) throws Exception {
X x = new X();
x.go();
}
public void go() throws Exception {
ayes = new Vector<PrivateAye>();
ayes.add(new PrivateAye("a", 0));
ayes.add(new PrivateAye("b", 1));
for (Aye a : this) {
System.out.println(a.aye());
}
}
public Iterator<Aye> iterator() {
return new Iterator<Aye>() {
private Vector<? extends Aye> vec = ayes;
private Iterator<? extends Aye> it = vec.iterator();
public boolean hasNext() { return it.hasNext(); }
public Aye next() { return it.next(); }
public void remove() { it.remove(); }
};
}
}