EitherSerializer extends TypeSerializer<Either<L, R>> { @Override public Either<L, R> copy(Either<L, R> from) { if (from.isLeft()) { L left = from.left(); L copyLeft = leftSerializer.copy(left); return Left(copyLeft); } else { R right = from.right(); R copyRight = rightSerializer.copy(right); return Right(copyRight); } } EitherSerializer(TypeSerializer<L> leftSerializer, TypeSerializer<R> rightSerializer); @Override boolean isImmutableType(); @Override TypeSerializer<Either<L, R>> duplicate(); @Override Either<L, R> createInstance(); @Override Either<L, R> copy(Either<L, R> from); @Override Either<L, R> copy(Either<L, R> from, Either<L, R> reuse); @Override int getLength(); @Override void serialize(Either<L, R> record, DataOutputView target); @Override Either<L, R> deserialize(DataInputView source); @Override Either<L, R> deserialize(Either<L, R> reuse, DataInputView source); @Override void copy(DataInputView source, DataOutputView target); @SuppressWarnings("unchecked") @Override boolean equals(Object obj); @Override boolean canEqual(Object obj); @Override int hashCode(); @Override EitherSerializerConfigSnapshot snapshotConfiguration(); @Override CompatibilityResult<Either<L, R>> ensureCompatibility(TypeSerializerConfigSnapshot configSnapshot);  }
@Test public void testEitherWithObjectReuse() { EitherTypeInfo<LongValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>( ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO); EitherSerializer<LongValue, DoubleValue> eitherSerializer = (EitherSerializer<LongValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig()); LongValue lv = new LongValue(); DoubleValue dv = new DoubleValue(); Either<LongValue, DoubleValue> left = Left(lv); Either<LongValue, DoubleValue> right = Right(dv); Either<LongValue, DoubleValue> copy0 = eitherSerializer.copy(left, right); Either<LongValue, DoubleValue> copy1 = eitherSerializer.copy(right, copy0); Either<LongValue, DoubleValue> copy2 = eitherSerializer.copy(left, copy1); assertSame(right, copy1); assertSame(copy0, copy2); assertSame(right.right(), copy1.right()); assertSame(copy0.left(), copy2.left()); }