/** * Since this coder uses elementCoders.get(index) and coders that are known to run in constant * time, we defer the return value to that coder. */ @Override public boolean isRegisterByteSizeObserverCheap(RawUnionValue union, Context context) { int index = getIndexForEncoding(union); @SuppressWarnings("unchecked") Coder<Object> coder = (Coder<Object>) elementCoders.get(index); return coder.isRegisterByteSizeObserverCheap(union.getValue(), context); }
private int getIndexForEncoding(RawUnionValue union) { if (union == null) { throw new IllegalArgumentException("cannot encode a null tagged union"); } int index = union.getUnionTag(); if (index < 0 || index >= elementCoders.size()) { throw new IllegalArgumentException( "union value index " + index + " not in range [0.." + (elementCoders.size() - 1) + "]"); } return index; }
/** Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */ @Override public void registerByteSizeObserver( RawUnionValue union, ElementByteSizeObserver observer, Context context) throws Exception { int index = getIndexForEncoding(union); // Write out the union tag. observer.update(VarInt.getLength(index)); // Write out the actual value. @SuppressWarnings("unchecked") Coder<Object> coder = (Coder<Object>) elementCoders.get(index); coder.registerByteSizeObserver(union.getValue(), observer, context); }
@SuppressWarnings("unchecked") @Override public void encode(RawUnionValue union, OutputStream outStream, Context context) throws IOException, CoderException { int index = getIndexForEncoding(union); // Write out the union tag. VarInt.encode(index, outStream); // Write out the actual value. Coder<Object> coder = (Coder<Object>) elementCoders.get(index); coder.encode(union.getValue(), outStream, context); }