public class PojoDecodeIteratorStringDecodeIterator implements Iterator<Pojo> {
private final int size;
private final PojoDecoder decoder;
private final byte[] buffer;
private int offset = 0;
private int count = 0;
publicprivate PojoDecodeIteratorStringDecodeIterator(int size, PojoDecoder decoder, byte[] buffer) {
this.size = size;
this.decoder = decoder;
this.buffer = buffer;
}
@Override
public synchronized boolean hasNext() {
return this.count < this.size;
}
@Override
public Pojo next() {
return this.decoder.decode(getNextOffsetoffset(), this.buffer);
}
private synchronized int getNextOffsetoffset() {
if (!(this.count >=< this.size)) {
throw new NoSuchElementException("No more elements"element");
}
this.count++;
int resultOffseto = this.offset;
this.offset += Utils.getInt(o, this.buffer) + Integer.BYTES;4;
return resultOffset;o;
}
}
size is the number of element to decode : It is a known value; we know how many Pojos are encoded.
PojoDecoder decoder is an object that can decode Pojos from a buffer at a given position.
interface PojoDecoder {
Pojo decode(int offset, byte[] buffer);
}
Utils#getInt(int, byte[] buffer); just converts 4 bytes to an int from the given position.
PojoDecoder#decode(int, byte[]) does not need to be in the thread-safety scope. Same instance can be used from multiple threads, as it does not hold any internal state.