@@ -766,7 +766,7 @@ public boolean equivalent(ModelObject compare, boolean ignoreRelatedElements) th
766766 List <String > propertyValueNames = getPropertyValueNames ();
767767 List <String > comparePropertyValueNames = new ArrayList <String >(compare .getPropertyValueNames ()); // create a copy since we're going to modify it
768768 for (String propertyName :propertyValueNames ) {
769- if (ignoreRelatedElements && SpdxConstants . PROP_RELATED_SPDX_ELEMENT . equals (propertyName )) {
769+ if (ignoreRelatedElements && isRelatedElement (propertyName )) {
770770 continue ;
771771 }
772772 if (comparePropertyValueNames .contains (propertyName )) {
@@ -779,36 +779,59 @@ public boolean equivalent(ModelObject compare, boolean ignoreRelatedElements) th
779779 comparePropertyValueNames .remove (propertyName );
780780 } else {
781781 // No property value
782- Optional <Object > propertyValue = this .getObjectPropertyValue (propertyName );
783- if (propertyValue .isPresent ()) {
784- if (propertyValue .get () instanceof ModelCollection ) {
785- if (((ModelCollection <?>)propertyValue .get ()).size () > 0 ) {
786- lastNotEquivalentReason = new NotEquivalentReason (
787- NotEquivalent .COMPARE_PROPERTY_MISSING , propertyName );
788- return false ;
789- }
790- } else if (!propertyNoAssertion (propertyValue )) {
791- lastNotEquivalentReason = new NotEquivalentReason (
792- NotEquivalent .COMPARE_PROPERTY_MISSING , propertyName );
793- return false ;
782+ Optional <Object > propertyValueOptional = this .getObjectPropertyValue (propertyName );
783+ if (propertyValueOptional .isPresent ()) {
784+ Object propertyValue = propertyValueOptional .get ();
785+ if (isEquivalentToNull (propertyValue )) {
786+ continue ;
794787 }
788+ lastNotEquivalentReason = new NotEquivalentReason (
789+ NotEquivalent .COMPARE_PROPERTY_MISSING , propertyName );
790+ return false ;
795791 }
796792 }
797793 }
798- for (String propertyName :comparePropertyValueNames ) { // check any remaining property values
799- if (!compare .getObjectPropertyValue (propertyName ).isPresent ()) {
800- lastNotEquivalentReason = new NotEquivalentReason (
801- NotEquivalent .MISSING_PROPERTY , propertyName );
802- return false ;
794+ for (String propertyName :comparePropertyValueNames ) { // check any remaining property values
795+ if (ignoreRelatedElements && isRelatedElement (propertyName )) {
796+ continue ;
803797 }
798+ Optional <Object > comparePropertyValueOptional = compare .getObjectPropertyValue (propertyName );
799+ if (!comparePropertyValueOptional .isPresent ()) {
800+ continue ;
801+ }
802+ Object comparePropertyValue = comparePropertyValueOptional .get ();
803+ if (isEquivalentToNull (comparePropertyValue )) {
804+ continue ;
805+ }
806+ lastNotEquivalentReason = new NotEquivalentReason (
807+ NotEquivalent .MISSING_PROPERTY , propertyName );
808+ return false ;
804809 }
805810 return true ;
806811 }
807812
808- boolean propertyNoAssertion (Optional <Object > propertyValue ) {
809- return propertyValue .isPresent () &&
810- (propertyValue .get () instanceof SpdxNoAssertionLicense ||
811- propertyValue .get ().equals ("NOASSERTION" ));
813+ // Some values are treated like null in comparisons - in particular empty model collections and
814+ // "no assertion" values.
815+ private boolean isEquivalentToNull (Object propertyValue ) {
816+ if (propertyValue instanceof ModelCollection ) {
817+ return ((ModelCollection <?>) propertyValue ).size () == 0 ;
818+ } else {
819+ return isNoAssertion (propertyValue );
820+ }
821+ }
822+
823+ private boolean isRelatedElement (String propertyName ) {
824+ return SpdxConstants .PROP_RELATED_SPDX_ELEMENT .equals (propertyName );
825+ }
826+
827+ private boolean isEmptyModelCollection (Object value ) {
828+ return (value instanceof ModelCollection )
829+ && (((ModelCollection <?>) value ).size () == 0 );
830+ }
831+
832+ private boolean isNoAssertion (Object propertyValue ) {
833+ return propertyValue instanceof SpdxNoAssertionLicense ||
834+ propertyValue .equals (SpdxConstants .NOASSERTION_VALUE );
812835 }
813836
814837 /**
@@ -823,22 +846,10 @@ private boolean propertyValuesEquivalent(String propertyName, Optional<Object> v
823846 Optional <Object > valueB , boolean ignoreRelatedElements ) throws InvalidSPDXAnalysisException {
824847 if (!valueA .isPresent ()) {
825848 if (valueB .isPresent ()) {
826- if (valueB .get () instanceof ModelCollection ) {
827- if (((ModelCollection <?>)valueB .get ()).size () > 0 ) {
828- return false ;
829- }
830- } else {
831- return false ;
832- }
849+ return isEmptyModelCollection (valueB .get ());
833850 }
834851 } else if (!valueB .isPresent ()) {
835- if (valueA .get () instanceof ModelCollection ) {
836- if (((ModelCollection <?>)valueA .get ()).size () > 0 ) {
837- return false ;
838- }
839- } else {
840- return false ;
841- }
852+ return isEmptyModelCollection (valueA .get ());
842853 } else if (valueA .get () instanceof ModelCollection && valueB .get () instanceof ModelCollection ) {
843854 List <?> myList = ((ModelCollection <?>)valueA .get ()).toImmutableList ();
844855 List <?> compareList = ((ModelCollection <?>)valueB .get ()).toImmutableList ();
@@ -856,7 +867,7 @@ private boolean propertyValuesEquivalent(String propertyName, Optional<Object> v
856867 // Note: we must check the IndividualValue before the ModelObject types since the IndividualValue takes precedence
857868 } else if (valueA .get () instanceof ModelObject && valueB .get () instanceof ModelObject ) {
858869 if (!((ModelObject )valueA .get ()).equivalent (((ModelObject )valueB .get ()),
859- SpdxConstants . PROP_RELATED_SPDX_ELEMENT . equals (propertyName ) ? true : ignoreRelatedElements )) {
870+ isRelatedElement (propertyName ) ? true : ignoreRelatedElements )) {
860871 return false ;
861872 }
862873
0 commit comments