KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > cache > CachePointer


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.cache;
20
21 /**
22  * Class to provide a soft link to an object in a cache, i.e. an link to
23  * an object which may or may not actually reside in the cache but can
24  * be accessed through the cache. Useful for pointing to objects which
25  * logically don't change but their object representations do.
26  *
27  * @author Michael Bell
28  * @version $Revision: 1.1 $
29  *
30  */

31 public class CachePointer {
32     
33     /**
34      * Cache key of object.
35      */

36     protected Object JavaDoc m_cache_key;
37     
38     /**
39      * Cache which will contain the object.
40      */

41     protected AbstractCache m_cache;
42     
43
44     /**
45      * Constructs a cache pointer which does not reference any object.
46      */

47     public CachePointer() {
48         
49     }
50     
51     /**
52      * Constructs a cache pointer which references the object found in
53      * cache <code>cache</code> with cache key <code>key</code>.
54      *
55      * @param key the cache key
56      * @param cache the cache
57      */

58     public CachePointer(Object JavaDoc key,AbstractCache cache) {
59         m_cache_key=key;
60         m_cache = cache;
61     }
62
63     /**
64      * Returns the cache the pointer is referencing.
65      *
66      * @return the cache
67      */

68     public AbstractCache getCache() {
69         return m_cache;
70     }
71
72     /**
73      * Returns the cache key for the object the pointer is referencing.
74      *
75      * @return the cache key
76      */

77     public Object JavaDoc getKey() {
78         return m_cache_key;
79     }
80
81     /**
82      * Set the cache the pointer will reference.
83      *
84      * @param cache cache to be referenced
85      */

86     public void setCache(AbstractCache cache) {
87         this.m_cache = cache;
88     }
89
90     /**
91      * Sets the cache key for the object the pointer will reference.
92      *
93      * @param object the cache key corresponding to the cached object
94      */

95     public void setKey(Object JavaDoc object) {
96         m_cache_key = object;
97     }
98     
99     /**
100      * Returns the object this pointer references.
101      *
102      * @return the cached object this cache pointer references
103      * @throws CacheException
104      */

105     public Object JavaDoc getObject() throws CacheException {
106         return m_cache.getObject(m_cache_key);
107     }
108     
109     /* (non-Javadoc)
110      * @see java.lang.Object#equals(java.lang.Object)
111      */

112     public boolean equals(Object JavaDoc obj) {
113         boolean bEq = false;
114         
115         if(obj instanceof CachePointer) {
116             CachePointer ptr = (CachePointer) obj;
117             
118             if(this == ptr) {
119                 bEq = true;
120             } else if(ptr.getCache().equals(m_cache) == true && ptr.getKey().equals(m_cache_key) == true) {
121                 bEq = true;
122             }
123         }
124         return bEq;
125     }
126
127 }
128
Popular Tags