protected StringDelegateCoder(final Class<T> clazz, TypeDescriptor<T> typeDescriptor) { delegateCoder = DelegateCoder.of( StringUtf8Coder.of(), Object::toString, input -> clazz.getConstructor(String.class).newInstance(input), typeDescriptor); this.clazz = clazz; }
@Override public TypeDescriptor<T> getEncodedTypeDescriptor() { return delegateCoder.getEncodedTypeDescriptor(); } }
public static <T, IntermediateT> DelegateCoder<T, IntermediateT> of( Coder<IntermediateT> coder, CodingFunction<T, IntermediateT> toFn, CodingFunction<IntermediateT, T> fromFn, @Nullable TypeDescriptor<T> typeDescriptor) { return new DelegateCoder<>(coder, toFn, fromFn, typeDescriptor); }
@Test public void testEncodedTypeDescriptorSimpleEncodedType() throws Exception { assertThat( DelegateCoder.of( StringUtf8Coder.of(), String::valueOf, Integer::valueOf, new TypeDescriptor<Integer>() {}) .getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(Integer.class))); }
@Override public void encode(T value, OutputStream outStream, Context context) throws CoderException, IOException { coder.encode(applyAndWrapExceptions(toFn, value), outStream, context); }
@Override public void encode(T value, OutputStream outStream, Context context) throws CoderException, IOException { delegateCoder.encode(value, outStream, context); }
@Override public T decode(InputStream inStream, Context context) throws CoderException, IOException { return delegateCoder.decode(inStream, context); }
@Override public T decode(InputStream inStream, Context context) throws CoderException, IOException { return applyAndWrapExceptions(fromFn, coder.decode(inStream, context)); }
@Override public void encode(T value, OutputStream outStream) throws CoderException, IOException { encode(value, outStream, Context.NESTED); }
@Override public T decode(InputStream inStream) throws CoderException, IOException { return decode(inStream, Context.NESTED); }
@Override public Coder<double[]> getAccumulatorCoder(CoderRegistry registry, Coder<Double> inputCoder) { return DelegateCoder.of( inputCoder, new ToDoubleCodingFunction(), new FromDoubleCodingFunction()); }
@Test public void testEncodedTypeDescriptor() throws Exception { TypeDescriptor<Set<Integer>> typeDescriptor = new TypeDescriptor<Set<Integer>>() {}; assertThat(TEST_CODER.getEncodedTypeDescriptor(), equalTo(typeDescriptor)); } }
@Override public Coder<int[]> getAccumulatorCoder(CoderRegistry registry, Coder<Integer> inputCoder) { return DelegateCoder.of( inputCoder, new ToIntegerCodingFunction(), new FromIntegerCodingFunction()); }
@Override public Coder<long[]> getAccumulatorCoder(CoderRegistry registry, Coder<Long> inputCoder) { return DelegateCoder.of(inputCoder, new ToLongCodingFunction(), new FromLongCodingFunction()); }
@Override public Coder<CounterMark> getCheckpointMarkCoder() { return DelegateCoder.of(VarIntCoder.of(), input -> input.current, CounterMark::new); }
@Test public void testCoderEquals() throws Exception { DelegateCoder.CodingFunction<Integer, Integer> identityFn = input -> input; Coder<Integer> varIntCoder1 = DelegateCoder.of(VarIntCoder.of(), identityFn, identityFn); Coder<Integer> varIntCoder2 = DelegateCoder.of(VarIntCoder.of(), identityFn, identityFn); Coder<Integer> bigEndianIntegerCoder = DelegateCoder.of(BigEndianIntegerCoder.of(), identityFn, identityFn); assertEquals(varIntCoder1, varIntCoder2); assertEquals(varIntCoder1.hashCode(), varIntCoder2.hashCode()); assertNotEquals(varIntCoder1, bigEndianIntegerCoder); assertNotEquals(varIntCoder1.hashCode(), bigEndianIntegerCoder.hashCode()); }
@Override public Coder<CounterMark> getCheckpointMarkCoder() { return DelegateCoder.of( VarIntCoder.of(), new FromCounterMark(), new ToCounterMark()); }
@Override public Coder<CounterMark> getCheckpointMarkCoder() { return DelegateCoder.of(VarIntCoder.of(), new FromCounterMark(), new ToCounterMark()); }
@Override public Coder<CounterMark> getCheckpointMarkCoder() { return DelegateCoder.of(VarIntCoder.of(), new FromCounterMark(), new ToCounterMark()); }