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