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    * Created on 9-Dec-2004
6    */
7   package ca.uhn.cache.impl;
8   
9   import java.io.Serializable;
10  import java.text.DateFormat;
11  import java.text.ParseException;
12  import java.text.SimpleDateFormat;
13  import java.util.Date;
14  
15  import org.apache.commons.lang.builder.HashCodeBuilder;
16  
17  import ca.uhn.cache.IDataItem;
18  import ca.uhn.cache.IQuery;
19  import ca.uhn.cache.VolatilityEnum;
20  import ca.uhn.cache.internal.util.UTC;
21  
22  /***
23   * Represents an individual item stored by a cache.  It is only defined in terms 
24   * of a <code>ParamSpace</code> (see getProjection()).  
25   * 
26   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
27   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:37 $ by $Author: bryan_tripp $
28   */
29  public class DataItem extends CommonsLangObject implements IDataItem {
30      
31      //NonZeroOddNumber
32      private static final int INITIAL = 13;
33      //NonZeroOddNumber
34      private static final int MULTIPLIER = 17;
35  
36      private final String myId;
37      private final Serializable myValue;
38      private final IQuery myProjection;
39      private VolatilityEnum myVolatility;
40      
41      private Date myLastUpdate;
42          
43  
44      /***
45       * Constructor
46       * @param theId The item's unique identifier.
47       * @param theValue The domain specific item to wrap with this object
48       * @param theProjection The location of the item in the associated <code>IParamSpace</code>
49       * @param theVolatility The item's volatility (has an impact on the item's TTL in the cache). 
50       * @param theLastUpdate The time the item was last updated in the source system represented as a string,
51       *                      where the format is (MMM dd, yyyy h:mm a). If this is unknown, and set
52       *                      to null, this class will set it to the current time 
53       *
54       * @throws ParseException If theLastUpdate date/time could not be parsed. 
55       */
56      public DataItem( String theId, Serializable theValue, IQuery theProjection, VolatilityEnum theVolatility,
57                       String theLastUpdate)
58          throws ParseException
59      {
60          assert theId != null;
61          assert theValue != null;
62          assert theProjection != null;
63          assert theVolatility != null;
64  
65          myId = theId;
66          myValue = theValue;
67          myProjection = theProjection;
68          myVolatility = theVolatility;
69  
70          if (theLastUpdate != null){
71              DateFormat df = new SimpleDateFormat("MMM d, yyyy h:mm a");
72              myLastUpdate = df.parse( theLastUpdate );
73          }
74          else{
75              myLastUpdate = UTC.currentTime();
76          }
77          
78      }
79  
80      /***
81       * Constructor
82       * @param theId The item's unique identifier.
83       * @param theValue The domain specific item to wrap with this object
84       * @param theProjection The location of the item in the associated <code>IParamSpace</code>
85       * @param theVolatility The item's volatility (has an impact on the item's TTL in the cache). 
86       * @param theLastUpdate The time the item was last updated in the source system. If this is unknown, and set
87       *                      to null, this class will set it to the current time 
88       */
89      public DataItem( String theId, Serializable theValue, IQuery theProjection, VolatilityEnum theVolatility,
90                       Date theLastUpdate)
91      {
92          assert theId != null;
93          assert theValue != null;
94          assert theProjection != null;
95          assert theVolatility != null;
96  
97          myId = theId;
98          myValue = theValue;
99          myProjection = theProjection;
100         myVolatility = theVolatility;
101         if (theLastUpdate == null){
102             myLastUpdate = UTC.currentTime();
103         }
104         else{
105             myLastUpdate = theLastUpdate;
106         }
107         
108     }
109     
110     /***
111      * @return the location of the <code>IDataItem</code> in the (implicitly) associated 
112      *      <code>IParamSpace</code>  
113      */
114     public IQuery getProjection()
115     {
116         return myProjection;
117     }
118     
119     /***
120      * @return the item's volatility 
121      */
122     public VolatilityEnum getVolatility()
123     {
124         return myVolatility;
125     }
126     
127     /***
128      * @return the domain-specific value wrapped by this <code>IDataItem</code>
129      */
130     public Serializable getValue()
131     {
132         return myValue;
133     }
134 
135     /***
136      * {@inheritDoc}
137      */
138     public Date getLastUpdate() {
139         return myLastUpdate;
140     }
141 
142     /***
143      * {@inheritDoc}
144      */
145     public String getId() {
146         return myId;
147     }
148     
149     /***
150      * {@inheritDoc}
151      */
152     public void setLastUpdate( Date theLastUpdate ) {
153         myLastUpdate = theLastUpdate;
154     }
155     
156     /***
157      * {@inheritDoc}
158      */
159     public int hashCode() {
160         return new HashCodeBuilder()
161             .append( getId() )
162 //            .append( getValue() )
163 //            .append( getProjection() )
164 //            .append( getVolatility() )
165             //.append( getLastUpdate() )
166             .toHashCode();
167     }
168 
169 }
170