@Override public Option<Traversable<V>> get(K key) { return back.get(key); }
@SuppressWarnings("unchecked") private <K2, V2> Multimap<K2, V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) { Map<K2, Traversable<V2>> back = emptyMapSupplier(); for (Tuple2<? extends K2, ? extends V2> entry : entries) { if (back.containsKey(entry._1)) { back = back.put(entry._1, containerType.add(back.get(entry._1).get(), entry._2)); } else { back = back.put(entry._1, containerType.add(emptyContainer.get(), entry._2)); } } return createFromMap(back); }
@Override public java.util.Map<String, T> handleChildKeys(Map<String, ?> childKeys, String pathPrefix) { java.util.Map<String, T> returnedMap = new HashMap<>(); childKeys.groupBy(tuple2 -> tuple2._1.split("\\.", 2)[0]).map((key, values) -> { // Drop the prefix Map<String, ?> valuesWithoutPrefix = values.map((configKey, configValue) -> Tuple.of(configKey.substring(key.length() + 1), configValue)); T value; try { value = Deserializer.deserialize(_type, valuesWithoutPrefix, ""); } catch (Exception e) { value = null; e.printStackTrace(); } return Tuple.of(key, value); }).forEach(returnedMap::put); if (returnedMap.isEmpty()) { return null; } else { return returnedMap; } }
@SuppressWarnings("unchecked") static <K, V, M extends Map<K, V>> M replaceValue(M map, K key, V value) { return map.containsKey(key) ? (M) map.put(key, value) : map; }
@SuppressWarnings("unchecked") static <K, V, M extends Map<K, V>> M replace(M map, Tuple2<K, V> currentElement, Tuple2<K, V> newElement) { Objects.requireNonNull(currentElement, "currentElement is null"); Objects.requireNonNull(newElement, "newElement is null"); return (M) (map.containsKey(currentElement._1) ? map.remove(currentElement._1).put(newElement) : map); }
@SuppressWarnings("unchecked") static <K, V, U extends V, M extends Map<K, V>> M put(M map, K key, U value, BiFunction<? super V, ? super U, ? extends V> merge) { Objects.requireNonNull(merge, "the merge function is null"); final Option<V> currentValue = map.get(key); if (currentValue.isEmpty()) { return (M) map.put(key, value); } else { return (M) map.put(key, merge.apply(currentValue.get(), value)); } }
@SuppressWarnings("unchecked") @Override public M remove(K key, V value) { final Traversable<V> values = back.get(key).getOrElse((Traversable<V>) emptyContainer.get()); final Traversable<V> newValues = containerType.remove(values, value); if (newValues == values) { return (M) this; } else if (newValues.isEmpty()) { return (M) createFromMap(back.remove(key)); } else { return (M) createFromMap(back.put(key, newValues)); } }
@SuppressWarnings("unchecked") static <K, V, U extends V, M extends Map<K, V>> M merge( M map, OfEntries<K, V, M> ofEntries, Map<? extends K, U> that, BiFunction<? super V, ? super U, ? extends V> collisionResolution) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(collisionResolution, "collisionResolution is null"); if (map.isEmpty()) { return ofEntries.apply(Map.narrow(that)); } else if (that.isEmpty()) { return map; } else { return that.foldLeft(map, (result, entry) -> { final K key = entry._1; final U value = entry._2; final V newValue = result.get(key).map(v -> (V) collisionResolution.apply(v, value)).getOrElse(value); return (M) result.put(key, newValue); }); } }
@Override public Map<String, ?> apply(Map<String, ?> config, String keyPrefix) { Option<String> tableName = config.get("table.name").map(Object::toString); if (tableName.isDefined() && config.getOrElse("table.schemaName", null) == null) { config = ((Map<String, Object>) config).put("table.schemaName", tableName.get()); final boolean hasRealtime = config.containsKey("table.type.realtime"); final boolean hasOffline = config.containsKey("table.type.offline"); Map<String, Object> remappedConfig = config.flatMap((k, v) -> { if (k.startsWith("table.schema.")) {
Set<String> profileKeys = configMap.keySet().filter(key -> key.contains(PROFILE_SEPARATOR)).toSet(); String destinationKey = enabledProfileKey.substring(0, lastUnderscoreIndex); if (!overrideConfigMap.containsKey(destinationKey)) { overrideConfigMap = overrideConfigMap.put(Tuple.of(destinationKey, config.getValue(enabledProfileKey))); } else { ConfigValue previousOverrideValue = overrideConfigMap.get(destinationKey).get(); ConfigValue newConfigValue = config.getValue(enabledProfileKey); if (!EqualityUtils.isEqual(previousOverrideValue.unwrapped(), newConfigValue.unwrapped())) { config = ConfigFactory.parseMap(overrideConfigMap.toJavaMap()).withFallback(config);
@SuppressWarnings("unchecked") static <K, V, K2, U extends Map<K2, V>> U mapKeys(Map<K, V> source, U zero, Function<? super K, ? extends K2> keyMapper, BiFunction<? super V, ? super V, ? extends V> valueMerge) { Objects.requireNonNull(zero, "zero is null"); Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMerge, "valueMerge is null"); return source.foldLeft(zero, (acc, entry) -> { final K2 k2 = keyMapper.apply(entry._1); final V v2 = entry._2; final Option<V> v1 = acc.get(k2); final V v = v1.isDefined() ? valueMerge.apply(v1.get(), v2) : v2; return (U) acc.put(k2, v); }); }
private void renderJsonLinks( ObjectNode objectNode, ResourceRepresentation<?> representation, boolean embedded) { if (!representation.getLinks().isEmpty() || (!embedded && !representation.getNamespaces().isEmpty())) { representation .getNamespaces() .map(ns -> Links.create(CURIES, ns._2, "name", ns._1))); objectNode.set(LINKS, linksNode); for (Tuple2<String, List<Link>> linkEntry : links.groupBy(Links::getRel).toList()) { Rel rel = representation.getRels().get(linkEntry._1).get(); boolean coalesce = !isCollection(rel) && (isSingleton(rel) || linkEntry._2.size() == 1);
@Override public Map<String, ?> apply(Map<String, ?> config, String keyPrefix) { // Generate keys for table types based on table.types // eg. table.types=[offline,realtime] -> table.type.realtime=realtime, table.type.offline=offline config = ((Map<String, Object>) config).merge(config.get("table.types").map(typeList -> { if (typeList instanceof String) { return List.of((String) typeList); } else if (typeList instanceof Collection) { return List.ofAll((Collection<String>) typeList); } else { return List.empty(); } }).map(typeList -> typeList.map(type -> Tuple.of("table.type." + type.toString().toLowerCase(), type)) .toMap(Function.identity())).getOrElse(HashMap::empty)); config = config.remove("table.types"); return config; }
private static void assertMetaDataParsedCorrectly(DataFrame df) { Map<String, String> dataFrameMetaData = df.getMetaData(); assertEquals(1, dataFrameMetaData.size()); assertEquals(Option.of("netzwerg"), dataFrameMetaData.get("author")); Map<String, String> columnMetaData = df.getColumn(df.getColumnId(2, ColumnType.DOUBLE)).getMetaData(); assertEquals(1, columnMetaData.size()); assertEquals(Option.of("m"), columnMetaData.get("unit")); }
Map<String, Object> remappedConfig = (Map<String, Object>) config.mapKeys(key -> { if (key.startsWith("schema.")) { remappedConfig.keySet().filter(key -> key.startsWith("table.") && key.endsWith(".realtime")) .map(key -> key.substring(0, key.lastIndexOf(".realtime"))); remappedConfig.keySet().filter(key -> key.startsWith("table.") && key.endsWith(".offline")) .map(key -> key.substring(0, key.lastIndexOf(".offline"))); String offlineKey = commonOfflineAndRealtimeKey + ".offline"; Object realtimeValue = remappedConfig.getOrElse(realtimeKey, null); Object offlineValue = remappedConfig.getOrElse(offlineKey, null); remappedConfig.remove(realtimeKey).remove(offlineKey).put(commonOfflineAndRealtimeKey, offlineValue);
@Override public Map<String, ?> unapply(Map<String, ?> config, String keyPrefix) { Map<String, ?> tableTypeMap = config.filterKeys(key -> key.startsWith("table.type.")); java.util.List<String> tableTypes = tableTypeMap.values().map(Object::toString).toSet().toJavaList(); Map<String, Object> remappedConfig = ((Map<String, Object>) config).removeAll(tableTypeMap.keySet()).put("table.types", tableTypes); return remappedConfig; } }
Map<String, Object> dslValue = dslValues.getOrElse(keyName, HashMap.empty()); dslValue = dslValue.put(useDsl.value(), field.get(object)); dslValues = dslValues.put(keyName, dslValue); dslClasses = dslClasses.put(keyName, useDsl.dsl()); } else if (useChildKeyHandler != null) { ChildKeyHandler childKeyHandler = useChildKeyHandler.value().newInstance(); Map serializedChildMap = childKeyHandler.unhandleChildKeys(field.get(object), keyName); if (serializedChildMap != null) { serializedChildMap = serializedChildMap.map((key, value) -> Tuple.of(keyName + "." + key, value)); values = values.merge(serializedChildMap); values = values.merge(serialize(field.get(object), field.getType(), keyName)); values = values.merge(serialize(field.get(object), field.getType(), pathContext)); final Map<String, String> dslUnparsedValues = dslValues.flatMap((configKey, dslValueData) -> { try { Class<? extends SingleKeyDsl> dslClass = finalDslClasses.getOrElse(configKey, null); SingleKeyDsl dslInstance = dslClass.newInstance(); Class<?> dslValueType = dslClass.getMethod("parse", String.class).getReturnType(); values = values.merge(dslUnparsedValues);
@SuppressWarnings("unchecked") static <K, V, M extends Map<K, V>> M put(M map, Tuple2<? extends K, ? extends V> entry) { Objects.requireNonNull(entry, "entry is null"); return (M) map.put(entry._1, entry._2); }
static <K, V, M extends Map<K, V>> M merge(M map, OfEntries<K, V, M> ofEntries, Map<? extends K, ? extends V> that) { Objects.requireNonNull(that, "that is null"); if (map.isEmpty()) { return ofEntries.apply(Map.narrow(that)); } else if (that.isEmpty()) { return map; } else { return that.foldLeft(map, (result, entry) -> !result.containsKey(entry._1) ? put(result, entry) : result); } }