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.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.LongFunction; import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
/** /**
* <H1>ParsedOp API</H1> * <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) { public <V> LongFunction<V> getAsFunctionOr(String name, V defaultValue) {
return tmap.getAsFunctionOr(name, 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 * 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.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.LongFunction; import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
/** /**
* A parsed map template, which allows construction of extracted or projected functions related * A parsed map template, which allows construction of extracted or projected functions related
@ -151,7 +152,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
} }
}); });
Map<String, Object> submap = (Map<String, Object>) v; Map<String, Object> submap = (Map<String, Object>) v;
ParsedTemplateMap subtpl = new ParsedTemplateMap(getName(),submap, bindings, cfgsources); ParsedTemplateMap subtpl = new ParsedTemplateMap(getName(), submap, bindings, cfgsources);
this.captures.addAll(subtpl.getCaptures()); this.captures.addAll(subtpl.getCaptures());
if (subtpl.isStatic()) { if (subtpl.isStatic()) {
statics.put(k, submap); statics.put(k, submap);
@ -168,8 +169,8 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
statics.put(k, sublist); statics.put(k, sublist);
protomap.put(k, sublist); protomap.put(k, sublist);
} else { } else {
dynamics.put(k,subtpl); dynamics.put(k, subtpl);
protomap.put(k,null); protomap.put(k, null);
} }
} else { } else {
// Eventually, nested and mixed static dynamic structure could be supported, but // Eventually, nested and mixed static dynamic structure could be supported, but
@ -215,11 +216,11 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
} }
public Map<String, Object> getConfigPrototype() { public Map<String, Object> getConfigPrototype() {
Map<String,Object> cfgs = new LinkedHashMap<>(); Map<String, Object> cfgs = new LinkedHashMap<>();
for (Map<String, Object> cfgsource : cfgsources) { for (Map<String, Object> cfgsource : cfgsources) {
for (String key : cfgsource.keySet()) { for (String key : cfgsource.keySet()) {
if (!cfgs.containsKey(key)) { if (!cfgs.containsKey(key)) {
cfgs.put(key,cfgsource.get(key)); cfgs.put(key, cfgsource.get(key));
} else { } else {
logger.warn("config sources contain overlapping keys for '" + key + "', precedence is undefined"); logger.warn("config sources contain overlapping keys for '" + key + "', precedence is undefined");
} }
@ -244,7 +245,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
public Map<String, Object> applyFull(long value) { public Map<String, Object> applyFull(long value) {
Map<String, Object> newmap = apply(value); Map<String, Object> newmap = apply(value);
for (int i = cfgsources.size()-1; i>0 ; i--) { for (int i = cfgsources.size() - 1; i > 0; i--) {
newmap.putAll(cfgsources.get(i)); newmap.putAll(cfgsources.get(i));
} }
return newmap; return newmap;
@ -279,7 +280,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
@Override @Override
public boolean isDefined(String... fields) { public boolean isDefined(String... fields) {
for (String field : fields) { for (String field : fields) {
if (!isStatic(field)&&!isDynamic(field)&&!isConfig(field)) { if (!isStatic(field) && !isDynamic(field) && !isConfig(field)) {
return false; return false;
} }
} }
@ -312,7 +313,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
return (T) cfgsource.get(field); return (T) cfgsource.get(field);
} }
} }
throw new OpConfigError("config value for '" +field +"' was not found in " + cfgsources); throw new OpConfigError("config value for '" + field + "' was not found in " + cfgsources);
} }
public <T> T takeStaticValue(String field, Class<T> classOfT) { public <T> T takeStaticValue(String field, Class<T> classOfT) {
@ -395,11 +396,11 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
public <T> T getStaticConfig(String name, Class<T> clazz) { public <T> T getStaticConfig(String name, Class<T> clazz) {
if (statics.containsKey(name)) { if (statics.containsKey(name)) {
return NBTypeConverter.convert(statics.get(name),clazz); return NBTypeConverter.convert(statics.get(name), clazz);
} }
for (Map<String, Object> cfgsource : cfgsources) { for (Map<String, Object> cfgsource : cfgsources) {
if (cfgsource.containsKey(name)) { if (cfgsource.containsKey(name)) {
return NBTypeConverter.convert(cfgsource.get(name),clazz); return NBTypeConverter.convert(cfgsource.get(name), clazz);
} }
} }
if (dynamics.containsKey(name)) { if (dynamics.containsKey(name)) {
@ -575,7 +576,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
Object cfgval = getConfig(name); Object cfgval = getConfig(name);
if (type.isAssignableFrom(cfgval.getClass())) { if (type.isAssignableFrom(cfgval.getClass())) {
return Optional.of(l -> type.cast(cfgval)); return Optional.of(l -> type.cast(cfgval));
} else if (NBTypeConverter.canConvert(cfgval,type)) { } else if (NBTypeConverter.canConvert(cfgval, type)) {
return Optional.of(l -> NBTypeConverter.convert(cfgval, type)); return Optional.of(l -> NBTypeConverter.convert(cfgval, type));
} else { } else {
throw new OpConfigError( throw new OpConfigError(
@ -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 * 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. * applies the result and cached it for subsequent access. This relies on {@link ObjectCache} internally.
@ -765,15 +785,15 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
* @param fieldname the field to take the templates from * @param fieldname the field to take the templates from
* @return A map of templates, or an empty map if the field is not defined or is empty. * @return A map of templates, or an empty map if the field is not defined or is empty.
*/ */
public Map<String,ParsedTemplateString> takeAsNamedTemplates(String fieldname) { public Map<String, ParsedTemplateString> takeAsNamedTemplates(String fieldname) {
Object entry = originalTemplateObject.get(fieldname); Object entry = originalTemplateObject.get(fieldname);
if (entry !=null) { if (entry != null) {
dynamics.remove(fieldname); dynamics.remove(fieldname);
statics.remove(fieldname); statics.remove(fieldname);
protomap.remove(fieldname); protomap.remove(fieldname);
} }
if (entry==null) { if (entry == null) {
for (Map<String, Object> cfgsource : cfgsources) { for (Map<String, Object> cfgsource : cfgsources) {
if (cfgsource.containsKey(fieldname)) { if (cfgsource.containsKey(fieldname)) {
entry = cfgsource.get(fieldname); entry = cfgsource.get(fieldname);
@ -782,27 +802,27 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
} }
} }
if (entry==null) { if (entry == null) {
return Map.of(); return Map.of();
} }
Map<String,Object> elements = new LinkedHashMap<>(); Map<String, Object> elements = new LinkedHashMap<>();
if (entry instanceof CharSequence chars) { if (entry instanceof CharSequence chars) {
elements.put(this.getName()+"-verifier-0",chars.toString()); elements.put(this.getName() + "-verifier-0", chars.toString());
} else if (entry instanceof List list) { } else if (entry instanceof List list) {
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
elements.put(this.getName()+"-verifier-"+i,list.get(0)); elements.put(this.getName() + "-verifier-" + i, list.get(0));
} }
} else if (entry instanceof Map map) { } else if (entry instanceof Map map) {
map.forEach((k,v) -> { map.forEach((k, v) -> {
elements.put(this.getName()+"-verifier-"+k,v); elements.put(this.getName() + "-verifier-" + k, v);
}); });
} }
Map<String,ParsedTemplateString> parsedStringTemplates Map<String, ParsedTemplateString> parsedStringTemplates
= new LinkedHashMap<>(); = new LinkedHashMap<>();
elements.forEach((k,v) -> { elements.forEach((k, v) -> {
if (v instanceof CharSequence chars) { if (v instanceof CharSequence chars) {
parsedStringTemplates.put(k,new ParsedTemplateString(chars.toString(), this.bindings)); parsedStringTemplates.put(k, new ParsedTemplateString(chars.toString(), this.bindings));
} }
}); });
return parsedStringTemplates; return parsedStringTemplates;
@ -903,7 +923,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
public Class<?> getValueType(String fieldname) { public Class<?> getValueType(String fieldname) {
if (isDynamic(fieldname)) { if (isDynamic(fieldname)) {
return get(fieldname,1).getClass(); return get(fieldname, 1).getClass();
} }
if (isStatic(fieldname)) { if (isStatic(fieldname)) {
return getStaticValue(fieldname).getClass(); return getStaticValue(fieldname).getClass();
@ -1095,12 +1115,12 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
public Map<String, Object> parseStaticCmdMap(String taskname, String mainField) { public Map<String, Object> parseStaticCmdMap(String taskname, String mainField) {
Object mapsrc = getStaticValue(taskname); Object mapsrc = getStaticValue(taskname);
return new LinkedHashMap<String,Object>(ParamsParser.parseToMap(mapsrc,mainField)); return new LinkedHashMap<String, Object>(ParamsParser.parseToMap(mapsrc, mainField));
} }
public List<Map<String, Object>> parseStaticCmdMaps(String key, String mainField) { public List<Map<String, Object>> parseStaticCmdMaps(String key, String mainField) {
Object mapsSrc = getStaticValue(key); Object mapsSrc = getStaticValue(key);
List<Map<String,Object>> maps = new ArrayList<>(); List<Map<String, Object>> maps = new ArrayList<>();
for (String spec : mapsSrc.toString().split("; +")) { for (String spec : mapsSrc.toString().split("; +")) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>(ParamsParser.parseToMap(spec, mainField)); LinkedHashMap<String, Object> map = new LinkedHashMap<>(ParamsParser.parseToMap(spec, mainField));
maps.add(map); maps.add(map);
@ -1118,7 +1138,7 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
.append(k) .append(k)
.append("->") .append("->")
.append( .append(
v ==null? originalTemplateObject.get(k) : v.toString() v == null ? originalTemplateObject.get(k) : v.toString()
).append("\n"); ).append("\n");
} }
@ -1135,11 +1155,12 @@ public class ParsedTemplateMap implements LongFunction<Map<String, ?>>, StaticFi
} }
public Map<String,Object> getCombinedPrototype() { public Map<String, Object> getCombinedPrototype() {
Map<String,Object> prototype = new LinkedHashMap<>(); Map<String, Object> prototype = new LinkedHashMap<>();
prototype.putAll(getDynamicPrototype()); prototype.putAll(getDynamicPrototype());
prototype.putAll(getStaticPrototype()); prototype.putAll(getStaticPrototype());
prototype.putAll(getConfigPrototype()); prototype.putAll(getConfigPrototype());
return prototype; return prototype;
} }
} }