org.schemarepo
Class Subject

java.lang.Object
  extended by org.schemarepo.Subject
Direct Known Subclasses:
DelegatingSubject

public abstract class Subject
extends Object

A Subject is a collection of mutually compatible Schemas.

Validation of schemas is pluggable and each subject may have its own validation rules defined with its own Validator when registered with a Repository. To create a Subject that validates its schemas, use validatingSubject(Subject, ValidatorFactory).

Caching of schemas is pluggable via cacheWith(Subject, SchemaEntryCache). A Subject can only cache the schema to id mappings, as other properties of a Subject are not safe to cache.

A Subject has a few basic methods for interacting with Schemas:

  • register(String) attempts to register a schema with the Subject, according to the validation rules of the Subject. The operation is idempotent -- the return value is the SchemaEntry corresponding to the schema String whether the schema existed before the operation or not.
  • registerIfLatest(String, SchemaEntry) attempts to register a schema with the Subject, according to the validation rules of the Subject. The operation succeeds only if the provided latest value is the current latest schema in the system, and returns null otherwise.
  • lookupById(String) looks up a schema by its id, and returns null if such a schema does not exist. Since the mapping from id to schema is immutable, this result is cacheable.
  • lookupBySchema(String) looks up an id for a schema, and returns null if no such schema exists. Since the mapping from id to schema is immutable, this result is cacheable.
  • allEntries() returns all the schema entries for the subject, ordered from most recent to oldest. The result is not cacheable, since additional entries may be added.

  • Constructor Summary
    protected Subject(String name)
              A Subject has a name.
     
    Method Summary
    abstract  Iterable<SchemaEntry> allEntries()
              List the ids of schemas registered with the given subject, ordered from most recent to oldest.
    static Subject cacheWith(Subject subject, SchemaEntryCache cache)
              Create a Subject that caches id to schema mappings using the SchemaEntryCache provided.
    abstract  SubjectConfig getConfig()
               
     String getName()
              Return the Name of the Subject.
    abstract  boolean integralKeys()
              Indicates whether the keys generated by this subject can be expected to parse as an integer.
    abstract  SchemaEntry latest()
              Lookup the most recently registered schema for the given subject.
    abstract  SchemaEntry lookupById(String id)
              Lookup the SchemaEntry for the given subject by id.
    abstract  SchemaEntry lookupBySchema(String schema)
              Lookup the SchemaEntry for the given schema.
    static Subject readOnly(Subject subject)
              Create a Subject that rejects modifications, throwing IllegalStateException if a modification is attempted
    abstract  SchemaEntry register(String schema)
              If the provided schema has already been registered in this subject, return the id.
    abstract  SchemaEntry registerIfLatest(String schema, SchemaEntry latest)
              Register the provided schema only if the current latest schema matches the provided latest entry.
     String toString()
               
    static Subject validatingSubject(Subject subject, ValidatorFactory factory)
              Create a Subject that validates schemas as configured.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    Constructor Detail

    Subject

    protected Subject(String name)
    A Subject has a name. The name must not be null or empty, and cannot contain whitespace. If the name contains whitespace an IllegalArgumentException is thrown.

    Method Detail

    getName

    public String getName()
    Return the Name of the Subject. A Subject name can not contain whitespace, and must not be empty or null.


    getConfig

    public abstract SubjectConfig getConfig()
    Returns:
    The SubjectConfig for this Subject

    integralKeys

    public abstract boolean integralKeys()
    Indicates whether the keys generated by this subject can be expected to parse as an integer. This delegates all the way through to the backing store and is not configurable through the Repository/Subject API, since implementations of the backing store are what determines how keys are generated; the contract otherwise is merely that they are Strings and unique per subject.

    Returns:
    a boolean indicating if the IDs for this Subject are integers

    register

    public abstract SchemaEntry register(String schema)
                                  throws SchemaValidationException
    If the provided schema has already been registered in this subject, return the id. If the provided schema has not been registered in this subject, register it and return its id. Idempotent -- If two users simultaneously register the same schema, they will both get the same SchemaEntry result and succeed.

    Parameters:
    schema - The schema to register
    Returns:
    The id of the schema
    Throws:
    SchemaValidationException - If the schema change is not valid according the validation rules of the subject

    registerIfLatest

    public abstract SchemaEntry registerIfLatest(String schema,
                                                 SchemaEntry latest)
                                          throws SchemaValidationException
    Register the provided schema only if the current latest schema matches the provided latest entry.

    Parameters:
    schema - The schema to register
    latest - the entry that must match the current actual latest value in order to register this schema.
    Returns:
    The id of the schema, or null if latest does not match.
    Throws:
    SchemaValidationException - If the schema change is not valid according the validation rules of the subject

    lookupBySchema

    public abstract SchemaEntry lookupBySchema(String schema)
    Lookup the SchemaEntry for the given schema. Since the mapping of schema to id is immutable, this result can be cached.

    Parameters:
    schema - The schema to look up
    Returns:
    The SchemaEntry of the schema or null if the schema is not registered

    lookupById

    public abstract SchemaEntry lookupById(String id)
    Lookup the SchemaEntry for the given subject by id. Since the mapping of schema to id is immutable the result can be cached.

    Parameters:
    id - the id of the schema to look up
    Returns:
    The SchemaEntry of the schema or null if no such schema is registered for the provided id

    latest

    public abstract SchemaEntry latest()
    Lookup the most recently registered schema for the given subject. This result is not cacheable, since the latest schema may change.

    Returns:
    The SchemaEntry or null if no schema is registered with this subject

    allEntries

    public abstract Iterable<SchemaEntry> allEntries()
    List the ids of schemas registered with the given subject, ordered from most recent to oldest. This result is not cacheable, since the SchemaEntry in the subject may grow over time.

    Returns:
    the SchemaEntry objects in this subject, ordered from most recent to oldest.

    toString

    public String toString()
    Overrides:
    toString in class Object
    Returns:
    The name of the Subject

    readOnly

    public static final Subject readOnly(Subject subject)
    Create a Subject that rejects modifications, throwing IllegalStateException if a modification is attempted


    validatingSubject

    public static Subject validatingSubject(Subject subject,
                                            ValidatorFactory factory)
    Create a Subject that validates schemas as configured.


    cacheWith

    public static Subject cacheWith(Subject subject,
                                    SchemaEntryCache cache)
    Create a Subject that caches id to schema mappings using the SchemaEntryCache provided.

    Parameters:
    subject - The Subject to wrap
    cache - The SchemaEntryCache to cache with
    Returns:
    returns a Subject instance that caches SchemaEntry values with the cache provided, if and only if both parameters are not null.
    If the provided subject is null, returns null. If the provided cache is null, returns the provided subject without wrapping it.


    Copyright © 2015. All rights reserved.