2929import java .util .Map .Entry ;
3030import java .util .Optional ;
3131import java .util .stream .Collectors ;
32+ import java .util .stream .Stream ;
3233
3334import org .slf4j .Logger ;
3435import org .slf4j .LoggerFactory ;
@@ -208,15 +209,21 @@ private void compareSnippets() throws SpdxCompareException {
208209 // N x N comparison of all snippets
209210 for (int i = 0 ; i < spdxDocs .size (); i ++) {
210211 List <SpdxSnippet > snippetsA ;
212+ Stream <SpdxSnippet > snippetStreamA = null ;
211213 try {
212- snippetsA = (List <SpdxSnippet >)SpdxModelFactory .getElements (spdxDocs .get (i ).getModelStore (), spdxDocs .get (i ).getDocumentUri (), null ,
213- SpdxSnippet .class ).collect (Collectors .toList ());
214+ snippetStreamA = (Stream <SpdxSnippet >)SpdxModelFactory .getElements (spdxDocs .get (i ).getModelStore (), spdxDocs .get (i ).getDocumentUri (), null ,
215+ SpdxSnippet .class );
216+ snippetsA = snippetStreamA .collect (Collectors .toList ());
214217 } catch (InvalidSPDXAnalysisException e ) {
215218 try {
216219 throw (new SpdxCompareException ("Error collecting snippets from SPDX document " +spdxDocs .get (i ).getName (), e ));
217220 } catch (InvalidSPDXAnalysisException e1 ) {
218221 throw (new SpdxCompareException ("Error collecting snippets from SPDX document " , e ));
219222 }
223+ } finally {
224+ if (Objects .nonNull (snippetStreamA )) {
225+ snippetStreamA .close ();
226+ }
220227 }
221228 // note - the snippet arrays MUST be sorted for the comparator methods to work
222229 Collections .sort (snippetsA );
@@ -230,15 +237,21 @@ private void compareSnippets() throws SpdxCompareException {
230237 continue ;
231238 }
232239 List <SpdxSnippet > snippetsB ;
240+ Stream <SpdxSnippet > snippetStreamB = null ;
233241 try {
234- snippetsB = (List <SpdxSnippet >)SpdxModelFactory .getElements (spdxDocs .get (j ).getModelStore (), spdxDocs .get (j ).getDocumentUri (), null ,
235- SpdxSnippet .class ).collect (Collectors .toList ());
242+ snippetStreamB = (Stream <SpdxSnippet >)SpdxModelFactory .getElements (spdxDocs .get (j ).getModelStore (), spdxDocs .get (j ).getDocumentUri (), null ,
243+ SpdxSnippet .class );
244+ snippetsB = snippetStreamB .collect (Collectors .toList ());
236245 } catch (InvalidSPDXAnalysisException e ) {
237246 try {
238247 throw (new SpdxCompareException ("Error collecting snippets from SPDX document " +spdxDocs .get (j ).getName (), e ));
239248 } catch (InvalidSPDXAnalysisException e1 ) {
240249 throw (new SpdxCompareException ("Error collecting snippets from SPDX document " , e ));
241250 }
251+ } finally {
252+ if (Objects .nonNull (snippetStreamB )) {
253+ snippetStreamB .close ();
254+ }
242255 }
243256 //Note that the files arrays must be sorted for the find methods to work
244257 Collections .sort (snippetsB );
@@ -430,8 +443,10 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
430443 // N x N comparison of all files
431444 for (int i = 0 ; i < spdxDocs .size (); i ++) {
432445 List <SpdxFile > filesListA ;
433- filesListA = (List <SpdxFile >)SpdxModelFactory .getElements (spdxDocs .get (i ).getModelStore (), spdxDocs .get (i ).getDocumentUri (),
434- null , SpdxFile .class ).collect (Collectors .toList ());
446+ Stream <SpdxFile > fileStreamA = (Stream <SpdxFile >) SpdxModelFactory .getElements (spdxDocs .get (i ).getModelStore (), spdxDocs .get (i ).getDocumentUri (),
447+ null , SpdxFile .class );
448+ filesListA = fileStreamA .collect (Collectors .toList ());
449+ fileStreamA .close ();
435450 // note - the file arrays MUST be sorted for the comparator methods to work
436451 Collections .sort (filesListA );
437452 SpdxFile [] filesA = filesListA .toArray (new SpdxFile [filesListA .size ()]);
@@ -448,10 +463,11 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
448463 if (j == i ) {
449464 continue ;
450465 }
451- List <SpdxFile > filesListB ;
452- filesListB = (List <SpdxFile >)SpdxModelFactory .getElements (spdxDocs .get (j ).getModelStore (), spdxDocs .get (j ).getDocumentUri (),
453- null , SpdxFile .class ).collect (Collectors .toList ());
454- //Note that the files arrays must be sorted for the find methods to work
466+ Stream <SpdxFile > fileStreamB = (Stream <SpdxFile >)SpdxModelFactory .getElements (spdxDocs .get (j ).getModelStore (), spdxDocs .get (j ).getDocumentUri (),
467+ null , SpdxFile .class );
468+ List <SpdxFile > filesListB = (List <SpdxFile >)fileStreamB .collect (Collectors .toList ());
469+ fileStreamB .close ();
470+ //Note that the files arrays must be sorted for the find methods to work
455471 Collections .sort (filesListB );
456472 SpdxFile [] filesB = filesListB .toArray (new SpdxFile [filesListB .size ()]);
457473 List <SpdxFile > uniqueAB = findUniqueFiles (filesA , filesB );
@@ -485,9 +501,12 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
485501 */
486502 @ SuppressWarnings ("unchecked" )
487503 protected List <SpdxPackage > collectAllPackages (SpdxDocument spdxDocument ) throws InvalidSPDXAnalysisException {
488- return (List <SpdxPackage >)SpdxModelFactory .getElements (
489- spdxDocument .getModelStore (), spdxDocument .getDocumentUri (),
490- null , SpdxPackage .class ).collect (Collectors .toList ());
504+ Stream <SpdxPackage > packageStream = (Stream <SpdxPackage >) SpdxModelFactory .getElements (
505+ spdxDocument .getModelStore (), spdxDocument .getDocumentUri (),
506+ null , SpdxPackage .class );
507+ List <SpdxPackage > retval = packageStream .collect (Collectors .toList ());
508+ packageStream .close ();
509+ return retval ;
491510 }
492511
493512 /**
@@ -499,8 +518,11 @@ protected List<SpdxPackage> collectAllPackages(SpdxDocument spdxDocument) throws
499518 */
500519 @ SuppressWarnings ("unchecked" )
501520 public List <SpdxFile > collectAllFiles (SpdxDocument spdxDocument ) throws InvalidSPDXAnalysisException {
502- return (List <SpdxFile >)SpdxModelFactory .getElements (spdxDocument .getModelStore (), spdxDocument .getDocumentUri (),
503- null , SpdxFile .class ).collect (Collectors .toList ());
521+ Stream <SpdxFile > fileElementStream = (Stream <SpdxFile >) SpdxModelFactory .getElements (spdxDocument .getModelStore (), spdxDocument .getDocumentUri (),
522+ null , SpdxFile .class );
523+ List <SpdxFile > retval = fileElementStream .collect (Collectors .toList ());
524+ fileElementStream .close ();
525+ return retval ;
504526 }
505527
506528 /**
0 commit comments