All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.scalecube.config.MultimapConfigPropertyImpl Maven / Gradle / Ivy

The newest version!
package io.scalecube.config;

import io.scalecube.config.source.LoadedConfigProperty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

class MultimapConfigPropertyImpl extends AbstractSimpleConfigProperty>>
    implements MultimapConfigProperty {

  static  Function>> toMultimapPropertyParser(
      Function valueParser) {
    return str -> {
      Map> result = new HashMap<>();
      String[] tokens = str.split(",");
      String key = null;
      for (String token : tokens) {
        String[] entry = token.split("=", 2);
        String value;
        if (entry.length > 1) { // entry ["key", "value"]
          key = entry[0];
          value = entry[1];
        } else { // only value ["value"]
          value = entry[0];
        }
        if (key != null) {
          result.computeIfAbsent(key, k -> new ArrayList<>()).add(valueParser.apply(value));
        }
      }
      return result;
    };
  }

  MultimapConfigPropertyImpl(
      String name,
      Map propertyMap,
      Map> propertyCallbackMap,
      Function valueParser) {
    super(
        name,
        getMapPropertyClass(valueParser),
        propertyMap,
        propertyCallbackMap,
        toMultimapPropertyParser(valueParser));
  }

  @Override
  public Map> value(Map> defaultValue) {
    return value().orElse(defaultValue);
  }

  @Override
  public Map> valueOrThrow() {
    return value().orElseThrow(this::newNoSuchElementException);
  }

  private static  Class getMapPropertyClass(Function valueParser) {
    Class result = null;
    if (ConfigRegistryImpl.STRING_PARSER == valueParser) {
      result = StringMultimap.class;
    } else if (ConfigRegistryImpl.DOUBLE_PARSER == valueParser) {
      result = DoubleMultimap.class;
    } else if (ConfigRegistryImpl.LONG_PARSER == valueParser) {
      result = LongMultimap.class;
    } else if (ConfigRegistryImpl.INT_PARSER == valueParser) {
      result = IntMultimap.class;
    } else if (ConfigRegistryImpl.DURATION_PARSER == valueParser) {
      result = DurationMultimap.class;
    }
    if (result == null) {
      throw new IllegalArgumentException(
          "MultimapConfigPropertyImpl: unsupported multimap valueParser " + valueParser);
    }
    return result;
  }

  private static class StringMultimap {}

  private static class DoubleMultimap {}

  private static class LongMultimap {}

  private static class IntMultimap {}

  private static class DurationMultimap {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy