Java Code Examples for java.util.LinkedHashMap#replace()

The following examples show how to use java.util.LinkedHashMap#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExceptionHandlerRegistryFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
        }
      });
}
 
Example 2
Source File: NetFlowUdpTransport.java    From graylog-plugin-netflow with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: FieldHasherProcessorUpgrader.java    From datacollector with Apache License 2.0 5 votes vote down vote up
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);
}