@Override public Either<L, R> copy(Either<L, R> from, Either<L, R> reuse) { if (from.isLeft()) { Left<L, R> to = Either.obtainLeft(reuse, leftSerializer); L left = leftSerializer.copy(from.left(), to.left()); to.setValue(left); return to; } else { Right<L, R> to = Either.obtainRight(reuse, rightSerializer); R right = rightSerializer.copy(from.right(), to.right()); to.setValue(right); return to; } }
@Override public Either<L, R> map1(R value) { return Either.Right(value); }
@Override public Either<L, R> map2(L value) { return Either.Left(value); } }
@Override public final Message next() { if (first != null) { Message toReturn = first; first = null; return toReturn; } return this.source.next().f1.right(); }
/** * Utility function for {@link EitherSerializer} to support object reuse. * * To support object reuse both subclasses of Either contain a reference to * an instance of the other type. This method provides access to and * initializes the cross-reference. * * @param input container for Left or Right value * @param leftSerializer for creating an instance of the left type * @param <L> * the type of Left * @param <R> * the type of Right * @return input if Left type else input's Left reference */ @Internal public static <L, R> Left<L, R> obtainLeft(Either<L, R> input, TypeSerializer<L> leftSerializer) { if (input.isLeft()) { return (Left<L, R>) input; } else { Right<L, R> right = (Right<L, R>) input; if (right.left == null) { right.left = Left.of(leftSerializer.createInstance()); right.left.right = right; } return right.left; } }
/** * Utility function for {@link EitherSerializer} to support object reuse. * * To support object reuse both subclasses of Either contain a reference to * an instance of the other type. This method provides access to and * initializes the cross-reference. * * @param input container for Left or Right value * @param rightSerializer for creating an instance of the right type * @param <L> * the type of Left * @param <R> * the type of Right * @return input if Right type else input's Right reference */ @Internal public static <L, R> Right<L, R> obtainRight(Either<L, R> input, TypeSerializer<R> rightSerializer) { if (input.isRight()) { return (Right<L, R>) input; } else { Left<L, R> left = (Left<L, R>) input; if (left.right == null) { left.right = Right.of(rightSerializer.createInstance()); left.right.left = left; } return left.right; } } }
@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(); }
@Override public void processElement( Either<DepositEvent, TransactionEvent> depositOrTransaction, Context context, Collector<DepositEvent> out) { if (depositOrTransaction.isLeft()) { out.collect(depositOrTransaction.left()); } else { context.output(transactionsSideOutput, depositOrTransaction.right()); } } });
@Override public Either<L, R> copy(Either<L, R> from, Either<L, R> reuse) { if (from.isLeft()) { Left<L, R> to = Either.obtainLeft(reuse, leftSerializer); L left = leftSerializer.copy(from.left(), to.left()); to.setValue(left); return to; } else { Right<L, R> to = Either.obtainRight(reuse, rightSerializer); R right = rightSerializer.copy(from.right(), to.right()); to.setValue(right); return to; } }
/** * Sends the given message to the vertex identified by the given key. If the target vertex does not exist, * the next superstep will cause an exception due to a non-deliverable message. * * @param target The key (id) of the target vertex to message. * @param m The message. */ public final void sendMessageTo(K target, Message m) { outMsg.f0 = target; outMsg.f1 = m; out.collect(Either.Right(outMsg)); }
/** * Sets the new value of this vertex. * * <p>This should be called at most once per ComputeFunction. * * @param newValue The new vertex value. */ public final void setNewVertexValue(VV newValue) { if (setNewVertexValueCalled) { throw new IllegalStateException("setNewVertexValue should only be called at most once per updateVertex"); } setNewVertexValueCalled = true; outVertex.f1 = newValue; out.collect(Either.Left(outVertex)); }
@Override public void reduce(Iterable<Tuple2<K, Either<NullValue, Message>>> messages, Collector<Tuple2<K, Either<NullValue, Message>>> out) throws Exception { final Iterator<Tuple2<K, Either<NullValue, Message>>> messageIterator = messages.iterator(); if (messageIterator.hasNext()) { final Tuple2<K, Either<NullValue, Message>> first = messageIterator.next(); final K vertexID = first.f0; final MessageIterator<Message> messageIter = new MessageIterator<>(); messageIter.setFirst(first.f1.right()); @SuppressWarnings("unchecked") Iterator<Tuple2<?, Either<NullValue, Message>>> downcastIter = (Iterator<Tuple2<?, Either<NullValue, Message>>>) (Iterator<?>) messageIterator; messageIter.setSource(downcastIter); combinerFunction.set(vertexID, out); combinerFunction.combineMessages(messageIter); } }
/** * Utility function for {@link EitherSerializer} to support object reuse. * * To support object reuse both subclasses of Either contain a reference to * an instance of the other type. This method provides access to and * initializes the cross-reference. * * @param input container for Left or Right value * @param leftSerializer for creating an instance of the left type * @param <L> * the type of Left * @param <R> * the type of Right * @return input if Left type else input's Left reference */ @Internal public static <L, R> Left<L, R> obtainLeft(Either<L, R> input, TypeSerializer<L> leftSerializer) { if (input.isLeft()) { return (Left<L, R>) input; } else { Right<L, R> right = (Right<L, R>) input; if (right.left == null) { right.left = Left.of(leftSerializer.createInstance()); right.left.right = right; } return right.left; } }