play.libs.Scala.partialFunction()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(151)

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

Scala.partialFunction介绍

[英]Create a Scala PartialFunction from a function. A PartialFunction is one that isn't defined for the whole of its domain. If the function isn't defined for a particular input parameter, it can throw F.noMatch(), and this will be translated into the semantics of a Scala PartialFunction. For example:

Flow<String, Integer, ?> collectInts = Flow.<String>collect(Scala.partialFunction( str -> { 
try { 
return Integer.parseInt(str); 
} catch (NumberFormatException e) { 
throw Scala.noMatch(); 
} 
}));

The above code will convert a flow of String into a flow of Integer, dropping any strings that can't be parsed as integers.
[中]从函数创建Scala PartialFunction。PartialFunction是一个没有为其整个域定义的函数。如果函数没有为特定的输入参数定义,它可以抛出[$0$],这将被转换为Scala PartialFunction的语义。例如:

Flow<String, Integer, ?> collectInts = Flow.<String>collect(Scala.partialFunction( str -> { 
try { 
return Integer.parseInt(str); 
} catch (NumberFormatException e) { 
throw Scala.noMatch(); 
} 
}));

上述代码将字符串流转换为整数流,删除任何无法解析为整数的字符串。

代码示例

代码示例来源:origin: com.typesafe.play/play_2.12

/**
 * Acceptor for JSON WebSockets.
 *
 * @param in The class of the incoming messages, used to decode them from the JSON.
 * @param <In> The websocket's input type (what it receives from clients)
 * @param <Out> The websocket's output type (what it writes to clients)
 * @return The WebSocket acceptor.
 */
public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) {
  return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> {
    try {
      if (message instanceof Message.Binary) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in));
      } else if (message instanceof Message.Text) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in));
      }
    } catch (Exception e) {
      return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage()));
    }
    throw Scala.noMatch();
  }), outMessage -> {
    try {
      return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  });
}

代码示例来源:origin: com.typesafe.play/play_2.11

/**
 * Acceptor for JSON WebSockets.
 *
 * @param in The class of the incoming messages, used to decode them from the JSON.
 * @param <In> The websocket's input type (what it receives from clients)
 * @param <Out> The websocket's output type (what it writes to clients)
 * @return The WebSocket acceptor.
 */
public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) {
  return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> {
    try {
      if (message instanceof Message.Binary) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in));
      } else if (message instanceof Message.Text) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in));
      }
    } catch (Exception e) {
      return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage()));
    }
    throw Scala.noMatch();
  }), outMessage -> {
    try {
      return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  });
}

代码示例来源:origin: com.typesafe.play/play

/**
 * Acceptor for JSON WebSockets.
 *
 * @param in The class of the incoming messages, used to decode them from the JSON.
 * @param <In> The websocket's input type (what it receives from clients)
 * @param <Out> The websocket's output type (what it writes to clients)
 * @return The WebSocket acceptor.
 */
public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) {
  return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> {
    try {
      if (message instanceof Message.Binary) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in));
      } else if (message instanceof Message.Text) {
        return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in));
      }
    } catch (Exception e) {
      return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage()));
    }
    throw Scala.noMatch();
  }), outMessage -> {
    try {
      return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  });
}

代码示例来源:origin: com.typesafe.play/play_2.12

UniformFanInShape<Out, Out> merge = builder.add(mergeStrategy);
Flow<F.Either<FlowIn, Out>, FlowIn, ?> collectIn = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.left.isPresent()) {
    return x.left.get();
Flow<F.Either<FlowIn, Out>, Out, ?> collectOut = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.right.isPresent()) {
    return x.right.get();

代码示例来源:origin: com.typesafe.play/play

UniformFanInShape<Out, Out> merge = builder.add(mergeStrategy);
Flow<F.Either<FlowIn, Out>, FlowIn, ?> collectIn = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.left.isPresent()) {
    return x.left.get();
Flow<F.Either<FlowIn, Out>, Out, ?> collectOut = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.right.isPresent()) {
    return x.right.get();

代码示例来源:origin: com.typesafe.play/play_2.11

UniformFanInShape<Out, Out> merge = builder.add(mergeStrategy);
Flow<F.Either<FlowIn, Out>, FlowIn, ?> collectIn = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.left.isPresent()) {
    return x.left.get();
Flow<F.Either<FlowIn, Out>, Out, ?> collectOut = Flow.<F.Either<FlowIn, Out>>create().collect(Scala.partialFunction(x -> {
  if (x.right.isPresent()) {
    return x.right.get();

相关文章