Class SchemaModel

All Implemented Interfaces:
DiffGenerating<SchemaModel>, Cloneable

public class SchemaModel extends AbstractXMLStreaming implements DiffGenerating<SchemaModel>
Models one Permazen Database schema version.
  • Field Details

    • ITEM_TYPE

      public static final ItemType ITEM_TYPE
      The ItemType that this class represents.
  • Constructor Details

    • SchemaModel

      public SchemaModel()
  • Method Details

    • getSchemaObjectTypes

      public NavigableMap<String,SchemaObjectType> getSchemaObjectTypes()
      Get the object types defined in this schema.
      Returns:
      object types keyed by name
    • isEmpty

      public boolean isEmpty()
      Determine if this schema is empty, i.e., defines zero object types.
      Returns:
      true if this is an empty schema
    • visitSchemaItems

      public void visitSchemaItems(Consumer<? super SchemaItem> visitor)
      Visit all SchemaItem descendents of this instance with the given visitor.

      If this instance is also a SchemaItem, then also visit this instance.

      The traversal is depth first, pre-order.

      Parameters:
      visitor - visitor for schema items
    • lockDown

      public void lockDown(boolean includingStorageIds)
      Lock down this SchemaModel and every SchemaItem it contains so that no further changes can be made.

      There are two levels of lock down: the first level locks everything, except storage ID's that are zero may be changed to a non-zero value. The second level locks down everything. Levels increase monotonically.

      Attempts to modify a locked down schema item generate an UnsupportedOperationException.

      Parameters:
      includingStorageIds - false to exclude storage ID's, true to lock down everything
    • toXML

      public void toXML(OutputStream output, boolean includeStorageIds, boolean prettyPrint) throws IOException
      Serialize this instance to the given XML output.

      The output is not closed by this method.

      Parameters:
      output - XML output
      includeStorageIds - true to include storage ID's
      prettyPrint - true to indent the XML and add schema ID comments
      Throws:
      IOException - if an I/O error occurs
      IllegalArgumentException - if output is null
    • fromXML

      public static SchemaModel fromXML(InputStream input) throws IOException
      Deserialize an instance from the given XML input.
      Parameters:
      input - XML input
      Returns:
      deserialized schema model
      Throws:
      IOException - if an I/O error occurs
      InvalidSchemaException - if the XML input or decoded SchemaModel is invalid
      IllegalArgumentException - if input is null
    • validate

      public final void validate()
      Validate this instance.

      This performs basic structural validation. Full validation is not possible without a Database instance; for example, simple field EncodingId's must be resolved in the associated EncodingRegistry.

      Once this instance is locked down, repeated invocations of this method will be very fast, just returning the cached previous result.

      Throws:
      InvalidSchemaException - if this instance is invalid
      IllegalStateException - if this instance is not locked down
    • validateWithEncodings

      public void validateWithEncodings(EncodingRegistry encodingRegistry)
      Validate this instance itself and also verify that all of its field encodings can be found in the given EncodingRegistry.
      Parameters:
      encodingRegistry - registry of encodings
      Throws:
      InvalidSchemaException - if this instance is itself invalid
      InvalidSchemaException - if any simple field's encoding ID can't be resovled by encodingRegistry
      IllegalStateException - if this instance is not locked down
      IllegalArgumentException - if encodingRegistry is null
    • resetStorageIds

      public void resetStorageIds()
      Reset all non-zero storage ID's in this instance back to zero.
      Throws:
      UnsupportedOperationException - if this instance is fully locked down
    • getItemType

      public final ItemType getItemType()
      Get the ItemType corresponding to this instance.
      Returns:
      schema item type
    • differencesFrom

      public Diffs differencesFrom(SchemaModel that)
      Description copied from interface: DiffGenerating
      Detect the differences of this instance when compared to the given instance.
      Specified by:
      differencesFrom in interface DiffGenerating<SchemaModel>
      Parameters:
      that - other instance
      Returns:
      differences; will be empty if there are none detected
    • readXML

      public void readXML(XMLStreamReader reader, boolean openTag) throws XMLStreamException
      Reset this instance and (re)populate it from the given XML input.
      Parameters:
      reader - XML input
      openTag - true if the opening XML <SchemaModel> tag has already been read from input, false to expect to read the tag as the next XML element
      Throws:
      XMLStreamException - if the XML input is invalid
      IllegalArgumentException - if reader is null
    • writeXML

      public void writeXML(XMLStreamWriter writer, boolean includeStorageIds, boolean prettyPrint) throws XMLStreamException
      Write this instance to the given XML output.
      Parameters:
      writer - XML output
      includeStorageIds - true to include storage ID's
      prettyPrint - true to indent and include SchemaId comments
      Throws:
      XMLStreamException - if an XML error occurs
      IllegalArgumentException - if writer is null
    • toString

      public String toString()
      Returns this schema model in XML form.
      Overrides:
      toString in class Object
    • toString

      public String toString(boolean includeStorageIds, boolean prettyPrint)
      Returns this schema model in XML form.
      Parameters:
      includeStorageIds - true to include storage ID's
      prettyPrint - true to indent and include SchemaId comments
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public SchemaModel clone()
      Deep-clone this instance.
    • visitSchemaItems

      public final <T extends SchemaItem> void visitSchemaItems(Class<T> nodeType, Consumer<? super T> visitor)
      Visit this schema item and all of its descendents matching the given type with the given visitor.

      The traversal is depth first, post-order.

      Parameters:
      nodeType - node type to include
      visitor - visitor for schema items
      Throws:
      IllegalArgumentException - if either parameter is null
    • isLockedDown

      public final boolean isLockedDown(boolean includingStorageIds)
      Determine whether this instance is locked down.
      Parameters:
      includingStorageIds - false to test all but storage ID's, true to require storage ID's to be locked down as well
      Returns:
      true if instance is locked down, otherwise false
    • getSchemaId

      public final SchemaId getSchemaId()
      Generate a unique SchemaId corresponding to the type and encoding structure of this schema item.

      The SchemaId does not depend on the storage ID.

      This instance must be locked down except for storage ID's. Repeated invocations of this method will be very fast, just returning the cached previous result.

      Returns:
      schema ID
      Throws:
      IllegalStateException - if this instance is not locked down
      See Also: