StructList.Reader.Iterator
This commit is contained in:
parent
739a34dde8
commit
8937d0607a
3 changed files with 24 additions and 11 deletions
|
@ -14,9 +14,7 @@ public class CarSales
|
||||||
result += car.getSeats() * 200;
|
result += car.getSeats() * 200;
|
||||||
result += car.getDoors() * 350;
|
result += car.getDoors() * 350;
|
||||||
|
|
||||||
StructList.Reader<Wheel.Reader> wheels = car.getWheels();
|
for (Wheel.Reader wheel : car.getWheels()) {
|
||||||
for (int i = 0; i < wheels.size(); ++i) {
|
|
||||||
Wheel.Reader wheel = wheels.get(i);
|
|
||||||
result += ((long)wheel.getDiameter() * (long)wheel.getDiameter());
|
result += ((long)wheel.getDiameter() * (long)wheel.getDiameter());
|
||||||
result += wheel.getSnowTires() ? 100 : 0;
|
result += wheel.getSnowTires() ? 100 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,15 +49,10 @@ public class AddressbookMain {
|
||||||
public static void printAddressBook() throws java.io.IOException {
|
public static void printAddressBook() throws java.io.IOException {
|
||||||
MessageReader message = InputStreamMessageReader.create(System.in);
|
MessageReader message = InputStreamMessageReader.create(System.in);
|
||||||
AddressBook.Reader addressbook = message.getRoot(AddressBook.factory);
|
AddressBook.Reader addressbook = message.getRoot(AddressBook.factory);
|
||||||
StructList.Reader<Person.Reader> people = addressbook.getPeople();
|
for(Person.Reader person : addressbook.getPeople()) {
|
||||||
int size = people.size();
|
|
||||||
for(int ii = 0; ii < size; ++ii) {
|
|
||||||
Person.Reader person = people.get(ii);
|
|
||||||
System.out.println(person.getName() + ": " + person.getEmail());
|
System.out.println(person.getName() + ": " + person.getEmail());
|
||||||
|
|
||||||
StructList.Reader<Person.PhoneNumber.Reader> phones = person.getPhones();
|
for (Person.PhoneNumber.Reader phone : person.getPhones()) {
|
||||||
for (int jj = 0; jj < phones.size(); ++jj) {
|
|
||||||
Person.PhoneNumber.Reader phone = phones.get(jj);
|
|
||||||
String typeName = "UNKNOWN";
|
String typeName = "UNKNOWN";
|
||||||
switch (phone.getType()) {
|
switch (phone.getType()) {
|
||||||
case MOBILE :
|
case MOBILE :
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package org.capnproto;
|
package org.capnproto;
|
||||||
|
|
||||||
public final class StructList {
|
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 ListReader reader;
|
||||||
public final FromStructReader<T> factory;
|
public final FromStructReader<T> factory;
|
||||||
|
|
||||||
|
@ -17,6 +17,26 @@ public final class StructList {
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
return this.factory.fromStructReader(this.reader.getStructElement(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> {
|
public static final class Builder<T> {
|
||||||
|
|
Loading…
Reference in a new issue