View Javadoc

1   /*
2    * Copyright 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * AbstractMethodDataSourceHelper.java
6    *
7    * Created on 21-Dec-2004 at 4:51:44 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.io.Serializable;
12  import java.util.Collection;
13  import java.util.Date;
14  import java.util.Iterator;
15  import java.util.Set;
16  
17  import org.apache.commons.collections.IteratorUtils;
18  
19  import ca.uhn.cache.IDataItem;
20  import ca.uhn.cache.IMethodDataSourceHelper;
21  import ca.uhn.cache.IQuery;
22  import ca.uhn.cache.IQueryResult;
23  import ca.uhn.cache.VolatilityEnum;
24  
25  
26  /***
27   * Base class for the <code>IMethodDataSourceHelper</code> implementors that are meant to be
28   * used with methods that return a <code>Collection</code>, <code>Set</code> or <b>array</b>.
29   * 
30   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
31   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:37 $ by $Author: bryan_tripp $
32   */
33  public abstract class AbstractMethodDataSourceHelper implements IMethodDataSourceHelper {
34      
35      /***
36       */
37      protected AbstractMethodDataSourceHelper() {
38      }
39  
40      /***
41       * {@inheritDoc}
42       */
43      public IQueryResult buildQueryResult( Object theObject ) {
44          IQueryResult retVal = new QueryResult();
45          
46          assert (theObject instanceof Collection) 
47               | (theObject instanceof Set)
48               | (theObject.getClass().isArray() );
49          
50          for (Iterator iter = IteratorUtils.getIterator( theObject ); iter.hasNext();) {
51              Object obj = (Object) iter.next();
52              
53              assert obj instanceof Serializable : "The individual objects returned by the invoked method " +
54                                                   "must be Serializable";
55              
56              IDataItem dataItem = buildDataItem( (Serializable) obj );
57              
58              retVal.add( dataItem );
59          }
60          
61          return retVal;
62      }
63      
64      /***
65       * Builds a data item from the provided object.
66       * 
67       * A <code>IDataItem</code> represents a discrete piece of data.
68       * 
69       * @param theObj The provided object.
70       * @return The data item.
71       */
72      public IDataItem buildDataItem( Serializable theObj ) {
73          return new DataItem( buildId( theObj ) , theObj, buildProjection(theObj), getVolatility( theObj ),
74                               getLastUpdateTime(theObj));
75      }
76  
77      /***
78       * Builds a unique identifier from the provided object. 
79       * 
80       * @param theObj The provided object.
81       * @return The object's unique identifier.
82       */
83      public abstract String buildId( Serializable theObj );
84  
85      /***
86       * Builds the query that represents the projection of the provided object in the
87       * parameter space. 
88       * 
89       * @param theObj The provided object.
90       * @return The query built.
91       */
92      public abstract IQuery buildProjection( Object theObj );
93  
94      /***
95       * @param theObj The provided object.
96       * @return The volatility of the provided object.
97       */
98      public abstract VolatilityEnum getVolatility( Serializable theObj );
99  
100     /***
101      * @param theObj The provided object
102      * @return The time this object was last updated. If this is unknown, it returns the current time
103      */
104     public abstract Date getLastUpdateTime( Serializable theObj );
105     
106 }