Class ContentState

Object
ContentState
Direct Known Subclasses:
JDBCState, MemoryState, VectorMosaicState, WFSContentState

public class ContentState extends Object
The state of an entry in a DataStore, maintained on a per-transaction basis. For information maintained on a typeName basis see ContentEntry.

Data Cache Synchronization Required

The default ContentState implementation maintains cached values on a per transaction basis:

Other types of state depend on the data format and must be handled by a subclass.

This class is a "data object" used to store values and is not thread safe. It is up to clients of this class to ensure that values are set in a thread-safe / synchronized manner. For example:

   
   ContentState state = ...;

   //get the count
   int count = state.getCount();
   if ( count == -1 ) {
     synchronized ( state ) {
       count = calculateCount();
       state.setCount( count );
     }
   }

Event Notification

The list of listeners interested in event notification as features are modified and edited is stored as part of ContentState. Each notification is considered to be broadcast from a specific FeatureSource. Since several ContentFeatureStores can be on the same transaction (and thus share a ContentState) the fire methods listed here require you pass in the FeatureSource making the change; this requires that your individual FeatureWriters keep a pointer to the ContentFeatureStore which created them.

You may also make use of ContentFeatureSource.canEvent() value of false allowing the base ContentFeatureStore class to take responsibility for sending event notifications.

Transaction Independence

The default ContentState implementation also supports the handling of ContentFeatureSource#canTransaction value of false. The implementation asks ContentState to store a Diff which is used to record any modifications made until commit is called.

Internally a Transaction.State is used to notify the implementation of {Transaction.commit() and Transaction.rollback().

Extension

This class may be extended if your implementaiton wishes to capture additional information per transaction. A database implementation using a JDBCContentState to store a JDBC Connection remains a good example.

Subclasses may extend (not override) the following methods:

  • flush() - remember to call super.flush()
  • close() - remember to call super.close()
Subclasses should also override copy() to ensure any additional state they are keeping is correctly accounted for.
Since:
2.6
Author:
Jody Garnett (LISASoft), Justin Deoliveira, The Open Planning Project
  • Field Details

    • tx

      protected Transaction tx
      Transaction the state works from.
    • entry

      protected ContentEntry entry
      entry maintaining the state
    • featureType

      protected SimpleFeatureType featureType
      cached feature type
    • count

      protected int count
      cached number of features
    • bounds

      protected ReferencedEnvelope bounds
      cached bounds of features
    • batchFeatureEvent

      protected BatchFeatureEvent batchFeatureEvent
      Even used for batch notification; used to collect the bounds and feature ids generated over the course of a transaction.
    • listeners

      protected List<FeatureListener> listeners
      observers
    • transactionState

      protected DiffTransactionState transactionState
      Callback used to issue batch feature events when commit/rollback issued on the transaction.
  • Constructor Details

    • ContentState

      public ContentState(ContentEntry entry)
      Creates a new state.
      Parameters:
      entry - The entry for the state.
    • ContentState

      protected ContentState(ContentState state)
      Creates a new state from a previous one.

      All state from the specified state is copied. Therefore subclasses extending this constructor should clone all mutable objects.

      Parameters:
      state - The existing state.
  • Method Details

    • getEntry

      public ContentEntry getEntry()
      The entry which maintains the state.
    • getTransaction

      public Transaction getTransaction()
      The transaction associated with the state.
    • setTransaction

      public void setTransaction(Transaction tx)
      Sets the transaction associated with the state.
    • getFeatureType

      public final SimpleFeatureType getFeatureType()
      The cached feature type.
    • setFeatureType

      public final void setFeatureType(SimpleFeatureType featureType)
      Sets the cached feature type.
    • getCount

      public final int getCount()
      The cached number of features.
    • setCount

      public final void setCount(int count)
      Sets the cached number of features.
    • getBounds

      public final ReferencedEnvelope getBounds()
      The cached spatial extent.
    • setBounds

      public final void setBounds(ReferencedEnvelope bounds)
      Sets the cached spatial extent.
    • addListener

      public final void addListener(FeatureListener listener)
      Adds a listener for collection events.
      Parameters:
      listener - The listener to add
    • removeListener

      public final void removeListener(FeatureListener listener)
      Removes a listener for collection events.
      Parameters:
      listener - The listener to remove
    • getBatchFeatureEvent

      public BatchFeatureEvent getBatchFeatureEvent()
    • hasListener

      public final boolean hasListener()
      Used to quickly test if any listeners are available.
    • fireFeatureUpdated

      public void fireFeatureUpdated(FeatureSource<?,?> source, Feature feature, ReferencedEnvelope before)
      Creates a FeatureEvent indicating that the provided feature has been changed.

      This method is provided to simplify event notification for implementors by constructing a FeatureEvent internally (and then only if listeners are interested). You may find it easier to construct your own FeatureEvent using {hasListener() to check if you need to fire events at all.

      Parameters:
      source - FeatureSource responsible for the change
      feature - The updated feature
      before - the bounds of the feature before the change
    • fireFeatureAdded

      public final void fireFeatureAdded(FeatureSource<?,?> source, Feature feature)
      Used to issue a Type.ADDED FeatureEvent indicating a new feature being created
    • fireFeatureRemoved

      public void fireFeatureRemoved(FeatureSource<?,?> source, Feature feature)
    • fireFeatureEvent

      public final void fireFeatureEvent(FeatureEvent event)
      Used to issue a single FeatureEvent.

      If this content state is used for Transaction.AUTO_COMMIT the notification will be passed to all interested parties.

      If not this event will be recored as part of a BatchFeatureEvent that will to be issued using issueBatchFeatureEvent()

    • fireBatchFeatureEvent

      public final void fireBatchFeatureEvent(boolean isCommit)
      Notifies all waiting listeners that a commit has been issued; this notification is also sent to our
    • flush

      public void flush()
      Clears cached state.

      This method does not affect any non-cached state. This method may be extended by subclasses, but not overiden.

    • close

      public void close()
      Clears all state.

      Any resources that the state holds onto (like a database connection) should be closed or disposes when this method is called. This method may be extended by subclasses, but not overiden.

    • copy

      public ContentState copy()
      Copies the state.

      Subclasses should override this method. Any mutable state objects should be cloned.

      Returns:
      A copy of the state.