}
};
}
public <T1> RangeEndpoint makeEndpoint(T1 value, Equality equality) {
RangeEndpoint endpoint = new RangeEndpoint() {
private ByteBuffer out = ByteBuffer.allocate(bufferSize);
private int position = 0;
private boolean done = false;
@Override
public RangeEndpoint append(Object value, Equality equality) {
ComponentSerializer<?> serializer = components.get(position);
position++;
// First, serialize the ByteBuffer for this component
ByteBuffer cb;
try {
cb = serializer.serializeValue(value);
}
catch (Exception e) {
throw new RuntimeException(e);
}
if (cb == null) {
cb = EMPTY_BYTE_BUFFER;
}
if (cb.limit() + COMPONENT_OVERHEAD > out.remaining()) {
int exponent = (int) Math.ceil(Math.log((double) (cb.limit() + COMPONENT_OVERHEAD) / (double) out.limit()) / Math.log(2));
int newBufferSize = out.limit() * (int) Math.pow(2, exponent);
ByteBuffer temp = ByteBuffer.allocate(newBufferSize);
out.flip();
temp.put(out);
out = temp;
}
// Write the data: <length><data><0>
out.putShort((short) cb.remaining());
out.put(cb.slice());
out.put(equality.toByte());
return this;
}
@Override
public ByteBuffer toBytes() {
if (!done) {
out.flip();
done = true;
}
return out;
}
};
endpoint.append(value, equality);
return endpoint;
}