java.util.LinkedHashMap#replace ( )源码实例Demo

下面列出了java.util.LinkedHashMap#replace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: buck   文件: ExceptionHandlerRegistryFactory.java
/**
 * Overriding is defined such that the order of the existing handlers are unaltered. When handlers
 * handle the same exception, the new one will replace the existing handler. New handlers not
 * present in the mapping will be appended at the end of the handling chain.
 *
 * @param exceptionHandlers the defaultHandlers to override some default handler
 * @return new {@link ExceptionHandlerRegistry} with the default defaultHandlers overridden by the
 *     given ones
 */
@SafeVarargs
public static ExceptionHandlerRegistry<ExitCode> create(
    ExceptionHandler<? extends Throwable, ExitCode>... exceptionHandlers) {
  LinkedHashMap<Class<? extends Throwable>, ExceptionHandler<? extends Throwable, ExitCode>>
      currentHandlers = new LinkedHashMap<>(defaultHandlers);
  for (int i = 0; i < exceptionHandlers.length; i++) {
    if (currentHandlers.replace(exceptionHandlers[i].getExceptionType(), exceptionHandlers[i])
        == null) {
      currentHandlers.put(exceptionHandlers[i].getExceptionType(), exceptionHandlers[i]);
    }
  }
  return new ExceptionHandlerRegistry<>(
      ImmutableList.copyOf(currentHandlers.values()),
      new ExceptionHandler<Throwable, ExitCode>(Throwable.class) {
        @Override
        public ExitCode handleException(Throwable t) {
          return ExitCode.FATAL_GENERIC;
        }
      });
}
 
@Override
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getChannelHandlers(MessageInput input) {
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlers = new LinkedHashMap<>(super.getChannelHandlers(input));

    // Replace the default "codec-aggregator" handler with one that passes the remote address
    final RemoteAddressCodecAggregator aggregator = (RemoteAddressCodecAggregator) getAggregator();
    handlers.replace("codec-aggregator", () -> new NetflowMessageAggregationHandler(aggregator, localRegistry));
    handlers.remove("udp-datagram");

    return handlers;
}
 
private void upgradeV3ToV4(List<Config> configs) {
  List<Config> configsToRemove = new ArrayList<>();
  List<Config> configsToAdd = new ArrayList<>();
  for (Config config : configs) {
    switch (config.getName()) {
      case RECORD_HASHER_CONFIG_TYPE:
        if (config.getValue().equals(SHA2)) {
          configsToRemove.add(config);
          configsToAdd.add(new Config(RECORD_HASHER_CONFIG_TYPE, HashType.SHA256.name()));
        }
        break;
      case HASHER_CONFIG_IN_PLACE_FIELD:
      case HASHER_CONFIG_TARGET_FIELD:
        List<LinkedHashMap<String, Object>> fieldHasherConfigs =
            (List<LinkedHashMap<String, Object>>) config.getValue();
        for (LinkedHashMap<String, Object> fieldHasherConfig : fieldHasherConfigs) {
          if (fieldHasherConfig.get(HASH_TYPE).equals(SHA2)) {
            fieldHasherConfig.replace(HASH_TYPE, HashType.SHA256.name());
          }
        }
        break;
      default:
        break;  // NO OP for others
    }
  }
  configs.removeAll(configsToRemove);
  configs.addAll(configsToAdd);
}