|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: none |
| 3 | + * SPDX-License-Identifier: CC0-1.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package gov.nist.secauto.metaschema.core.metapath.function.library; |
| 7 | + |
| 8 | +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; |
| 9 | +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; |
| 10 | +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; |
| 11 | +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; |
| 12 | +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; |
| 13 | +import gov.nist.secauto.metaschema.core.metapath.item.IItem; |
| 14 | +import gov.nist.secauto.metaschema.core.metapath.item.ISequence; |
| 15 | +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; |
| 16 | +import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem; |
| 17 | +import gov.nist.secauto.metaschema.core.util.ObjectUtils; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.BiFunction; |
| 21 | + |
| 22 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Implements the XPath 3.1 <a href= |
| 26 | + * "https://www.w3.org/TR/xpath-functions-31/#func-map-contains">map:contains</a> |
| 27 | + * function. |
| 28 | + */ |
| 29 | +public final class MapForEach { |
| 30 | + private static final String NAME = "for-each"; |
| 31 | + @NonNull |
| 32 | + static final IFunction SIGNATURE = IFunction.builder() |
| 33 | + .name(NAME) |
| 34 | + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_MAP) |
| 35 | + .deterministic() |
| 36 | + .contextIndependent() |
| 37 | + .focusIndependent() |
| 38 | + .argument(IArgument.builder() |
| 39 | + .name("map") |
| 40 | + .type(IMapItem.type()) |
| 41 | + .one() |
| 42 | + .build()) |
| 43 | + .argument(IArgument.builder() |
| 44 | + .name("action") |
| 45 | + .type(IFunction.type()) |
| 46 | + .one() |
| 47 | + .build()) |
| 48 | + .returnType(IItem.type()) |
| 49 | + .returnOne() |
| 50 | + .functionHandler(MapForEach::execute) |
| 51 | + .build(); |
| 52 | + |
| 53 | + private MapForEach() { |
| 54 | + // disable construction |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("unused") |
| 58 | + @NonNull |
| 59 | + private static ISequence<?> execute(@NonNull IFunction function, |
| 60 | + @NonNull List<ISequence<?>> arguments, |
| 61 | + @NonNull DynamicContext dynamicContext, |
| 62 | + IItem focus) { |
| 63 | + IMapItem<?> map = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); |
| 64 | + IFunction action = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); |
| 65 | + |
| 66 | + BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> lambda = adaptFunction(action, dynamicContext); |
| 67 | + return forEach(map, lambda); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Adapts the provided function call to a lambda expression for use with |
| 72 | + * {@link #forEach(IMapItem, BiFunction)}. |
| 73 | + * |
| 74 | + * @param function |
| 75 | + * the function to adapt |
| 76 | + * @param dynamicContext |
| 77 | + * the dynamic context for use in evaluating the function |
| 78 | + * @return |
| 79 | + */ |
| 80 | + @NonNull |
| 81 | + private static BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> adaptFunction( |
| 82 | + @NonNull IFunction function, |
| 83 | + @NonNull DynamicContext dynamicContext) { |
| 84 | + return (key, value) -> { |
| 85 | + List<ISequence<?>> arguments = ObjectUtils.notNull(List.of( |
| 86 | + ISequence.of(key), |
| 87 | + value.toSequence())); |
| 88 | + |
| 89 | + return function.execute(arguments, dynamicContext, ISequence.empty()); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * An implementation of XPath 3.1 <a href= |
| 95 | + * "https://www.w3.org/TR/xpath-functions-31/#func-map-for-each">map:for-each</a>. |
| 96 | + * |
| 97 | + * @param map |
| 98 | + * the map of Metapath items that is the target of retrieval |
| 99 | + * @param action |
| 100 | + * the action to perform on each entry in the map |
| 101 | + * @return a stream resulting from concatenating the resulting streams provided |
| 102 | + * by the action result |
| 103 | + */ |
| 104 | + @NonNull |
| 105 | + public static ISequence<?> forEach( |
| 106 | + @NonNull IMapItem<?> map, |
| 107 | + @NonNull BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> action) { |
| 108 | + return ISequence.of(ObjectUtils.notNull(map.entrySet().stream() |
| 109 | + .flatMap(entry -> { |
| 110 | + IAnyAtomicItem key = entry.getKey().getKey(); |
| 111 | + ISequence<?> values = entry.getValue().contentsAsSequence(); |
| 112 | + return action.apply(key, values).stream(); |
| 113 | + }))); |
| 114 | + } |
| 115 | +} |
0 commit comments