Skip to content

Commit b685579

Browse files
authored
Merge pull request #70 from spdx/issue69
Change Package hasFiles from a list property to a relationship collection
2 parents 01a1179 + 1734702 commit b685579

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

src/main/java/org/spdx/library/model/RelatedElementCollection.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public class RelatedElementCollection implements Collection<SpdxElement> {
4949

5050
ModelCollection<Relationship> relationshipCollection;
5151
private RelationshipType relationshipTypeFilter;
52+
private String relatedElementTypeFilter;
5253

5354
private SpdxElement owningElement;
54-
55+
5556
/**
5657
* @param owningElement
5758
* @param relationshipTypeFilter relationship type to filter the results
@@ -60,12 +61,26 @@ public class RelatedElementCollection implements Collection<SpdxElement> {
6061
*/
6162
public RelatedElementCollection(SpdxElement owningElement,
6263
@Nullable RelationshipType relationshipTypeFilter) throws InvalidSPDXAnalysisException {
64+
this(owningElement, relationshipTypeFilter, null);
65+
}
66+
67+
/**
68+
* @param owningElement
69+
* @param relationshipTypeFilter relationship type to filter the results
70+
* collection on - if null, do not filter
71+
* @param relatedElementTypeFilter filter for only related element types - if null, do not filter
72+
* @throws InvalidSPDXAnalysisException
73+
*/
74+
public RelatedElementCollection(SpdxElement owningElement,
75+
@Nullable RelationshipType relationshipTypeFilter,
76+
@Nullable String relatedElementTypeFilter) throws InvalidSPDXAnalysisException {
6377
Objects.requireNonNull(owningElement, "Owning element can not be null");
6478
this.owningElement = owningElement;
6579
this.relationshipCollection = new ModelCollection<Relationship>(owningElement.getModelStore(),
6680
owningElement.getDocumentUri(), owningElement.getId(), SpdxConstants.PROP_RELATIONSHIP,
6781
owningElement.getCopyManager(), Relationship.class);
6882
this.relationshipTypeFilter = relationshipTypeFilter;
83+
this.relatedElementTypeFilter = relatedElementTypeFilter;
6984
}
7085

7186
public List<SpdxElement> toImmutableList() {
@@ -79,7 +94,10 @@ public List<SpdxElement> toImmutableList() {
7994
(this.relationshipTypeFilter.equals(relationshipType))) {
8095
Optional<SpdxElement> relatedElement = relationship.getRelatedSpdxElement();
8196
if (relatedElement.isPresent()) {
82-
retval.add(relatedElement.get());
97+
if (Objects.isNull(this.relatedElementTypeFilter) ||
98+
this.relatedElementTypeFilter.equals(relatedElement.get().getType())) {
99+
retval.add(relatedElement.get());
100+
}
83101
}
84102
}
85103
} catch (InvalidSPDXAnalysisException e) {
@@ -152,7 +170,7 @@ public boolean add(SpdxElement e) {
152170
return false;
153171
}
154172
try {
155-
Relationship relationship = owningElement.createRelationship(e, relationshipTypeFilter, "");
173+
Relationship relationship = owningElement.createRelationship(e, relationshipTypeFilter, null);
156174
return owningElement.addRelationship(relationship);
157175
} catch (InvalidSPDXAnalysisException e1) {
158176
logger.error("Error adding relationship",e1);
@@ -250,7 +268,7 @@ public boolean retainAll(Collection<?> c) {
250268
*/
251269
@Override
252270
public void clear() {
253-
if (Objects.isNull(relationshipTypeFilter)) {
271+
if (Objects.isNull(relationshipTypeFilter) && Objects.isNull(relatedElementTypeFilter)) {
254272
relationshipCollection.clear();
255273
} else {
256274
List<SpdxElement> existingElements = toImmutableList();
@@ -270,19 +288,23 @@ public boolean equals(Object o) {
270288
}
271289
RelatedElementCollection compare = (RelatedElementCollection)o;
272290
return Objects.equals(this.owningElement, compare.getOwningElement()) &&
273-
Objects.equals(relationshipTypeFilter, compare.getRelationshipTypeFilter());
291+
Objects.equals(relationshipTypeFilter, compare.getRelationshipTypeFilter()) &&
292+
Objects.equals(relatedElementTypeFilter, compare.getRelatedElementTypeFilter());
274293
}
275294

276295
/* (non-Javadoc)
277296
* @see java.util.Collection#hashCode()
278297
*/
279298
@Override
280299
public int hashCode() {
281-
if (Objects.isNull(relationshipTypeFilter)) {
282-
return 33 ^ this.owningElement.hashCode();
283-
} else {
284-
return 33 ^ this.owningElement.hashCode() ^ this.relationshipTypeFilter.hashCode();
300+
int retval = 33 ^ this.owningElement.hashCode();
301+
if (Objects.nonNull(relationshipTypeFilter)) {
302+
retval = retval ^ this.relationshipTypeFilter.hashCode();
303+
}
304+
if (Objects.nonNull(relatedElementTypeFilter)) {
305+
retval = retval ^ this.relatedElementTypeFilter.hashCode();
285306
}
307+
return retval;
286308
}
287309

288310
/**
@@ -299,6 +321,13 @@ public RelationshipType getRelationshipTypeFilter() {
299321
return relationshipTypeFilter;
300322
}
301323

324+
/**
325+
* @return the relatedElementTypeFilter
326+
*/
327+
public String getRelatedElementTypeFilter() {
328+
return relatedElementTypeFilter;
329+
}
330+
302331
/**
303332
* @return the owningElement
304333
*/

src/main/java/org/spdx/library/model/SpdxDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SpdxDocument extends SpdxElement {
6060
@SuppressWarnings("unchecked")
6161
public SpdxDocument(IModelStore modelStore, String documentUri, ModelCopyManager copyManager, boolean create) throws InvalidSPDXAnalysisException {
6262
super(modelStore, documentUri, SpdxConstants.SPDX_DOCUMENT_ID, copyManager, create);
63-
documentDescribes = new RelatedElementCollection(this, RelationshipType.DESCRIBES);
63+
documentDescribes = new RelatedElementCollection(this, RelationshipType.DESCRIBES, null);
6464
externalDocumentRefs = (Collection<ExternalDocumentRef>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_SPDX_EXTERNAL_DOC_REF, ExternalDocumentRef.class);
6565
extractedLicenseInfos = (Collection<ExtractedLicenseInfo>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_SPDX_EXTRACTED_LICENSES, ExtractedLicenseInfo.class);
6666
}

src/main/java/org/spdx/library/model/SpdxPackage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.spdx.library.Version;
3333
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
3434
import org.spdx.library.model.enumerations.Purpose;
35+
import org.spdx.library.model.enumerations.RelationshipType;
3536
import org.spdx.library.model.license.AnyLicenseInfo;
3637
import org.spdx.library.model.license.OrLaterOperator;
3738
import org.spdx.library.model.license.SimpleLicensingInfo;
@@ -48,12 +49,14 @@
4849
*
4950
*/
5051
public class SpdxPackage extends SpdxItem implements Comparable<SpdxPackage> {
52+
Collection<SpdxElement> files;
5153

5254
/**
5355
* @throws InvalidSPDXAnalysisException
5456
*/
5557
public SpdxPackage() throws InvalidSPDXAnalysisException {
5658
super();
59+
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
5760
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
5861
this.setFilesAnalyzed(true); // Set default value
5962
}
@@ -71,6 +74,7 @@ public SpdxPackage(IModelStore modelStore, String documentUri, String id,
7174
@Nullable ModelCopyManager copyManager, boolean create)
7275
throws InvalidSPDXAnalysisException {
7376
super(modelStore, documentUri, id, copyManager, create);
77+
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
7478
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
7579
this.setFilesAnalyzed(true); // Set default value
7680
}
@@ -82,6 +86,7 @@ public SpdxPackage(IModelStore modelStore, String documentUri, String id,
8286
*/
8387
public SpdxPackage(String id) throws InvalidSPDXAnalysisException {
8488
super(id);
89+
files = new RelatedElementCollection(this, RelationshipType.CONTAINS, SpdxConstants.CLASS_SPDX_FILE);
8590
if (!getBooleanPropertyValue(SpdxConstants.PROP_PACKAGE_FILES_ANALYZED).isPresent()) {
8691
this.setFilesAnalyzed(true); // Set default value
8792
}
@@ -490,7 +495,7 @@ public SpdxPackage addExternalRef(ExternalRef externalRef) throws InvalidSPDXAna
490495
*/
491496
@SuppressWarnings("unchecked")
492497
public Collection<SpdxFile> getFiles() throws InvalidSPDXAnalysisException {
493-
return (Collection<SpdxFile>)(Collection<?>)this.getObjectPropertyValueSet(SpdxConstants.PROP_PACKAGE_FILE, SpdxFile.class);
498+
return (Collection<SpdxFile>)(Collection<?>)files;
494499
}
495500

496501
/**

src/test/java/org/spdx/library/model/SpdxPackageTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,6 @@ public void testAddRelationship() throws InvalidSPDXAnalysisException {
13001300
List<Relationship> relationships1 = Arrays.asList(new Relationship[] {RELATIONSHIP1});
13011301
List<Relationship> relationships2 = Arrays.asList(new Relationship[] {RELATIONSHIP1, RELATIONSHIP2});
13021302
List<Checksum> checksums = Arrays.asList(new Checksum[] {CHECKSUM2, CHECKSUM3, CHECKSUM1});
1303-
List<SpdxFile> files = Arrays.asList(new SpdxFile[] {FILE1, FILE2});
13041303
List<AnyLicenseInfo> licenseFromFiles = Arrays.asList(new AnyLicenseInfo[] {LICENSE2});
13051304
String id = gmo.getModelStore().getNextId(IdType.SpdxId, gmo.getDocumentUri());
13061305
List<ExternalRef> externalRefs = Arrays.asList(new ExternalRef[] {EXTERNAL_REF1});
@@ -1314,7 +1313,6 @@ public void testAddRelationship() throws InvalidSPDXAnalysisException {
13141313
.setLicenseComments(LICENSE_COMMENT1)
13151314
.setDescription(DESCRIPTION1)
13161315
.setDownloadLocation(DOWNLOAD_LOCATION1)
1317-
.setFiles(files)
13181316
.setHomepage(HOMEPAGE1)
13191317
.setOriginator(ORIGINATOR1)
13201318
.setPackageFileName(PACKAGEFILENAME1)

src/test/java/org/spdx/utility/compare/SpdxPackageComparerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,7 @@ public void testIsPackageFilesEquals() throws SpdxCompareException, InvalidSPDXA
10761076
assertTrue(pc.isPackageSuppliersEquals());
10771077
assertTrue(pc.isPackageVerificationCodesEquals());
10781078
assertTrue(pc.isPackageVersionsEquals());
1079-
assertTrue(pc.isRelationshipsEquals());
1080-
assertTrue(pc.isSeenLicenseEquals());
1079+
assertTrue(pc.isSeenLicenseEquals());
10811080
assertEquals(0, pc.getUniqueChecksums(DOCA, DOCB).size());
10821081
assertEquals(0, pc.getUniqueChecksums(DOCB, DOCA).size());
10831082
assertEquals(1, pc.getUniqueFiles(DOCA, DOCB).size());

0 commit comments

Comments
 (0)