001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.oscal.lib.model.control.profile;
007
008import gov.nist.secauto.oscal.lib.model.Matching;
009import gov.nist.secauto.oscal.lib.model.ProfileSelectControlById;
010
011import java.util.Collection;
012import java.util.LinkedList;
013import java.util.List;
014import java.util.regex.Pattern;
015import java.util.stream.Collectors;
016
017import edu.umd.cs.findbugs.annotations.NonNull;
018
019public abstract class AbstractProfileSelectControlById implements IProfileSelectControlById {
020  // TODO: move implementation from profile resolver selection code here
021
022  @NonNull
023  public static Builder builder() {
024    return new Builder();
025  }
026
027  public static class Builder {
028    private boolean withChildControls; // false;
029    private final List<String> withIds = new LinkedList<>();
030    private final List<Pattern> matching = new LinkedList<>();
031
032    @NonNull
033    public Builder withChildControls(boolean value) {
034      this.withChildControls = value;
035      return this;
036    }
037
038    @NonNull
039    public Builder withId(@NonNull String id) {
040      withIds.add(id);
041      return this;
042    }
043
044    @NonNull
045    public Builder withIds(@NonNull Collection<String> ids) {
046      withIds.addAll(ids);
047      return this;
048    }
049
050    @NonNull
051    public Builder matching(@NonNull Pattern pattern) {
052      matching.add(pattern);
053      return this;
054    }
055
056    @NonNull
057    public ProfileSelectControlById build() {
058      ProfileSelectControlById retval = new ProfileSelectControlById();
059      retval.setWithChildControls(withChildControls ? "yes" : "no");
060      retval.setWithIds(withIds);
061      retval.setMatching(matching.stream()
062          .map(pattern -> {
063            Matching matching = new Matching();
064            matching.setPattern(pattern.pattern());
065            return matching;
066          })
067          .collect(Collectors.toList()));
068      return retval;
069    }
070  }
071}