Skip to content

Commit

Permalink
code cleanup and fixes to PMD warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jun 12, 2024
1 parent c1b9b09 commit 433908f
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ public static ISequence<?> executeTwoArg(
@NonNull
public static IBooleanItem hasNamespace(
@NonNull IAssemblyNodeItem propOrPart,
@NonNull ISequence<? extends IStringItem> namespaces)
throws MetapathException {
@NonNull ISequence<? extends IStringItem> namespaces) {
Object propOrPartObject = propOrPart.getValue();
if (propOrPartObject == null) {
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, propOrPart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Stream<String> getParameterReferences() {
Stream<String> aggregatesIds = CollectionUtil.listOrEmpty(getProps()).stream()
.filter(Objects::nonNull)
.filter(prop -> prop.isNamespaceEqual(IProperty.OSCAL_NAMESPACE) && "aggregates".equals(prop.getName()))
.map(prop -> prop.getValue());
.map(Property::getValue);

// handle select/choice/insert
ParameterSelection selection = getSelect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Stream<IPart> getPartsRecursively() {
return Stream.concat(
Stream.of(this),
CollectionUtil.listOrEmpty(getParts()).stream()
.flatMap(part -> part.getPartsRecursively()));
.flatMap(AbstractPart::getPartsRecursively));
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.AbstractOscalInstance;
import gov.nist.secauto.oscal.lib.model.control.AbstractParameter;

import java.util.stream.Stream;

Expand All @@ -45,7 +46,7 @@ public Stream<String> getReferencedParameterIds() {
return ObjectUtils.notNull(
CollectionUtil.listOrEmpty(getParams()).stream()
.flatMap(ObjectUtils::filterNull)
.flatMap(param -> param.getParameterReferences())
.flatMap(AbstractParameter::getParameterReferences)
.distinct());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.oscal.lib.model.Link;
import gov.nist.secauto.oscal.lib.model.Parameter;
import gov.nist.secauto.oscal.lib.model.Property;
import gov.nist.secauto.oscal.lib.model.control.AbstractParameter;

import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -63,7 +64,7 @@ public Stream<String> getReferencedParameterIds() {
// get parameters referenced by the control's parameters
Stream<String> parameterIds = CollectionUtil.listOrEmpty(getParams()).stream()
.flatMap(ObjectUtils::filterNull)
.flatMap(param -> param.getParameterReferences());
.flatMap(AbstractParameter::getParameterReferences);

return ObjectUtils.notNull(
Stream.concat(insertIds, parameterIds).distinct());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,43 @@ public RESULT visitCatalog(Catalog catalog, CONTEXT context) {
RESULT result = CollectionUtil.listOrEmpty(catalog.getGroups()).stream()
.filter(Objects::nonNull)
.map(childGroup -> visitGroup(ObjectUtils.notNull(childGroup), context))
.reduce(defaultResult(), (previous, current) -> aggregateResult(previous, current));
.reduce(defaultResult(), this::aggregateResult);
result = CollectionUtil.listOrEmpty(catalog.getControls()).stream()
.filter(Objects::nonNull)
.map(childControl -> visitControl(ObjectUtils.notNull(childControl), context))
.reduce(result, (previous, current) -> aggregateResult(previous, current));
.reduce(result, this::aggregateResult);
return CollectionUtil.listOrEmpty(catalog.getParams()).stream()
.filter(Objects::nonNull)
.map(childParameter -> visitParameter(ObjectUtils.notNull(childParameter), context))
.reduce(result, (previous, current) -> aggregateResult(previous, current));
.reduce(result, this::aggregateResult);
}

@Override
public RESULT visitGroup(@NonNull CatalogGroup group, CONTEXT context) {
RESULT result = CollectionUtil.listOrEmpty(group.getGroups()).stream()
.filter(Objects::nonNull)
.map(childGroup -> visitGroup(ObjectUtils.notNull(childGroup), context))
.reduce(defaultResult(), (previous, current) -> aggregateResult(previous, current));
.reduce(defaultResult(), this::aggregateResult);
result = CollectionUtil.listOrEmpty(group.getControls()).stream()
.filter(Objects::nonNull)
.map(childControl -> visitControl(ObjectUtils.notNull(childControl), context))
.reduce(result, (previous, current) -> aggregateResult(previous, current));
.reduce(result, this::aggregateResult);
return CollectionUtil.listOrEmpty(group.getParams()).stream()
.filter(Objects::nonNull)
.map(childParameter -> visitParameter(ObjectUtils.notNull(childParameter), context))
.reduce(result, (previous, current) -> aggregateResult(previous, current));
.reduce(result, this::aggregateResult);
}

@Override
public RESULT visitControl(Control control, CONTEXT context) {
RESULT result = CollectionUtil.listOrEmpty(control.getControls()).stream()
.filter(Objects::nonNull)
.map(childControl -> visitControl(ObjectUtils.notNull(childControl), context))
.reduce(defaultResult(), (previous, current) -> aggregateResult(previous, current));
.reduce(defaultResult(), this::aggregateResult);
return CollectionUtil.listOrEmpty(control.getParams()).stream()
.filter(Objects::nonNull)
.map(childParameter -> visitParameter(ObjectUtils.notNull(childParameter), context))
.reduce(result, (previous, current) -> aggregateResult(previous, current));
.reduce(result, this::aggregateResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.oscal.lib.model.Link;
import gov.nist.secauto.oscal.lib.model.Parameter;
import gov.nist.secauto.oscal.lib.model.Property;
import gov.nist.secauto.oscal.lib.model.control.AbstractParameter;

import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -86,7 +87,7 @@ public Stream<String> getReferencedParameterIds() {
// get parameters referenced by the control's parameters
Stream<String> parameterIds = CollectionUtil.listOrEmpty(getParams()).stream()
.flatMap(ObjectUtils::filterNull)
.flatMap(param -> param.getParameterReferences());
.flatMap(AbstractParameter::getParameterReferences);

return ObjectUtils.notNull(
Stream.concat(insertIds, parameterIds).distinct());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
import gov.nist.secauto.metaschema.databind.model.IBoundObject;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.OscalModelConstants;
import gov.nist.secauto.oscal.lib.OscalUtils;
Expand All @@ -56,6 +57,9 @@
import gov.nist.secauto.oscal.lib.model.Control;
import gov.nist.secauto.oscal.lib.model.Merge;
import gov.nist.secauto.oscal.lib.model.Metadata;
import gov.nist.secauto.oscal.lib.model.Metadata.Location;
import gov.nist.secauto.oscal.lib.model.Metadata.Party;
import gov.nist.secauto.oscal.lib.model.Metadata.Role;
import gov.nist.secauto.oscal.lib.model.Modify;
import gov.nist.secauto.oscal.lib.model.Modify.ProfileSetParameter;
import gov.nist.secauto.oscal.lib.model.Parameter;
Expand Down Expand Up @@ -395,9 +399,8 @@ protected IIndexer resolveImport(
// changes to the data.
try {
IRootAssemblyNodeItem importedCatalogRoot = ObjectUtils.requireNonNull(getRoot(importedCatalog, CATALOG));
Catalog catalogCopy
= (Catalog) OscalBindingContext.instance().deepCopy(
ObjectUtils.requireNonNull(importedCatalogRoot.getValue()), null);
Catalog catalogCopy = (Catalog) OscalBindingContext.instance().deepCopy(
(IBoundObject) ObjectUtils.requireNonNull(importedCatalogRoot).getValue(), null);

importedCatalog = INodeItemFactory.instance().newDocumentNodeItem(
importedCatalogRoot.getDefinition(),
Expand Down Expand Up @@ -637,6 +640,7 @@ protected void handleSetParameter(IAssemblyNodeItem item, IIndexer indexer) {
param.setSelect(setParameter.getSelect());
}

@SuppressWarnings("PMD.ExceptionAsFlowControl")
protected void handleAlter(IAssemblyNodeItem item, IIndexer indexer) {
Modify.Alter alter = ObjectUtils.requireNonNull((Modify.Alter) item.getValue());
String controlId = ObjectUtils.requireNonNull(alter.getControlId());
Expand Down Expand Up @@ -721,19 +725,19 @@ private static void handleReferences(@NonNull Catalog resolvedCatalog, @NonNull
IIndexer.filterDistinct(
ObjectUtils.notNull(CollectionUtil.listOrEmpty(resolvedMetadata.getRoles()).stream()),
profileIndex.getEntitiesByItemType(IEntityItem.ItemType.ROLE),
item -> item.getId())
Role::getId)
.collect(Collectors.toCollection(LinkedList::new)));
resolvedMetadata.setParties(
IIndexer.filterDistinct(
ObjectUtils.notNull(CollectionUtil.listOrEmpty(resolvedMetadata.getParties()).stream()),
profileIndex.getEntitiesByItemType(IEntityItem.ItemType.PARTY),
item -> item.getUuid())
Party::getUuid)
.collect(Collectors.toCollection(LinkedList::new)));
resolvedMetadata.setLocations(
IIndexer.filterDistinct(
ObjectUtils.notNull(CollectionUtil.listOrEmpty(resolvedMetadata.getLocations()).stream()),
profileIndex.getEntitiesByItemType(IEntityItem.ItemType.LOCATION),
item -> item.getUuid())
Location::getUuid)
.collect(Collectors.toCollection(LinkedList::new)));

// copy resources
Expand All @@ -744,7 +748,7 @@ private static void handleReferences(@NonNull Catalog resolvedCatalog, @NonNull
List<Resource> resources = IIndexer.filterDistinct(
ObjectUtils.notNull(resolvedResources.stream()),
profileIndex.getEntitiesByItemType(IEntityItem.ItemType.RESOURCE),
item -> item.getUuid())
Resource::getUuid)
.collect(Collectors.toCollection(LinkedList::new));

if (!resources.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ private static <T> boolean handleChild(
// if id match, inject the new items into the collection
switch (context.getPosition()) {
case AFTER: {
newItemsSupplier.get().forEach(add -> iter.add(add));
newItemsSupplier.get().forEach(iter::add);
retval = true;
break;
}
case BEFORE: {
iter.previous();
List<T> adds = newItemsSupplier.get();
adds.forEach(add -> iter.add(add));
adds.forEach(iter::add);
item = iter.next();
retval = true;
break;
Expand Down Expand Up @@ -437,26 +437,26 @@ public Boolean visitControl(Control control, Context context) {

boolean retval = handleCurrent(
control,
title -> control.setTitle(title),
() -> control.getParams(),
() -> control.getProps(),
() -> control.getLinks(),
() -> control.getParts(),
control::setTitle,
control::getParams,
control::getProps,
control::getLinks,
control::getParts,
context);

// visit params
retval = retval || handleChild(
TargetType.PARAM,
() -> control.getParams(),
() -> context.getParams(),
control::getParams,
context::getParams,
child -> visitParameter(ObjectUtils.notNull(child), context),
context);

// visit parts
retval = retval || handleChild(
TargetType.PART,
() -> control.getParts(),
() -> context.getParts(),
control::getParts,
context::getParts,
child -> visitPart(child, context),
context);

Expand Down Expand Up @@ -485,8 +485,8 @@ public Boolean visitParameter(Parameter parameter, Context context) {
parameter,
null,
null,
() -> parameter.getProps(),
() -> parameter.getLinks(),
parameter::getProps,
parameter::getLinks,
null,
context);
}
Expand Down Expand Up @@ -518,19 +518,17 @@ public boolean visitPart(ControlPart part, Context context) {
part,
null,
null,
() -> part.getProps(),
() -> part.getLinks(),
() -> part.getParts(),
part::getProps,
part::getLinks,
part::getParts,
context);

// visit parts
retval = retval || handleChild(
return retval || handleChild(
TargetType.PART,
() -> part.getParts(),
() -> context.getParts(),
part::getParts,
context::getParts,
child -> visitPart(child, context),
context);
return retval;
}

static class Context {
Expand Down Expand Up @@ -613,20 +611,19 @@ public Context(
}

if (Position.BEFORE.equals(position) || Position.AFTER.equals(position)) {
if (sequenceTarget) {
if (sequenceTarget && !params.isEmpty() && parts.isEmpty()) {
targetItemTypes.retainAll(Set.of(TargetType.PARAM));
} else if (sequenceTarget && !parts.isEmpty() && params.isEmpty()) {
targetItemTypes.retainAll(Set.of(TargetType.PART));
} else {
throw new ProfileResolutionEvaluationException(
"When using position before or after, only one collection of parameters or parts can be specified.");
}
} else {
if (!sequenceTarget) {
throw new ProfileResolutionEvaluationException(
"When using position before or after, one collection of parameters or parts can be specified."
+ " Other additions must not be used.");
}
if (sequenceTarget && !params.isEmpty() && parts.isEmpty()) {
targetItemTypes.retainAll(Set.of(TargetType.PARAM));
} else if (sequenceTarget && !parts.isEmpty() && params.isEmpty()) {
targetItemTypes.retainAll(Set.of(TargetType.PART));
} else {
throw new ProfileResolutionEvaluationException(
"When using position before or after, only one collection of parameters or parts can be specified.");
}
}

if (targetItemTypes.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public DefaultControlSelectionFilter(@NonNull List<? extends IProfileSelectContr
// ignore null entries
.filter(Objects::nonNull)
// create a selection object for the selection
.map(selection -> new Selection(selection))
.map(Selection::new)
.collect(Collectors.toUnmodifiableList());
}

Expand Down Expand Up @@ -94,7 +94,7 @@ protected Pair<Boolean, Boolean> match(String id) {
return selections.parallelStream()
.map(selection -> selection.match(id))
// filter out non-matches
.filter(pair -> pair.getLeft())
.filter(Pair::getLeft)
// aggregate matches
.reduce((first, second) -> {
Pair<Boolean, Boolean> result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,15 @@ public interface IControlSelectionFilter extends Function<IControl, Pair<Boolean
Pair<Boolean, Boolean> MATCH = ObjectUtils.notNull(Pair.of(true, true));

@NonNull
IControlSelectionFilter ALL_MATCH = new IControlSelectionFilter() {
@Override
public Pair<Boolean, Boolean> apply(IControl control) {
return MATCH;
}
};
IControlSelectionFilter ALL_MATCH = control -> IControlSelectionFilter.MATCH;

@NonNull
IControlSelectionFilter NONE_MATCH = new IControlSelectionFilter() {
@Override
public Pair<Boolean, Boolean> apply(IControl control) {
return NON_MATCH;
}
};
IControlSelectionFilter NONE_MATCH = control -> IControlSelectionFilter.NON_MATCH;

@NonNull
static IControlSelectionFilter matchIds(@NonNull String... identifiers) {
return new IControlSelectionFilter() {
private Set<String> keys = Arrays.stream(identifiers).collect(Collectors.toUnmodifiableSet());
private final Set<String> keys = Arrays.stream(identifiers).collect(Collectors.toUnmodifiableSet());

@Override
public @NonNull Pair<Boolean, Boolean> apply(IControl control) {
Expand Down
Loading

0 comments on commit 433908f

Please sign in to comment.