@SuppressWarnings("unchecked") @Override @PublicEvolving public TupleSerializer<T> createSerializer(ExecutionConfig executionConfig) { if (getTypeClass() == Tuple0.class) { return (TupleSerializer<T>) Tuple0Serializer.INSTANCE; } TypeSerializer<?>[] fieldSerializers = new TypeSerializer<?>[getArity()]; for (int i = 0; i < types.length; i++) { fieldSerializers[i] = types[i].createSerializer(executionConfig); } Class<T> tupleClass = getTypeClass(); return new TupleSerializer<T>(tupleClass, fieldSerializers); }
private <T extends Tuple> void runTests(int length, T... instances) { try { TupleTypeInfo<T> tupleTypeInfo = (TupleTypeInfo<T>) TypeExtractor.getForObject(instances[0]); TypeSerializer<T> serializer = tupleTypeInfo.createSerializer(new ExecutionConfig()); Class<T> tupleClass = tupleTypeInfo.getTypeClass(); if(tupleClass == Tuple0.class) { length = 1; } TupleSerializerTestInstance<T> test = new TupleSerializerTestInstance<T>(serializer, tupleClass, length, instances); test.testAll(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); Assert.fail(e.getMessage()); } } }
Assert.assertEquals(Tuple2.class, tti.getTypeClass()); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) ti2; Assert.assertEquals(Tuple2.class, tti2.getTypeClass()); Assert.assertEquals(Long.class, tti2.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti2.getTypeAt(1) instanceof PojoTypeInfo);
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testSameGenericVariable() { RichMapFunction<?, ?> function = new RichMapFunction<SameTypeVariable<String>, SameTypeVariable<String>>() { private static final long serialVersionUID = 1L; @Override public SameTypeVariable<String> map(SameTypeVariable<String> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(SameTypeVariable.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testGenericsNotInSuperclass() { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<LongKeyValue<String>, LongKeyValue<String>>() { private static final long serialVersionUID = 1L; @Override public LongKeyValue<String> map(LongKeyValue<String> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<Long, String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(LongKeyValue.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testNestedTupleGenerics() { RichMapFunction<?, ?> function = new RichMapFunction<Nested<String, Integer>, Nested<String, Integer>>() { private static final long serialVersionUID = 1L; @Override public Nested<String, Integer> map(Nested<String, Integer> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, Tuple2<Integer, Integer>>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Nested.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertTrue(tti.getTypeAt(1).isTupleType()); Assert.assertEquals(2, tti.getTypeAt(1).getArity()); // Nested TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) tti.getTypeAt(1); Assert.assertEquals(Tuple2.class, tti2.getTypeClass()); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti2.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti2.getTypeAt(1)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testSubclassOfTuple() { // use getJoinReturnTypes() RichFlatJoinFunction<?, ?, ?> function = new RichFlatJoinFunction<CustomTuple, String, CustomTuple>() { private static final long serialVersionUID = 1L; @Override public void join(CustomTuple first, String second, Collector<CustomTuple> out) throws Exception { out.collect(null); } }; TypeInformation<?> ti = TypeExtractor.getFlatJoinReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, Integer>>(){}), (TypeInformation) Types.STRING); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(1)); Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti).getTypeClass()); // use getForObject() CustomTuple t = new CustomTuple("hello", 1); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(2, ti2.getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(1)); Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti2).getTypeClass()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testChainedGenericsNotInSuperclass() { // use TypeExtractor RichMapFunction<?, ?> function = new RichMapFunction<ChainedTwo<Integer>, ChainedTwo<Integer>>() { private static final long serialVersionUID = 1L; @Override public ChainedTwo<Integer> map(ChainedTwo<Integer> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<String, Long, Integer>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(ChainedTwo.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti.getTypeAt(2)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testGenericsNotInSuperclassWithNonGenericClassAtEnd() { // use TypeExtractor RichMapFunction<?, ?> function = new RichMapFunction<ChainedFour, ChainedFour>() { private static final long serialVersionUID = 1L; @Override public ChainedFour map(ChainedFour value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<String, Long, String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(ChainedFour.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(2)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testGenericsInDirectSuperclass() { // use TypeExtractor RichMapFunction<?, ?> function = new RichMapFunction<ChainedThree, ChainedThree>() { private static final long serialVersionUID = 1L; @Override public ChainedThree map(ChainedThree value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<String, Long, String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(ChainedThree.class, tti.getTypeClass()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(2)); }
Assert.assertEquals(Tuple3.class, tti.getTypeClass());
Assert.assertEquals(Tuple9.class, tti.getTypeClass());
@SuppressWarnings("unchecked") @Override @PublicEvolving public TupleSerializer<T> createSerializer(ExecutionConfig executionConfig) { if (getTypeClass() == Tuple0.class) { return (TupleSerializer<T>) Tuple0Serializer.INSTANCE; } TypeSerializer<?>[] fieldSerializers = new TypeSerializer<?>[getArity()]; for (int i = 0; i < types.length; i++) { fieldSerializers[i] = types[i].createSerializer(executionConfig); } Class<T> tupleClass = getTypeClass(); return new TupleSerializer<T>(tupleClass, fieldSerializers); }
@SuppressWarnings("unchecked") @Override @PublicEvolving public TupleSerializer<T> createSerializer(ExecutionConfig executionConfig) { if (getTypeClass() == Tuple0.class) { return (TupleSerializer<T>) Tuple0Serializer.INSTANCE; } TypeSerializer<?>[] fieldSerializers = new TypeSerializer<?>[getArity()]; for (int i = 0; i < types.length; i++) { fieldSerializers[i] = types[i].createSerializer(executionConfig); } Class<T> tupleClass = getTypeClass(); return new TupleSerializer<T>(tupleClass, fieldSerializers); }