Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/main/java/org/spdx/library/model/RelatedElementCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public class RelatedElementCollection implements Collection<SpdxElement> {

ModelCollection<Relationship> relationshipCollection;
private RelationshipType relationshipTypeFilter;
private String relatedElementTypeFilter;

private SpdxElement owningElement;

/**
* @param owningElement
* @param relationshipTypeFilter relationship type to filter the results
Expand All @@ -60,12 +61,26 @@ public class RelatedElementCollection implements Collection<SpdxElement> {
*/
public RelatedElementCollection(SpdxElement owningElement,
@Nullable RelationshipType relationshipTypeFilter) throws InvalidSPDXAnalysisException {
this(owningElement, relationshipTypeFilter, null);
}

/**
* @param owningElement
* @param relationshipTypeFilter relationship type to filter the results
* collection on - if null, do not filter
* @param relatedElementTypeFilter filter for only related element types - if null, do not filter
* @throws InvalidSPDXAnalysisException
*/
public RelatedElementCollection(SpdxElement owningElement,
@Nullable RelationshipType relationshipTypeFilter,
@Nullable String relatedElementTypeFilter) throws InvalidSPDXAnalysisException {
Objects.requireNonNull(owningElement, "Owning element can not be null");
this.owningElement = owningElement;
this.relationshipCollection = new ModelCollection<Relationship>(owningElement.getModelStore(),
owningElement.getDocumentUri(), owningElement.getId(), SpdxConstants.PROP_RELATIONSHIP,
owningElement.getCopyManager(), Relationship.class);
this.relationshipTypeFilter = relationshipTypeFilter;
this.relatedElementTypeFilter = relatedElementTypeFilter;
}

public List<SpdxElement> toImmutableList() {
Expand All @@ -79,7 +94,10 @@ public List<SpdxElement> toImmutableList() {
(this.relationshipTypeFilter.equals(relationshipType))) {
Optional<SpdxElement> relatedElement = relationship.getRelatedSpdxElement();
if (relatedElement.isPresent()) {
retval.add(relatedElement.get());
if (Objects.isNull(this.relatedElementTypeFilter) ||
this.relatedElementTypeFilter.equals(relatedElement.get().getType())) {
retval.add(relatedElement.get());
}
}
}
} catch (InvalidSPDXAnalysisException e) {
Expand Down Expand Up @@ -152,7 +170,7 @@ public boolean add(SpdxElement e) {
return false;
}
try {
Relationship relationship = owningElement.createRelationship(e, relationshipTypeFilter, "");
Relationship relationship = owningElement.createRelationship(e, relationshipTypeFilter, null);
return owningElement.addRelationship(relationship);
} catch (InvalidSPDXAnalysisException e1) {
logger.error("Error adding relationship",e1);
Expand Down Expand Up @@ -250,7 +268,7 @@ public boolean retainAll(Collection<?> c) {
*/
@Override
public void clear() {
if (Objects.isNull(relationshipTypeFilter)) {
if (Objects.isNull(relationshipTypeFilter) && Objects.isNull(relatedElementTypeFilter)) {
relationshipCollection.clear();
} else {
List<SpdxElement> existingElements = toImmutableList();
Expand All @@ -270,19 +288,23 @@ public boolean equals(Object o) {
}
RelatedElementCollection compare = (RelatedElementCollection)o;
return Objects.equals(this.owningElement, compare.getOwningElement()) &&
Objects.equals(relationshipTypeFilter, compare.getRelationshipTypeFilter());
Objects.equals(relationshipTypeFilter, compare.getRelationshipTypeFilter()) &&
Objects.equals(relatedElementTypeFilter, compare.getRelatedElementTypeFilter());
}

/* (non-Javadoc)
* @see java.util.Collection#hashCode()
*/
@Override
public int hashCode() {
if (Objects.isNull(relationshipTypeFilter)) {
return 33 ^ this.owningElement.hashCode();
} else {
return 33 ^ this.owningElement.hashCode() ^ this.relationshipTypeFilter.hashCode();
int retval = 33 ^ this.owningElement.hashCode();
if (Objects.nonNull(relationshipTypeFilter)) {
retval = retval ^ this.relationshipTypeFilter.hashCode();
}
if (Objects.nonNull(relatedElementTypeFilter)) {
retval = retval ^ this.relatedElementTypeFilter.hashCode();
}
return retval;
}

/**
Expand All @@ -299,6 +321,13 @@ public RelationshipType getRelationshipTypeFilter() {
return relationshipTypeFilter;
}

/**
* @return the relatedElementTypeFilter
*/
public String getRelatedElementTypeFilter() {
return relatedElementTypeFilter;
}

/**
* @return the owningElement
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/library/model/SpdxDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class SpdxDocument extends SpdxElement {
@SuppressWarnings("unchecked")
public SpdxDocument(IModelStore modelStore, String documentUri, ModelCopyManager copyManager, boolean create) throws InvalidSPDXAnalysisException {
super(modelStore, documentUri, SpdxConstants.SPDX_DOCUMENT_ID, copyManager, create);
documentDescribes = new RelatedElementCollection(this, RelationshipType.DESCRIBES);
documentDescribes = new RelatedElementCollection(this, RelationshipType.DESCRIBES, null);
externalDocumentRefs = (Collection<ExternalDocumentRef>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_SPDX_EXTERNAL_DOC_REF, ExternalDocumentRef.class);
extractedLicenseInfos = (Collection<ExtractedLicenseInfo>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_SPDX_EXTRACTED_LICENSES, ExtractedLicenseInfo.class);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/spdx/library/model/SpdxPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.spdx.library.Version;
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
import org.spdx.library.model.enumerations.Purpose;
import org.spdx.library.model.enumerations.RelationshipType;
import org.spdx.library.model.license.AnyLicenseInfo;
import org.spdx.library.model.license.OrLaterOperator;
import org.spdx.library.model.license.SimpleLicensingInfo;
Expand All @@ -48,12 +49,14 @@
*
*/
public class SpdxPackage extends SpdxItem implements Comparable<SpdxPackage> {
Collection<SpdxElement> files;

/**
* @throws InvalidSPDXAnalysisException
*/
public SpdxPackage() throws InvalidSPDXAnalysisException {
super();
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
this.setFilesAnalyzed(true); // Set default value
}
Expand All @@ -71,6 +74,7 @@ public SpdxPackage(IModelStore modelStore, String documentUri, String id,
@Nullable ModelCopyManager copyManager, boolean create)
throws InvalidSPDXAnalysisException {
super(modelStore, documentUri, id, copyManager, create);
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
this.setFilesAnalyzed(true); // Set default value
}
Expand All @@ -82,6 +86,7 @@ public SpdxPackage(IModelStore modelStore, String documentUri, String id,
*/
public SpdxPackage(String id) throws InvalidSPDXAnalysisException {
super(id);
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
this.setFilesAnalyzed(true); // Set default value
}
Expand Down Expand Up @@ -490,7 +495,7 @@ public SpdxPackage addExternalRef(ExternalRef externalRef) throws InvalidSPDXAna
*/
@SuppressWarnings("unchecked")
public Collection<SpdxFile> getFiles() throws InvalidSPDXAnalysisException {
return (Collection<SpdxFile>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_PACKAGE_FILE, SpdxFile.class);
return (Collection<SpdxFile>)(Collection<?>)files;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/spdx/library/model/SpdxPackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,6 @@ public void testAddRelationship() throws InvalidSPDXAnalysisException {
List<Relationship> relationships1 = Arrays.asList(new Relationship[] {RELATIONSHIP1});
List<Relationship> relationships2 = Arrays.asList(new Relationship[] {RELATIONSHIP1, RELATIONSHIP2});
List<Checksum> checksums = Arrays.asList(new Checksum[] {CHECKSUM2, CHECKSUM3, CHECKSUM1});
List<SpdxFile> files = Arrays.asList(new SpdxFile[] {FILE1, FILE2});
List<AnyLicenseInfo> licenseFromFiles = Arrays.asList(new AnyLicenseInfo[] {LICENSE2});
String id = gmo.getModelStore().getNextId(IdType.SpdxId, gmo.getDocumentUri());
List<ExternalRef> externalRefs = Arrays.asList(new ExternalRef[] {EXTERNAL_REF1});
Expand All @@ -1314,7 +1313,6 @@ public void testAddRelationship() throws InvalidSPDXAnalysisException {
.setLicenseComments(LICENSE_COMMENT1)
.setDescription(DESCRIPTION1)
.setDownloadLocation(DOWNLOAD_LOCATION1)
.setFiles(files)
.setHomepage(HOMEPAGE1)
.setOriginator(ORIGINATOR1)
.setPackageFileName(PACKAGEFILENAME1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,7 @@ public void testIsPackageFilesEquals() throws SpdxCompareException, InvalidSPDXA
assertTrue(pc.isPackageSuppliersEquals());
assertTrue(pc.isPackageVerificationCodesEquals());
assertTrue(pc.isPackageVersionsEquals());
assertTrue(pc.isRelationshipsEquals());
assertTrue(pc.isSeenLicenseEquals());
assertTrue(pc.isSeenLicenseEquals());
assertEquals(0, pc.getUniqueChecksums(DOCA, DOCB).size());
assertEquals(0, pc.getUniqueChecksums(DOCB, DOCA).size());
assertEquals(1, pc.getUniqueFiles(DOCA, DOCB).size());
Expand Down