001    package com.sptci.prevayler;
002    
003    import java.io.Serializable;
004    import java.util.Collection;
005    import java.util.LinkedHashMap;
006    import java.util.LinkedHashSet;
007    import java.util.Map;
008    
009    /**
010     * A class used as the primary storage mechanism for persisting prevalent
011     * objects in the prevalent system.
012     *
013     * <p>&copy; Copyright 2008 <a href='http://sptci.com/' target='_top'>Sans Pareil Technologies, Inc.</a></p>
014     * @author Rakesh Vidyadharan 2008-05-22
015     * @version $Id: PrimaryStorage.java 4345 2008-06-30 21:22:03Z rakesh $
016     */
017    public class PrimaryStorage implements Serializable
018    {
019      private static final long serialVersionUID = 1L;
020    
021      /** The map used as the primary storage container. */
022      private Map<Object,PrevalentObject> storage =
023        new LinkedHashMap<Object,PrevalentObject>();
024    
025      /**
026       * Add the specified prevalent object to the primary storage.
027       *
028       * @param object The prevalent object to store in the system.
029       */
030      public void add( final PrevalentObject object )
031      {
032        if ( object == null ) return;
033        storage.put( object.getObjectId(), object );
034      }
035    
036      /**
037       * Remove the specified prevalent object from the primary storage.
038       *
039       * @param object The prevalent object to remove from the system.
040       */
041      public void remove( final PrevalentObject object )
042      {
043        if ( object == null ) return;
044        storage.remove( object.getObjectId() );
045      }
046    
047      /**
048       * Check to see if the specified object exists in storage.  Note that
049       * the checking only checks based upon the object id and not on a deep
050       * equality check.
051       *
052       * @param object The prevalent object to check for.
053       * @return Returns <code>true</code> if the object exists in the store.
054       */
055      public boolean isStored( final PrevalentObject object )
056      {
057        return ( object != null ) && storage.containsKey( object.getObjectId() );
058      }
059    
060      /**
061       * Return the total number of prevalent objects in {@link #storage}.
062       *
063       * @return The total number of objects stored.
064       */
065      public int size()
066      {
067        return storage.size();
068      }
069    
070      /**
071       * Return the prevalent object identified by its object id from the store.
072       *
073       * @param oid The object id to use to retrieve the prevalent object.
074       * @return Returs the prevalent object.  Returns <code>null</code> if no
075       *   matching object exists in the store.
076       */
077      public PrevalentObject get( final Object oid )
078      {
079        return storage.get( oid );
080      }
081    
082      /**
083       * Fetch the prevalent objects in the specified range of data.  This
084       * method supports display of paginated view of the prevalent objects.
085       * Note that the objects are returned in insertion order.
086       *
087       * @param start The starting index (inclusive) from which to fetch the
088       *   prevalent objects.
089       * @param end The ending index (exclusive) to which to fetch the
090       *   prevalent objects.
091       * @return The collection of prevalent objects.  Returns an empty
092       *   collection if there are no objects in the specified range.
093       */
094      public Collection<PrevalentObject> get( final long start, final long end )
095      {
096        final Collection<PrevalentObject> collection =
097            new LinkedHashSet<PrevalentObject>( (int) ( end - start ) );
098    
099        int index = 0;
100        for ( Map.Entry<Object,PrevalentObject> entry : storage.entrySet() )
101        {
102          if ( ( index >= start ) && ( index < end ) )
103          {
104            collection.add( entry.getValue() );
105          }
106    
107          if ( index >= end ) break;
108        }
109    
110        return collection;
111      }
112    }