org.apache.flink.types.Either类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(157)

本文整理了Java中org.apache.flink.types.Either类的一些代码示例,展示了Either类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Either类的具体详情如下:
包路径:org.apache.flink.types.Either
类名称:Either

Either介绍

[英]This type represents a value of one two possible types, Left or Right (a disjoint union), inspired by Scala's Either type.
[中]此类型表示一个值,即两种可能的类型,左或右(不相交的并集),其灵感来自Scala的任意一种类型。

代码示例

代码示例来源:origin: apache/flink

@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);
  }
}

代码示例来源:origin: apache/flink

public void flatMap(Either<Vertex<K, VV>, Tuple2<K, Message>> value,
      Collector<Tuple2<K, Either<NullValue, Message>>> out) {
    if (value.isRight()) {
      Tuple2<K, Message> message = value.right();
      outTuple.f0 = message.f0;
      outTuple.f1 = Either.Right(message.f1);
      out.collect(outTuple);
    }
  }
}

代码示例来源:origin: apache/flink

public VGV getVertexGroupValue() {
  return f2.isLeft() ? f2.left() : null;
}

代码示例来源:origin: apache/flink

@Override
public Either<L, R> deserialize(DataInputView source) throws IOException {
  boolean isLeft = source.readBoolean();
  if (isLeft) {
    return Left(leftSerializer.deserialize(source));
  }
  else {
    return Right(rightSerializer.deserialize(source));
  }
}

代码示例来源:origin: apache/flink

@Override
public void serialize(Either<L, R> record, DataOutputView target) throws IOException {
  if (record.isLeft()) {
    target.writeBoolean(true);
    leftSerializer.serialize(record.left(), target);
  }
  else {
    target.writeBoolean(false);
    rightSerializer.serialize(record.right(), target);
  }
}

代码示例来源:origin: apache/flink

@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;
  }
}

代码示例来源:origin: apache/flink

@Override
public Either<L, R> map1(R value) {
  return Either.Right(value);
}

代码示例来源:origin: apache/flink

@Override
  public Either<L, R> map2(L value) {
    return Either.Left(value);
  }
}

代码示例来源:origin: apache/flink

@Override
public final Message next() {
  if (first != null) {
    Message toReturn = first;
    first = null;
    return toReturn;
  }
  return this.source.next().f1.right();
}

代码示例来源:origin: apache/flink

/**
 * 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;
  }
}

代码示例来源:origin: stackoverflow.com

public static void thirdParty(Their their, Either either) {
  if (either.isLeft())
    their.thirdPartyExpectsString(either.getLeft());
  else
    their.thirdPartyExpectsDouble(either.getRight());
}

代码示例来源:origin: apache/flink

/**
   * 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;
    }
  }
}

代码示例来源:origin: apache/flink

@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();
}

代码示例来源:origin: dataArtisans/da-streamingledger

@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());
    }
  }
});

代码示例来源:origin: org.apache.flink/flink-core

@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;
  }
}

代码示例来源:origin: apache/flink

public void flatMap(Either<Vertex<K, VV>, Tuple2<K, Message>> value,
      Collector<Vertex<K, VV>> out) {
    if (value.isLeft()) {
      out.collect(value.left());
    }
  }
}

代码示例来源:origin: apache/flink

/**
 * 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));
}

代码示例来源:origin: apache/flink

/**
 * 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));
}

代码示例来源:origin: apache/flink

@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);
  }
}

代码示例来源:origin: org.apache.flink/flink-core

/**
 * 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;
  }
}

相关文章

微信公众号

最新文章

更多