1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.oscal.lib.model.control.profile;
7   
8   import java.util.Collection;
9   import java.util.LinkedList;
10  import java.util.List;
11  import java.util.regex.Pattern;
12  import java.util.stream.Collectors;
13  
14  import dev.metaschema.oscal.lib.model.ProfileMatching;
15  import dev.metaschema.oscal.lib.model.ProfileSelectControlById;
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  
18  public abstract class AbstractProfileSelectControlById implements IProfileSelectControlById {
19    // TODO: move implementation from profile resolver selection code here
20  
21    @NonNull
22    public static Builder builder() {
23      return new Builder();
24    }
25  
26    public static class Builder {
27      private boolean withChildControls; // false;
28      private final List<String> withIds = new LinkedList<>();
29      private final List<Pattern> matching = new LinkedList<>();
30  
31      @NonNull
32      public Builder withChildControls(boolean value) {
33        this.withChildControls = value;
34        return this;
35      }
36  
37      @NonNull
38      public Builder withId(@NonNull String id) {
39        withIds.add(id);
40        return this;
41      }
42  
43      @NonNull
44      public Builder withIds(@NonNull Collection<String> ids) {
45        withIds.addAll(ids);
46        return this;
47      }
48  
49      @NonNull
50      public Builder matching(@NonNull Pattern pattern) {
51        matching.add(pattern);
52        return this;
53      }
54  
55      @NonNull
56      public ProfileSelectControlById build() {
57        ProfileSelectControlById retval = new ProfileSelectControlById();
58        retval.setWithChildControls(withChildControls ? "yes" : "no");
59        retval.setWithIds(withIds);
60        retval.setMatching(matching.stream()
61            .map(pattern -> {
62              ProfileMatching matching = new ProfileMatching();
63              matching.setPattern(pattern.pattern());
64              return matching;
65            })
66            .collect(Collectors.toList()));
67        return retval;
68      }
69    }
70  }