
com.fluxtion.runtime.dataflow.groupby.GroupByMapFlowFunction Maven / Gradle / Ivy
package com.fluxtion.runtime.dataflow.groupby;
import com.fluxtion.runtime.annotations.builder.SepNode;
import com.fluxtion.runtime.dataflow.groupby.GroupBy.KeyValue;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableBiFunction;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableFunction;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
public class GroupByMapFlowFunction {
private final SerializableFunction mapFunction;
private final SerializableBiFunction mapFrom2MapsBiFunction;
private final transient GroupByHashMap outputCollection = new GroupByHashMap();
private final transient GroupByView wrappedCollection = new GroupByView();
@SepNode
public Object defaultValue;
public GroupByMapFlowFunction(SerializableFunction mapFunction) {
this(mapFunction, null);
}
public GroupByMapFlowFunction(SerializableBiFunction mapFrom2MapsBiFunction) {
this(null, mapFrom2MapsBiFunction);
}
public GroupByMapFlowFunction(SerializableFunction mapFunction, SerializableBiFunction mapFrom2MapsBiFunction) {
this.mapFunction = mapFunction;
this.mapFrom2MapsBiFunction = mapFrom2MapsBiFunction;
}
public GroupByMapFlowFunction(SerializableFunction mapFunction, SerializableBiFunction mapFrom2MapsBiFunction, Object defaultValue) {
this.mapFunction = mapFunction;
this.mapFrom2MapsBiFunction = mapFrom2MapsBiFunction;
this.defaultValue = defaultValue;
}
//required for serialised version
public GroupBy mapValues(Object inputMap) {
return mapValues((GroupBy) inputMap);
}
public GroupBy mapForEachValues(Object inputMap) {
return mapForEachValues((GroupBy) inputMap);
}
public GroupBy mapKeyedValue(Object inputMap, Object secondArgument) {
return mapKeyedValue((GroupBy) inputMap, secondArgument);
}
public GroupBy mapValueWithKeyValue(Object inputMap, KeyValue secondArgument) {
return mapValueWithKeyValue((GroupBy) inputMap, secondArgument);
}
public GroupBy biMapValuesWithParamMap(Object firstArgGroupBy, Object secondArgGroupBY) {
return biMapValuesWithParamMap((GroupBy) firstArgGroupBy, (GroupBy) secondArgGroupBY);
}
public GroupBy mapKeys(Object inputMap) {
return mapKeys((GroupBy) inputMap);
}
public GroupBy mapEntry(Object inputMap) {
return mapEntry((GroupBy) inputMap);
}
public GroupBy mapForEachValues(GroupBy inputMap) {
//make this recursive on value type == GroupBy?
//TODO FIX
throw new UnsupportedOperationException("not implemented");
// return mapValues(inputMap);
}
public GroupBy mapValues(GroupBy inputMap) {
outputCollection.reset();
inputMap.toMap().entrySet().forEach(e -> {
Map.Entry entry = (Entry) e;
outputCollection.toMap().put(entry.getKey(), mapFunction.apply(entry.getValue()));
});
return outputCollection;
}
public GroupBy mapKeys(GroupBy inputMap) {
outputCollection.reset();
inputMap.toMap().entrySet().forEach(e -> {
Map.Entry entry = (Entry) e;
outputCollection.toMap().put(mapFunction.apply(entry.getKey()), entry.getValue());
});
return outputCollection;
}
public GroupBy mapEntry(GroupBy inputMap) {
outputCollection.reset();
inputMap.toMap().entrySet().forEach(e -> {
Map.Entry entry = (Entry) mapFunction.apply(e);
outputCollection.toMap().put(entry.getKey(), entry.getValue());
});
return outputCollection;
}
public GroupBy mapKeyedValue(G inputMap, Object argumentProvider) {
wrappedCollection.reset();
Object key = mapFunction.apply(argumentProvider);
Object item = inputMap.toMap().get(key);
if (item != null) {
KeyValue kv = new KeyValue(key, mapFrom2MapsBiFunction.apply(item, argumentProvider));
outputCollection.fromMap(inputMap.toMap());
outputCollection.add(kv);
wrappedCollection.setGroupBy(outputCollection);
wrappedCollection.setKeyValue(kv);
}
return wrappedCollection;
}
public GroupBy mapValueWithKeyValue(G inputMap, KeyValue argumentProvider) {
wrappedCollection.reset();
Object key = argumentProvider.getKey();
Object item = inputMap.toMap().get(key);
if (item != null) {
KeyValue kv = new KeyValue(key, mapFrom2MapsBiFunction.apply(item, argumentProvider.getValue()));
outputCollection.fromMap(inputMap.toMap());
outputCollection.add(kv);
wrappedCollection.setGroupBy(outputCollection);
wrappedCollection.setKeyValue(kv);
}
return wrappedCollection;
}
public GroupBy biMapValuesWithParamMap(G firstArgGroupBy, H secondArgGroupBY) {
outputCollection.reset();
Map arg2Map = (secondArgGroupBY == null && defaultValue != null) ? Collections.emptyMap() : secondArgGroupBY.toMap();
firstArgGroupBy.toMap().forEach((key, arg1) -> {
Object arg2 = arg2Map.getOrDefault(key, defaultValue);
if (arg2 != null) {
outputCollection.toMap().put(key, mapFrom2MapsBiFunction.apply(arg1, arg2));
}
});
return outputCollection;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy