add primitive int lambda for op fields

This commit is contained in:
Jonathan Shook 2024-10-23 13:40:00 -05:00
parent 7b8e9145d7
commit d5ec597152
2 changed files with 54 additions and 29 deletions

View File

@ -43,6 +43,7 @@ import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
/**
* <H1>ParsedOp API</H1>
@ -662,6 +663,9 @@ public class ParsedOp extends NBBaseComponent implements LongFunction<Map<String
public <V> LongFunction<V> getAsFunctionOr(String name, V defaultValue) {
return tmap.getAsFunctionOr(name, defaultValue);
}
public LongToIntFunction getAsFunctionOrInt(String name, int defaultValue) {
return tmap.getAsFunctionOrInt(name, defaultValue);
}
/**
* Get a LongFunction that first creates a LongFunction of String as in

View File

@ -38,6 +38,7 @@ import org.apache.logging.log4j.Logger;
import java.util.*;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
/**
* A parsed map template, which allows construction of extracted or projected functions related
@ -618,6 +619,25 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
}
}
public LongToIntFunction getAsFunctionOrInt(String name, int defaultValue) {
if (isDynamic(name)) {
LongFunction<?> f = dynamics.get(name);
Object testValue = f.apply(0);
if (!testValue.getClass().isPrimitive()) {
throw new OpConfigError(STR."getAsFunctionOrInt returned non primitive type: \{testValue.getClass().getCanonicalName()}");
}
if (!testValue.getClass().equals(int.class)) {
throw new OpConfigError(STR."getAsFunctionOrInt returned non-int type: \{testValue.getClass().getCanonicalName()}");
}
return (long i) -> (int) f.apply(i);
} else if (isStatic(name) || isConfig(name)) {
int v = (int) getStaticValue(name);
return l -> v;
} else {
return l -> defaultValue;
}
}
/**
* Get a LongFunction that first creates a LongFunction of String as in {@link #getAsFunctionOr(String, Object)} )}, but then
* applies the result and cached it for subsequent access. This relies on {@link ObjectCache} internally.
@ -1142,4 +1162,5 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
prototype.putAll(getConfigPrototype());
return prototype;
}
}