001    package com.sptci.prevayler.query;
002    
003    import com.sptci.prevayler.PrevalentException;
004    import com.sptci.prevayler.PrevalentObject;
005    import com.sptci.prevayler.PrevalentSystem;
006    
007    import java.util.Collection;
008    import java.util.Date;
009    
010    /**
011     * The query used to retrieve prevalent objects of the specified type in
012     * the range specified.  This is used to retrieve paginated view of the
013     * prevalent objects stored in the prevalent system.
014     *
015     * @see com.sptci.prevayler.PrevalentManager#fetch(Class, long, long)
016     * <p>&copy; Copyright 2008 <a href='http://sptci.com/' target='_top'>Sans
017     *   Pareil Technologies, Inc.</a></p>
018     * @author Rakesh Vidyadharan 2008-05-27
019     * @version $Id: FetchRange.java 22 2008-11-24 19:04:25Z sptrakesh $
020     */
021    public class FetchRange<P extends PrevalentObject, S extends PrevalentSystem>
022        extends AbstractQuery<Collection<P>,S>
023    {
024      /** The type of prevalent objects to retrieve. */
025      private final Class cls;
026    
027      /**
028       * The starting index (inclusive) of the range from which to fetch
029       * objects.
030       */
031      private final long start;
032    
033      /**
034       * The ending index (exclusive) of the range to which to fetch
035       * objects.
036       */
037      private final long end;
038    
039      /**
040       * Create a new instance of the query for the specified parameters.
041       * @param cls The {@link #cls} value to use.
042       * @param start The {@link #start} value to use.
043       * @param end The {@link #end} value to use.
044       */
045      public FetchRange( final Class cls, final long start, final long end )
046      {
047        this.cls = cls;
048        this.start = start;
049        this.end = end;
050      }
051    
052      /**
053       * Execute the query on the prevalent system and return the prevalent
054       *  objects that fall in the specified range.
055       *
056       * @param system The prevalent system that is to be acted upon.
057       * @param timestamp The timestamp for the query.
058       * @return The collection of prevalent objects in the specified range.
059       *   Return an empty collection if no objects are in the range.
060       * @throws PrevalentException If errors are encountered while fetching
061       *   the required prevalent objects.
062       */
063      @SuppressWarnings( {"unchecked"} )
064      protected Collection<P> query( final S system, final Date timestamp )
065          throws PrevalentException
066      {
067        return (Collection<P>) system.fetch( cls, start, end );
068      }
069    }