1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.oscal.lib.profile.resolver.selection;
7   
8   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
9   import gov.nist.secauto.oscal.lib.model.control.catalog.IControl;
10  
11  import org.apache.commons.lang3.tuple.Pair;
12  
13  import java.util.Arrays;
14  import java.util.Set;
15  import java.util.function.Function;
16  import java.util.stream.Collectors;
17  
18  import edu.umd.cs.findbugs.annotations.NonNull;
19  
20  public interface IControlSelectionFilter extends Function<IControl, Pair<Boolean, Boolean>> {
21  
22    @NonNull
23    Pair<Boolean, Boolean> NON_MATCH = ObjectUtils.notNull(Pair.of(false, false));
24    @NonNull
25    Pair<Boolean, Boolean> MATCH = ObjectUtils.notNull(Pair.of(true, true));
26  
27    @NonNull
28    IControlSelectionFilter ALL_MATCH = control -> MATCH;
29  
30    @NonNull
31    IControlSelectionFilter NONE_MATCH = control -> NON_MATCH;
32  
33    @NonNull
34    static IControlSelectionFilter matchIds(@NonNull String... identifiers) {
35      return new IControlSelectionFilter() {
36        private final Set<String> keys = Arrays.stream(identifiers).collect(Collectors.toUnmodifiableSet());
37  
38        @Override
39        public @NonNull
40        Pair<Boolean, Boolean> apply(IControl control) {
41          return ObjectUtils.notNull(Pair.of(keys.contains(control.getId()), false));
42        }
43  
44      };
45    }
46  
47    /**
48     * Determines if the control is matched by this filter. This method returns a
49     * {@link Pair} where the first member of the pair indicates if the control
50     * matches, and the second indicates if the match applies to child controls as
51     * well.
52     *
53     * @param control
54     *          the control to check for a match
55     * @return a pair indicating the status of the match ({@code true} for a match
56     *         or {@code false} otherwise), and if a match applies to child controls
57     */
58    @NonNull
59    @Override
60    Pair<Boolean, Boolean> apply(IControl control);
61  }