/** * Returns type information for Flink's {@link org.apache.flink.types.Either} type. Null values * are not supported. * * <p>Either type can be used for a value of two possible types. * * <p>Example use: <code>Types.EITHER(Types.VOID, Types.INT)</code> * * @param leftType type information of left side / {@link org.apache.flink.types.Either.Left} * @param rightType type information of right side / {@link org.apache.flink.types.Either.Right} */ public static <L, R> TypeInformation<Either<L, R>> EITHER(TypeInformation<L> leftType, TypeInformation<R> rightType) { return new EitherTypeInfo<>(leftType, rightType); }
@SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj instanceof EitherTypeInfo) { EitherTypeInfo<L, R> other = (EitherTypeInfo<L, R>) obj; return other.canEqual(this) && leftType.equals(other.leftType) && rightType.equals(other.rightType); } else { return false; } }
@SuppressWarnings("unchecked") @Test public void testStringDoubleEither() { Either<String, Double>[] testData = new Either[] { Left("banana"), Left(""), Right(32.0), Right(Double.MIN_VALUE), Right(Double.MAX_VALUE)}; EitherTypeInfo<String, Double> eitherTypeInfo = (EitherTypeInfo<String, Double>) new EitherTypeInfo<String, Double>( BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO); EitherSerializer<String, Double> eitherSerializer = (EitherSerializer<String, Double>) eitherTypeInfo.createSerializer(new ExecutionConfig()); SerializerTestInstance<Either<String, Double>> testInstance = new EitherSerializerTestInstance<Either<String, Double>>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData); testInstance.testAll(); }
@Test public void testMyEitherGenericType() { MapFunction<Boolean, MyEither<Boolean, String>> f = new MyEitherMapper<>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(f, BOOLEAN_TYPE_INFO); assertTrue(ti instanceof EitherTypeInfo); EitherTypeInfo eti = (EitherTypeInfo) ti; assertEquals(BOOLEAN_TYPE_INFO, eti.getLeftType()); assertEquals(STRING_TYPE_INFO, eti.getRightType()); }
@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); // the first copy creates a new instance of Left Either<LongValue, DoubleValue> copy0 = eitherSerializer.copy(left, right); // then the cross-references are used for future copies Either<LongValue, DoubleValue> copy1 = eitherSerializer.copy(right, copy0); Either<LongValue, DoubleValue> copy2 = eitherSerializer.copy(left, copy1); // validate reference equality assertSame(right, copy1); assertSame(copy0, copy2); // validate reference equality of contained objects assertSame(right.right(), copy1.right()); assertSame(copy0.left(), copy2.left()); }
@Test public void testSerializeIndividually() throws IOException { 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); TestOutputView out = new TestOutputView(); eitherSerializer.serialize(left, out); eitherSerializer.serialize(right, out); eitherSerializer.serialize(left, out); TestInputView in = out.getInputView(); // the first deserialization creates a new instance of Left Either<LongValue, DoubleValue> copy0 = eitherSerializer.deserialize(right, in); // then the cross-references are used for future copies Either<LongValue, DoubleValue> copy1 = eitherSerializer.deserialize(copy0, in); Either<LongValue, DoubleValue> copy2 = eitherSerializer.deserialize(copy1, in); // validate reference equality assertSame(right, copy1); assertSame(copy0, copy2); // validate reference equality of contained objects assertSame(right.right(), copy1.right()); assertSame(copy0.left(), copy2.left()); }
@Override public TypeInformation<Either<L, R>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) { TypeInformation<?> leftType = genericParameters.get("L"); TypeInformation<?> rightType = genericParameters.get("R"); if (leftType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'left' type."); } if (rightType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'right' type."); } return new EitherTypeInfo(leftType, rightType); } }
@SuppressWarnings("unchecked") @Test public void testEitherWithTuple() { Either<Tuple2<Long, Long>, Double>[] testData = new Either[] { Either.Left(new Tuple2<>(2L, 9L)), new Left<>(new Tuple2<>(Long.MIN_VALUE, Long.MAX_VALUE)), new Right<>(32.0), Right(Double.MIN_VALUE), Right(Double.MAX_VALUE)}; EitherTypeInfo<Tuple2<Long, Long>, Double> eitherTypeInfo = (EitherTypeInfo<Tuple2<Long, Long>, Double>) new EitherTypeInfo<Tuple2<Long, Long>, Double>( new TupleTypeInfo<Tuple2<Long, Long>>(BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO), BasicTypeInfo.DOUBLE_TYPE_INFO); EitherSerializer<Tuple2<Long, Long>, Double> eitherSerializer = (EitherSerializer<Tuple2<Long, Long>, Double>) eitherTypeInfo.createSerializer(new ExecutionConfig()); SerializerTestInstance<Either<Tuple2<Long, Long>, Double>> testInstance = new EitherSerializerTestInstance<Either<Tuple2<Long, Long>, Double>>( eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData); testInstance.testAll(); }
@SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj instanceof EitherTypeInfo) { EitherTypeInfo<L, R> other = (EitherTypeInfo<L, R>) obj; return other.canEqual(this) && leftType.equals(other.leftType) && rightType.equals(other.rightType); } else { return false; } }
@Override @SuppressWarnings("unchecked") public TypeInformation<MyEither<A,B>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParams) { return new EitherTypeInfo(genericParams.get("A"), genericParams.get("B")); } }
@Test public void testStringValueDoubleValueEither() { @SuppressWarnings("unchecked") Either<StringValue, DoubleValue>[] testData = new Either[] { Left(new StringValue("banana")), Left.of(new StringValue("apple")), new Left(new StringValue("")), Right(new DoubleValue(32.0)), Right.of(new DoubleValue(Double.MIN_VALUE)), new Right(new DoubleValue(Double.MAX_VALUE))}; EitherTypeInfo<StringValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>( ValueTypeInfo.STRING_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO); EitherSerializer<StringValue, DoubleValue> eitherSerializer = (EitherSerializer<StringValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig()); SerializerTestInstance<Either<StringValue, DoubleValue>> testInstance = new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData); testInstance.testAll(); }
@SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj instanceof EitherTypeInfo) { EitherTypeInfo<L, R> other = (EitherTypeInfo<L, R>) obj; return other.canEqual(this) && leftType.equals(other.leftType) && rightType.equals(other.rightType); } else { return false; } }
@Override protected EitherTypeInfo<?, ?>[] getTestData() { return new EitherTypeInfo<?, ?>[] { new EitherTypeInfo<>(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO), new EitherTypeInfo<>(BasicTypeInfo.INT_TYPE_INFO, new TupleTypeInfo<Tuple2<Double, Long>>(TypeExtractor.getForClass(Double.class), TypeExtractor.getForClass(String.class))) }; } }
@Test public void testEitherWithTupleValues() { @SuppressWarnings("unchecked") Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] { Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))), new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))), new Right<>(new DoubleValue(32.0)), Right(new DoubleValue(Double.MIN_VALUE)), Right(new DoubleValue(Double.MAX_VALUE))}; EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>( new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO), ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO); EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer = (EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig()); SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance = new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData); testInstance.testAll(); }
@Test public void testEither() { MapFunction<?, ?> function = new MapFunction<Either<String, Boolean>, Either<String, Boolean>>() { @Override public Either<String, Boolean> map(Either<String, Boolean> value) throws Exception { return null; } }; TypeInformation<?> expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.BOOLEAN_TYPE_INFO); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) function, expected); Assert.assertEquals(expected, ti); }
final TypeInformation<Either<L, R>> outTypeInfo = new EitherTypeInfo<>(timedOutTypeInfo, mainTypeInfo);
false); final TypeInformation<Either<L, R>> outTypeInfo = new EitherTypeInfo<>(timeoutTypeInfo, mainTypeInfo);
@Test public void testEitherHierarchy() { MapFunction<?, ?> function = new EitherMapper<Boolean>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.BOOLEAN_TYPE_INFO); TypeInformation<?> expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.BOOLEAN_TYPE_INFO); Assert.assertEquals(expected, ti); function = new EitherMapper2(); ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.STRING_TYPE_INFO); expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, new TupleTypeInfo(BasicTypeInfo.INT_TYPE_INFO)); Assert.assertEquals(expected, ti); function = new EitherMapper3(); ti = TypeExtractor.getMapReturnTypes((MapFunction) function, expected); Assert.assertEquals(expected, ti); Either<String, Tuple1<Integer>> either = new Either2(); ti = TypeExtractor.getForObject(either); Assert.assertEquals(expected, ti); }
TypeInformation<Vertex<K, VV>> vertexType = initialVertices.getType(); TypeInformation<Either<Vertex<K, VV>, Tuple2<K, Message>>> intermediateTypeInfo = new EitherTypeInfo<>(vertexType, messageTypeInfo); TypeInformation<Either<NullValue, Message>> nullableMsgTypeInfo = new EitherTypeInfo<>(TypeExtractor.getForClass(NullValue.class), messageType); TypeInformation<Tuple2<K, Either<NullValue, Message>>> workSetTypeInfo = new TupleTypeInfo<>(keyType, nullableMsgTypeInfo);
/** * Returns type information for Flink's {@link org.apache.flink.types.Either} type. Null values * are not supported. * * <p>Either type can be used for a value of two possible types. * * <p>Example use: <code>Types.EITHER(Types.VOID, Types.INT)</code> * * @param leftType type information of left side / {@link org.apache.flink.types.Either.Left} * @param rightType type information of right side / {@link org.apache.flink.types.Either.Right} */ public static <L, R> TypeInformation<Either<L, R>> EITHER(TypeInformation<L> leftType, TypeInformation<R> rightType) { return new EitherTypeInfo<>(leftType, rightType); }