StructList.Reader.Iterator

This commit is contained in:
David Renshaw 2014-06-20 20:45:00 -04:00
parent 739a34dde8
commit 8937d0607a
3 changed files with 24 additions and 11 deletions

View file

@ -14,9 +14,7 @@ public class CarSales
result += car.getSeats() * 200;
result += car.getDoors() * 350;
StructList.Reader<Wheel.Reader> wheels = car.getWheels();
for (int i = 0; i < wheels.size(); ++i) {
Wheel.Reader wheel = wheels.get(i);
for (Wheel.Reader wheel : car.getWheels()) {
result += ((long)wheel.getDiameter() * (long)wheel.getDiameter());
result += wheel.getSnowTires() ? 100 : 0;
}

View file

@ -49,15 +49,10 @@ public class AddressbookMain {
public static void printAddressBook() throws java.io.IOException {
MessageReader message = InputStreamMessageReader.create(System.in);
AddressBook.Reader addressbook = message.getRoot(AddressBook.factory);
StructList.Reader<Person.Reader> people = addressbook.getPeople();
int size = people.size();
for(int ii = 0; ii < size; ++ii) {
Person.Reader person = people.get(ii);
for(Person.Reader person : addressbook.getPeople()) {
System.out.println(person.getName() + ": " + person.getEmail());
StructList.Reader<Person.PhoneNumber.Reader> phones = person.getPhones();
for (int jj = 0; jj < phones.size(); ++jj) {
Person.PhoneNumber.Reader phone = phones.get(jj);
for (Person.PhoneNumber.Reader phone : person.getPhones()) {
String typeName = "UNKNOWN";
switch (phone.getType()) {
case MOBILE :

View file

@ -1,7 +1,7 @@
package org.capnproto;
public final class StructList {
public static final class Reader<T> {
public static final class Reader<T> implements Iterable<T> {
public final ListReader reader;
public final FromStructReader<T> factory;
@ -17,6 +17,26 @@ public final class StructList {
public T get(int index) {
return this.factory.fromStructReader(this.reader.getStructElement(index));
}
public final class Iterator implements java.util.Iterator<T> {
public Reader<T> list;
public int idx = 0;
public Iterator(Reader<T> list) {
this.list = list;
}
public T next() {
return list.factory.fromStructReader(list.reader.getStructElement(idx++));
}
public boolean hasNext() {
return idx < list.size();
}
}
public java.util.Iterator<T> iterator() {
return new Iterator(this);
}
}
public static final class Builder<T> {