Package org.spdx.utility.compare
Class SpdxComparer
- java.lang.Object
-
- org.spdx.utility.compare.SpdxComparer
-
public class SpdxComparer extends Object
Performs a comparison between two or more SPDX documents and holds the results of the comparison The main function to perform the comparison iscompare(spdxdoc1, spdxdoc2)
For files, the comparison results are separated into unique files based on the file names which can be obtained by the methodgetUniqueFiles(index1, index2)
. If two documents contain files with the same name, but different data, the differences for these files can be obtained through the methodgetFileDifferences(index1, index2)
Multi-threading considerations: This class is "mostly" threadsafe in that the calls to perform the comparison are synchronized and a flag is used to throw an error for any calls to getters when a compare is in progress. There is a small theoretical window in the getters where the compare operation is started in the middle of a get operation.- Author:
- Gary O'Neall
-
-
Constructor Summary
Constructors Constructor Description SpdxComparer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<org.spdx.library.model.v2.SpdxFile>
collectAllFiles(org.spdx.library.model.v2.SpdxDocument spdxDocument)
Collect all of the files present in the SPDX document including files within documents and files embedded in packagesprotected List<org.spdx.library.model.v2.SpdxPackage>
collectAllPackages(org.spdx.library.model.v2.SpdxDocument spdxDocument)
Collect all of the packages present in the SPDX document including packages embedded in other relationships within documentsstatic boolean
collectionsEquals(Collection<? extends Object> a1, Collection<? extends Object> a2)
Compare two object listsstatic boolean
collectionsEquivalent(Collection<? extends org.spdx.library.model.v2.ModelObjectV2> collectionA, Collection<? extends org.spdx.library.model.v2.ModelObjectV2> collectionB)
void
compare(List<org.spdx.library.model.v2.SpdxDocument> spdxDocuments)
Compares multiple SPDX documentsvoid
compare(org.spdx.library.model.v2.SpdxDocument spdxDoc1, org.spdx.library.model.v2.SpdxDocument spdxDoc2)
Compares 2 SPDX documentsboolean
compareLicense(int doc1, org.spdx.library.model.v2.license.AnyLicenseInfo license1, int doc2, org.spdx.library.model.v2.license.AnyLicenseInfo license2)
Compares two licenses from two different SPDX documents taking into account the extracted license infos who's ID's may be different between the two documents Note: The ExtracedLicenseIDMap must be initialized before this method is invokedstatic int
compareStrings(String stringA, String stringB)
Compares two strings including trimming the string and taking into account they may be null.static int
compareStrings(Optional<String> stringA, Optional<String> stringB)
Compares two strings including trimming the string and taking into account they may be null.static boolean
elementsEquivalent(Optional<? extends org.spdx.library.model.v2.ModelObjectV2> elementA, Optional<? extends org.spdx.library.model.v2.ModelObjectV2> elementB)
static List<org.spdx.library.model.v2.Annotation>
findUniqueAnnotations(Collection<org.spdx.library.model.v2.Annotation> annotationsA, Collection<org.spdx.library.model.v2.Annotation> annotationsB)
Find any SPDX annotations which are in annotationsA but not in annotationsBstatic List<org.spdx.library.model.v2.Checksum>
findUniqueChecksums(Collection<org.spdx.library.model.v2.Checksum> checksumsA, Collection<org.spdx.library.model.v2.Checksum> checksumsB)
Find any SPDX checksums which are in elementsA but not in elementsBstatic List<org.spdx.library.model.v2.ExternalDocumentRef>
findUniqueExternalDocumentRefs(Collection<org.spdx.library.model.v2.ExternalDocumentRef> externalDocRefsA, Collection<org.spdx.library.model.v2.ExternalDocumentRef> externalDocRefsB)
Find unique relationships that are present in relationshipsA but not relationshipsBstatic List<org.spdx.library.model.v2.Relationship>
findUniqueRelationships(Collection<org.spdx.library.model.v2.Relationship> relationshipsA, Collection<org.spdx.library.model.v2.Relationship> relationshipsB)
Find unique relationships that are present in relationshipsA but not relationshipsBList<SpdxLicenseDifference>
getExtractedLicenseDifferences(int docIndexA, int docIndexB)
Retrieves any licenses which where the text matches in both documents but other fields are differentList<SpdxFileDifference>
getFileDifferences(int docindex1, int docindex2)
Returns any file differences found between the first and second SPDX documents as specified by the document indexint
getNumSpdxDocs()
SpdxPackageComparer[]
getPackageComparers()
List<SpdxPackageComparer>
getPackageDifferences()
SpdxSnippetComparer[]
getSnippetComparers()
org.spdx.library.model.v2.SpdxDocument
getSpdxDoc(int docIndex)
List<org.spdx.library.model.v2.SpdxDocument>
getSpdxDocuments()
List<String>
getUniqueCreators(int doc1index, int doc2index)
Returns any creators which are in the SPDX document 1 which are not in document 2List<org.spdx.library.model.v2.Annotation>
getUniqueDocumentAnnotations(int docindex1, int docindex2)
Return any document annotations which are in spdx document index 1 but not in spdx document index 2List<org.spdx.library.model.v2.Relationship>
getUniqueDocumentRelationship(int docindex1, int docindex2)
Return any document annotations which are in spdx document index 1 but not in spdx document index 2List<org.spdx.library.model.v2.ExternalDocumentRef>
getUniqueExternalDocumentRefs(int docindex1, int docindex2)
Return any external document references which are in spdx document index 1 but not in spdx document index 2List<org.spdx.library.model.v2.license.ExtractedLicenseInfo>
getUniqueExtractedLicenses(int docIndexA, int docIndexB)
Retrieves any unique extracted licenses fromt the first SPDX document index relative to the second - unique is determined by the license text matchingList<org.spdx.library.model.v2.SpdxFile>
getUniqueFiles(int docindex1, int docindex2)
Return any files which are in spdx document index 1 but not in spdx document index 2List<org.spdx.library.model.v2.SpdxPackage>
getUniquePackages(int docindex1, int docindex2)
Return any files which are in spdx document index 1 but not in spdx document index 2boolean
isCreatorCommentsEqual()
boolean
isCreatorDatesEqual()
boolean
isCreatorInformationEqual()
boolean
isDataLicenseEqual()
boolean
isDifferenceFound()
boolean
isDocumentAnnotationsEquals()
boolean
isDocumentCommentsEqual()
boolean
isDocumentContentsEquals()
boolean
isDocumentRelationshipsEquals()
boolean
isExternalDcoumentRefsEquals()
boolean
isExtractedLicensingInfosEqual()
boolean
isfilesEquals()
boolean
isLicenseListVersionEqual()
boolean
isPackagesEquals()
boolean
isSnippetsEqual()
boolean
isSpdxVersionEqual()
static boolean
listsEquals(List<? extends Object> a1, List<? extends Object> a2)
Compare two object listsstatic boolean
objectsEqual(Object o1, Object o2)
returns true if the two objects are equal considering nullsstatic boolean
stringCollectionsEqual(Collection<String> stringsA, Collection<String> stringsB)
Compares 2 collections and returns true if the contents are equal ignoring order and trimming strings.static boolean
stringsEqual(String stringA, String stringB)
Compares two strings returning true if they are equal considering null values and trimming the strings.static boolean
stringsEqual(Optional<String> stringA, Optional<String> stringB)
Compares two strings returning true if they are equal considering null values and trimming the strings.
-
-
-
Method Detail
-
compare
public void compare(org.spdx.library.model.v2.SpdxDocument spdxDoc1, org.spdx.library.model.v2.SpdxDocument spdxDoc2) throws org.spdx.core.InvalidSPDXAnalysisException, SpdxCompareException
Compares 2 SPDX documents- Parameters:
spdxDoc1
-spdxDoc2
-- Throws:
org.spdx.core.InvalidSPDXAnalysisException
SpdxCompareException
-
compare
public void compare(List<org.spdx.library.model.v2.SpdxDocument> spdxDocuments) throws org.spdx.core.InvalidSPDXAnalysisException, SpdxCompareException
Compares multiple SPDX documents- Parameters:
spdxDocuments
-- Throws:
SpdxCompareException
org.spdx.core.InvalidSPDXAnalysisException
-
collectAllPackages
protected List<org.spdx.library.model.v2.SpdxPackage> collectAllPackages(org.spdx.library.model.v2.SpdxDocument spdxDocument) throws org.spdx.core.InvalidSPDXAnalysisException
Collect all of the packages present in the SPDX document including packages embedded in other relationships within documents- Parameters:
spdxDocument
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
collectAllFiles
public List<org.spdx.library.model.v2.SpdxFile> collectAllFiles(org.spdx.library.model.v2.SpdxDocument spdxDocument) throws org.spdx.core.InvalidSPDXAnalysisException
Collect all of the files present in the SPDX document including files within documents and files embedded in packages- Parameters:
spdxDocument
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
compareLicense
public boolean compareLicense(int doc1, org.spdx.library.model.v2.license.AnyLicenseInfo license1, int doc2, org.spdx.library.model.v2.license.AnyLicenseInfo license2) throws SpdxCompareException
Compares two licenses from two different SPDX documents taking into account the extracted license infos who's ID's may be different between the two documents Note: The ExtracedLicenseIDMap must be initialized before this method is invoked- Parameters:
doc1
- Index of the SPDX document for license1license1
-doc2
- Index of the SPDX document for license2license2
-- Returns:
- true if the licenses are equivalent
- Throws:
SpdxCompareException
-
stringCollectionsEqual
public static boolean stringCollectionsEqual(Collection<String> stringsA, Collection<String> stringsB)
Compares 2 collections and returns true if the contents are equal ignoring order and trimming strings. Nulls are also considered as equal to other nulls.- Parameters:
stringsA
-stringsB
-- Returns:
-
objectsEqual
public static boolean objectsEqual(Object o1, Object o2)
returns true if the two objects are equal considering nulls- Parameters:
o1
-o2
-- Returns:
-
elementsEquivalent
public static boolean elementsEquivalent(Optional<? extends org.spdx.library.model.v2.ModelObjectV2> elementA, Optional<? extends org.spdx.library.model.v2.ModelObjectV2> elementB) throws org.spdx.core.InvalidSPDXAnalysisException
- Parameters:
elementA
-elementB
-- Returns:
- true of the elements are present and equivalent
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
collectionsEquivalent
public static boolean collectionsEquivalent(Collection<? extends org.spdx.library.model.v2.ModelObjectV2> collectionA, Collection<? extends org.spdx.library.model.v2.ModelObjectV2> collectionB) throws org.spdx.core.InvalidSPDXAnalysisException
- Parameters:
collectionA
-collectionB
-- Returns:
- true if the collections all contain equivalent items
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
listsEquals
public static boolean listsEquals(List<? extends Object> a1, List<? extends Object> a2)
Compare two object lists- Parameters:
a1
-a2
-- Returns:
-
collectionsEquals
public static boolean collectionsEquals(Collection<? extends Object> a1, Collection<? extends Object> a2)
Compare two object lists- Parameters:
a1
-a2
-- Returns:
-
stringsEqual
public static boolean stringsEqual(String stringA, String stringB)
Compares two strings returning true if they are equal considering null values and trimming the strings. and normalizing linefeeds. Empty strings are treated as the same as null values.- Parameters:
stringA
-stringB
-- Returns:
-
stringsEqual
public static boolean stringsEqual(Optional<String> stringA, Optional<String> stringB)
Compares two strings returning true if they are equal considering null values and trimming the strings. and normalizing linefeeds. Empty strings are treated as the same as null values.- Parameters:
stringA
-stringB
-- Returns:
-
compareStrings
public static int compareStrings(String stringA, String stringB)
Compares two strings including trimming the string and taking into account they may be null. Null is considered a smaller value- Parameters:
stringA
-stringB
-- Returns:
-
compareStrings
public static int compareStrings(Optional<String> stringA, Optional<String> stringB)
Compares two strings including trimming the string and taking into account they may be null. Null is considered a smaller value- Parameters:
stringA
-stringB
-- Returns:
-
isDifferenceFound
public boolean isDifferenceFound()
- Returns:
-
isSpdxVersionEqual
public boolean isSpdxVersionEqual() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
getSpdxDoc
public org.spdx.library.model.v2.SpdxDocument getSpdxDoc(int docIndex) throws SpdxCompareException
- Parameters:
docIndex
- Reference to which document number - 0 is the first document parameter in compare- Returns:
- Throws:
SpdxCompareException
-
isDataLicenseEqual
public boolean isDataLicenseEqual() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isDocumentCommentsEqual
public boolean isDocumentCommentsEqual() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isExternalDcoumentRefsEquals
public boolean isExternalDcoumentRefsEquals() throws SpdxCompareException
- Throws:
SpdxCompareException
-
isExtractedLicensingInfosEqual
public boolean isExtractedLicensingInfosEqual() throws SpdxCompareException
- Throws:
SpdxCompareException
-
getUniqueExtractedLicenses
public List<org.spdx.library.model.v2.license.ExtractedLicenseInfo> getUniqueExtractedLicenses(int docIndexA, int docIndexB) throws SpdxCompareException
Retrieves any unique extracted licenses fromt the first SPDX document index relative to the second - unique is determined by the license text matching- Parameters:
docIndexA
-docIndexB
-- Returns:
- Throws:
SpdxCompareException
-
getExtractedLicenseDifferences
public List<SpdxLicenseDifference> getExtractedLicenseDifferences(int docIndexA, int docIndexB) throws SpdxCompareException
Retrieves any licenses which where the text matches in both documents but other fields are different- Parameters:
docIndexA
-docIndexB
-- Returns:
- Throws:
SpdxCompareException
-
isCreatorInformationEqual
public boolean isCreatorInformationEqual() throws SpdxCompareException
- Returns:
- true if all creation information fields equals
- Throws:
SpdxCompareException
-
isCreatorCommentsEqual
public boolean isCreatorCommentsEqual() throws SpdxCompareException
- Returns:
- true all creator comments equal
- Throws:
SpdxCompareException
-
isCreatorDatesEqual
public boolean isCreatorDatesEqual() throws SpdxCompareException
- Returns:
- true if all creation information fields equals
- Throws:
SpdxCompareException
-
getUniqueCreators
public List<String> getUniqueCreators(int doc1index, int doc2index) throws SpdxCompareException
Returns any creators which are in the SPDX document 1 which are not in document 2- Parameters:
doc1index
-doc2index
-- Returns:
- Throws:
SpdxCompareException
-
isfilesEquals
public boolean isfilesEquals() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isPackagesEquals
public boolean isPackagesEquals() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isDocumentAnnotationsEquals
public boolean isDocumentAnnotationsEquals() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isDocumentRelationshipsEquals
public boolean isDocumentRelationshipsEquals() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
getUniqueFiles
public List<org.spdx.library.model.v2.SpdxFile> getUniqueFiles(int docindex1, int docindex2) throws SpdxCompareException
Return any files which are in spdx document index 1 but not in spdx document index 2- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getFileDifferences
public List<SpdxFileDifference> getFileDifferences(int docindex1, int docindex2) throws SpdxCompareException
Returns any file differences found between the first and second SPDX documents as specified by the document index- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getUniquePackages
public List<org.spdx.library.model.v2.SpdxPackage> getUniquePackages(int docindex1, int docindex2) throws SpdxCompareException
Return any files which are in spdx document index 1 but not in spdx document index 2- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getUniqueExternalDocumentRefs
public List<org.spdx.library.model.v2.ExternalDocumentRef> getUniqueExternalDocumentRefs(int docindex1, int docindex2) throws SpdxCompareException
Return any external document references which are in spdx document index 1 but not in spdx document index 2- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getUniqueDocumentAnnotations
public List<org.spdx.library.model.v2.Annotation> getUniqueDocumentAnnotations(int docindex1, int docindex2) throws SpdxCompareException
Return any document annotations which are in spdx document index 1 but not in spdx document index 2- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getUniqueDocumentRelationship
public List<org.spdx.library.model.v2.Relationship> getUniqueDocumentRelationship(int docindex1, int docindex2) throws SpdxCompareException
Return any document annotations which are in spdx document index 1 but not in spdx document index 2- Parameters:
docindex1
-docindex2
-- Returns:
- Throws:
SpdxCompareException
-
getPackageDifferences
public List<SpdxPackageComparer> getPackageDifferences() throws SpdxCompareException
- Returns:
- Package comparers where there is at least one difference
- Throws:
SpdxCompareException
-
getPackageComparers
public SpdxPackageComparer[] getPackageComparers()
- Returns:
- all package comparers
-
getNumSpdxDocs
public int getNumSpdxDocs()
- Returns:
-
isLicenseListVersionEqual
public boolean isLicenseListVersionEqual() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
findUniqueChecksums
public static List<org.spdx.library.model.v2.Checksum> findUniqueChecksums(Collection<org.spdx.library.model.v2.Checksum> checksumsA, Collection<org.spdx.library.model.v2.Checksum> checksumsB) throws org.spdx.core.InvalidSPDXAnalysisException
Find any SPDX checksums which are in elementsA but not in elementsB- Parameters:
checksumsA
-checksumsB
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
findUniqueAnnotations
public static List<org.spdx.library.model.v2.Annotation> findUniqueAnnotations(Collection<org.spdx.library.model.v2.Annotation> annotationsA, Collection<org.spdx.library.model.v2.Annotation> annotationsB) throws org.spdx.core.InvalidSPDXAnalysisException
Find any SPDX annotations which are in annotationsA but not in annotationsB- Parameters:
annotationsA
-annotationsB
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
findUniqueRelationships
public static List<org.spdx.library.model.v2.Relationship> findUniqueRelationships(Collection<org.spdx.library.model.v2.Relationship> relationshipsA, Collection<org.spdx.library.model.v2.Relationship> relationshipsB) throws org.spdx.core.InvalidSPDXAnalysisException
Find unique relationships that are present in relationshipsA but not relationshipsB- Parameters:
relationshipsA
-relationshipsB
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
findUniqueExternalDocumentRefs
public static List<org.spdx.library.model.v2.ExternalDocumentRef> findUniqueExternalDocumentRefs(Collection<org.spdx.library.model.v2.ExternalDocumentRef> externalDocRefsA, Collection<org.spdx.library.model.v2.ExternalDocumentRef> externalDocRefsB) throws org.spdx.core.InvalidSPDXAnalysisException
Find unique relationships that are present in relationshipsA but not relationshipsB- Parameters:
externalDocRefsA
-externalDocRefsB
-- Returns:
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
-
getSpdxDocuments
public List<org.spdx.library.model.v2.SpdxDocument> getSpdxDocuments()
- Returns:
-
isDocumentContentsEquals
public boolean isDocumentContentsEquals() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
isSnippetsEqual
public boolean isSnippetsEqual() throws SpdxCompareException
- Returns:
- Throws:
SpdxCompareException
-
getSnippetComparers
public SpdxSnippetComparer[] getSnippetComparers()
- Returns:
- all snippet comparers
-
-