support covariance in enum mapping

This commit is contained in:
Jonathan Shook 2024-10-30 13:09:21 -05:00
parent a13b433b23
commit f47498a185
2 changed files with 15 additions and 11 deletions

View File

@ -40,10 +40,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; import java.util.function.*;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
/** /**
* <H1>ParsedOp API</H1> * <H1>ParsedOp API</H1>
@ -1131,19 +1128,26 @@ public class ParsedOp extends NBBaseComponent implements LongFunction<Map<String
* The enhancer function result type * The enhancer function result type
* @return an (optionally) enhanced base function * @return an (optionally) enhanced base function
*/ */
public <FA, FE extends Enum<FE>> LongFunction<FA> enhanceEnumOptionally(
LongFunction<FA> func, public <S extends FA, FA, FE extends Enum<FE>> LongFunction<S> enhanceEnumOptionally(
LongFunction<S> func,
String field, String field,
Class<FE> type, Class<FE> type,
BiFunction<FA, FE, FA> combiner BiFunction<S, FE, S> combiner
) { ) {
Optional<LongFunction<FE>> fieldEnhancerFunc = getAsOptionalEnumFunction(field, type); Optional<LongFunction<FE>> fieldEnhancerFunc = getAsOptionalEnumFunction(field, type);
if (fieldEnhancerFunc.isEmpty()) { if (fieldEnhancerFunc.isEmpty()) {
return func; return func;
} }
LongFunction<FE> feLongFunction = fieldEnhancerFunc.get(); LongFunction<FE> feLongFunction = fieldEnhancerFunc.get();
LongFunction<FA> lfa = l -> combiner.apply(func.apply(l), feLongFunction.apply(l)); return l -> {
return lfa; S initialResult = func.apply(l);
FE enumValue = feLongFunction.apply(l);
return combiner.apply(initialResult, enumValue); // Apply combiner and return the result
};
// LongFunction<S> lfa = l -> combiner.apply(func.apply(l), feLongFunction.apply(l));
// return lfa;
} }
public Map<String, Object> parseStaticCmdMap(String key, String mainField) { public Map<String, Object> parseStaticCmdMap(String key, String mainField) {

View File

@ -533,9 +533,9 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
return getAsRequiredFunction(name, String.class); return getAsRequiredFunction(name, String.class);
} }
public <V extends Enum<V>> Optional<LongFunction<V>> getAsOptionalEnumFunction(String name, Class<V> type) { public <V extends Enum<V>> Optional<LongFunction<V>> getAsOptionalEnumFunction(String name, Class<? extends V> type) {
Optional<LongFunction<String>> nameFunc = this.getAsOptionalFunction(name, String.class); Optional<LongFunction<String>> nameFunc = this.getAsOptionalFunction(name, String.class);
return nameFunc.map((f) -> (l) -> Enum.valueOf(type, f.apply(l))); return nameFunc.map((f) -> (l) -> Enum.valueOf((Class<V>) type, f.apply(l)));
} }
/** /**