@@ -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 */
0 commit comments