Package org.spdx.core

Class CoreModelObject


  • public abstract class CoreModelObject
    extends Object
    Superclass for all SPDX model objects

    Provides the primary interface to the storage class that access and stores the data for the model objects.

    This class includes several helper methods to manage the storage and retrieval of properties.

    Each model object is in itself stateless. All state is maintained in the Model Store.

    The concrete classes are expected to implement getters for the model class properties which translate into calls to the getTYPEPropertyValue where TYPE is the type of value to be returned and the property descriptor is passed as a parameter.

    There are 2 methods of setting values:

    • Call the setPropertyValue, clearValueCollection or addValueToCollection methods. This will call the modelStore and store the value immediately.
    • Gather a list of updates by calling the updatePropertyValue, updateClearValueList, or updateAddPropertyValue methods. These methods return a ModelUpdate which can be applied later by calling the apply() method. A convenience method Write.applyUpdatesInOneTransaction will perform all updates within a single transaction. This method may result in higher performance updates for some Model Store implementations. Note that none of the updates will be applied until the storage manager update method is invoked.

    Property values are restricted to the following types:

    • String - Java Strings
    • Boolean - Java Boolean or primitive boolean types
    • CoreModelObject - A concrete subclass of this type
    • Collection<T> - A Collection of type T where T is one of the supported non-collection types

    This class also handles the conversion of a CoreModelObject to and from a TypeValue for storage in the ModelStore.

    Author:
    Gary O'Neall
    • Field Detail

      • objectUri

        protected String objectUri
      • specVersion

        protected String specVersion
      • idPrefix

        protected String idPrefix
      • copyManager

        protected IModelCopyManager copyManager
        If non-null, a reference made to a model object stored in a different modelStore and/or document will be copied to this modelStore and documentUri
      • strict

        protected boolean strict
        if true, checks input values for setters to verify valid SPDX inputs
    • Constructor Detail

      • CoreModelObject

        protected CoreModelObject​(String specVersion)
                           throws InvalidSPDXAnalysisException
        Create a new Model Object using an Anonymous ID with the default store and default document URI
        Parameters:
        specVersion - version of the SPDX spec the object complies with
        Throws:
        InvalidSPDXAnalysisException - on any SPDX related exception
      • CoreModelObject

        protected CoreModelObject​(String objectUri,
                                  String specVersion)
                           throws InvalidSPDXAnalysisException
        Open or create a model object with the default store
        Parameters:
        objectUri - Anonymous ID or URI for the model object
        specVersion - Version of the SPDX spec the object complies with
        Throws:
        InvalidSPDXAnalysisException - on any SPDX related exception
      • CoreModelObject

        protected CoreModelObject​(IModelStore modelStore,
                                  String objectUri,
                                  @Nullable
                                  IModelCopyManager copyManager,
                                  boolean create,
                                  String specVersion,
                                  String idPrefix)
                           throws InvalidSPDXAnalysisException
        Creates a new model object
        Parameters:
        modelStore - Storage for the model objects - Must support model V3 classes
        objectUri - Anonymous ID or URI for the model object
        copyManager - if supplied, model objects will be implicitly copied into this model store and document URI when referenced by setting methods
        create - if true, the object will be created in the store if it is not already present
        specVersion - version of the SPDX spec the object complies with
        idPrefix - prefix to be used when generating new SPDX IDs
        Throws:
        InvalidSPDXAnalysisException - invalid parameters or duplicate objectUri
    • Method Detail

      • isExternal

        public boolean isExternal()
        NOTE: Subclasses the represent model object not stored in the modelStore should override this method and set it to true
        Returns:
        true if this model object is external to the store
      • getType

        public abstract String getType()
        Returns:
        The class name for this object. Class names are defined in the constants file
      • leaveCriticalSection

        public void leaveCriticalSection​(IModelStore.IModelStoreLock lock)
        Leave a critical section. Releases the lock form the matching enterCriticalSection
      • _verify

        public abstract List<String> _verify​(Set<String> verifiedElementIds,
                                             String specVersion,
                                             List<IndividualUriValue> profiles)
        Implementation of the specific verifications for this model object
        Parameters:
        specVersion - Version of the SPDX spec to verify against
        verifiedElementIds - list of all Element Id's which have already been verified - prevents infinite recursion
        profiles - List of profiles to validate against
        Returns:
        Any verification errors or warnings associated with this object
      • verify

        public abstract List<String> verify​(Set<String> verifiedIds,
                                            String specVersion)
        Parameters:
        specVersion - Version of the SPDX spec to verify against
        verifiedIds - verifiedIds list of all Id's which have already been verified - prevents infinite recursion
        Returns:
        Any verification errors or warnings associated with this object
      • verify

        public List<String> verify​(String specVersion)
        Parameters:
        specVersion - Version of the SPDX spec to verify against
        Returns:
        Any verification errors or warnings associated with this object
      • verify

        public List<String> verify()
        Returns:
        Any verification errors or warnings associated with this object
      • verifyCollection

        public List<String> verifyCollection​(Collection<? extends CoreModelObject> collection,
                                             String warningPrefix,
                                             Set<String> verifiedIds,
                                             String specVersion)
        Verifies all elements in a collection
        Parameters:
        specVersion - version of the SPDX specification to verify against
        collection - collection to be verifies
        verifiedIds - verifiedIds list of all Id's which have already been verified - prevents infinite recursion
        warningPrefix - String to prefix any warning messages
      • getObjectUri

        public String getObjectUri()
        Returns:
        the Object URI or anonymous ID
      • getModelStore

        public IModelStore getModelStore()
        Returns:
        the model store for this object
      • isStrict

        public boolean isStrict()
        Returns:
        if strict input checking is enabled
      • setStrict

        public void setStrict​(boolean strict)
        Parameters:
        strict - if true, inputs will be validated against the SPDX spec
      • updatePropertyValue

        public IModelStore.ModelUpdate updatePropertyValue​(PropertyDescriptor propertyDescriptor,
                                                           Object value)
        Create an update when, when applied by the ModelStore, sets a property value for a property descriptor, creating the property if necessary
        Parameters:
        propertyDescriptor - Descriptor for the property associated with this object
        value - Value to associate with the property
        Returns:
        an update which can be applied by invoking the apply method
      • updateRemoveProperty

        public IModelStore.ModelUpdate updateRemoveProperty​(PropertyDescriptor propertyDescriptor)
        Create an update when, when applied by the ModelStore, removes a property and its value from the model store if it exists
        Parameters:
        propertyDescriptor - Descriptor for the property associated with this object to be removed
        Returns:
        an update which can be applied by invoking the apply method
      • updateClearValueCollection

        public IModelStore.ModelUpdate updateClearValueCollection​(PropertyDescriptor propertyDescriptor)
        Create an update when, when applied by the ModelStore, clears a collection of values associated with a property
        Parameters:
        propertyDescriptor - Descriptor for the property
        Returns:
        an update which can be applied by invoking the apply method
      • addPropertyValueToCollection

        public void addPropertyValueToCollection​(PropertyDescriptor propertyDescriptor,
                                                 Object value)
                                          throws InvalidSPDXAnalysisException
        Add a value to a collection of values associated with a property. If a value is a CoreModelObject and does not belong to the document, it will be copied into the object store
        Parameters:
        propertyDescriptor - Descriptor for the property
        value - to add
        Throws:
        InvalidSPDXAnalysisException - on any SPDX related exception
      • updateAddPropertyValueToCollection

        public IModelStore.ModelUpdate updateAddPropertyValueToCollection​(PropertyDescriptor propertyDescriptor,
                                                                          Object value)
        Create an update when, when applied, adds a value to a collection of values associated with a property. If a value is a CoreModelObject and does not belong to the document, it will be copied into the object store
        Parameters:
        propertyDescriptor - Descriptor for the property
        value - to add
        Returns:
        an update which can be applied by invoking the apply method
      • updateRemovePropertyValueFromCollection

        public IModelStore.ModelUpdate updateRemovePropertyValueFromCollection​(PropertyDescriptor propertyDescriptor,
                                                                               Object value)
        Create an update when, when applied, removes a property value from a collection
        Parameters:
        propertyDescriptor - descriptor for the property
        value - Value to be removed
        Returns:
        an update which can be applied by invoking the apply method
      • equivalent

        public boolean equivalent​(CoreModelObject compare,
                                  boolean ignoreRelatedElements)
                           throws InvalidSPDXAnalysisException
        Parameters:
        compare - model object to compare this model object to
        ignoreRelatedElements - if true, do not compare properties relatedSpdxElement - used to prevent infinite recursion
        Returns:
        true if all the properties have the same or equivalent values
        Throws:
        InvalidSPDXAnalysisException
      • isEquivalentToNull

        protected boolean isEquivalentToNull​(Object propertyValue,
                                             PropertyDescriptor descriptor)
        Parameters:
        propertyValue - value to check
        Returns:
        true if the value is equivalent to null
      • isRelatedElement

        public abstract boolean isRelatedElement​(PropertyDescriptor propertyDescriptor)
        Parameters:
        propertyDescriptor - property descriptor for the object in question
        Returns:
        true if the object is "to" part of a relationship
      • isNoAssertion

        protected abstract boolean isNoAssertion​(Object propertyValue)
        Parameters:
        propertyValue - value to check
        Returns:
        true if the propertyValue is a no assertion value
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • clone

        public CoreModelObject clone​(IModelStore modelStore)
        Clone a new object using a different model store
        Parameters:
        modelStore - model store to store the copy in
        Returns:
        a copy of this model object
      • setCopyManager

        public void setCopyManager​(IModelCopyManager copyManager)
        Set the copy manager
        Parameters:
        copyManager - copy manager to set
      • getCopyManager

        public IModelCopyManager getCopyManager()
        Returns:
        the copy manager - value may be null if copies are not allowed
      • getIdPrefix

        @Nullable
        public String getIdPrefix()
        Returns:
        prefix to be used when generating new IDs
      • setIdPrefix

        public void setIdPrefix​(@Nullable
                                String idPrefix)
        Sets the idPrefix
        Parameters:
        idPrefix - prefix to be used when generating new IDs
      • getId

        public String getId()
        Returns:
        if the objectUri is prefixed by the idPrefix, returns just the id portion of the objectUri, otherwise returns the objectUri