KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_Cacheable


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_Cacheable
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derby.iapi.services.cache.*;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 /**
29
30 */

31 public abstract class T_Cacheable implements Cacheable {
32
33     protected boolean isDirty;
34
35     protected Thread JavaDoc owner;
36         
37     public T_Cacheable() {
38     }
39
40     /*
41     ** Cacheable methods
42     */

43
44     public Cacheable setIdentity(Object JavaDoc key) throws StandardException {
45         // we expect a key of type Object[]
46
if (!(key instanceof T_Key)) {
47             throw T_CacheException.invalidKey();
48         }
49
50         owner = null;
51
52         return null; // must be overriden by super class
53
}
54
55
56
57     public Cacheable createIdentity(Object JavaDoc key, Object JavaDoc createParameter) throws StandardException {
58
59         // we expect a key of type Object[]
60
if (!(key instanceof T_Key)) {
61             throw T_CacheException.invalidKey();
62         }
63
64         owner = (Thread JavaDoc) createParameter;
65
66         return null; // must be overriden by super class
67
}
68
69     /**
70         Returns true of the object is dirty. Will only be called when the object is unkept.
71
72         <BR> MT - thread safe
73
74     */

75     public boolean isDirty() {
76         synchronized (this) {
77             return isDirty;
78         }
79     }
80
81
82
83     public void clean(boolean forRemove) throws StandardException {
84         synchronized (this) {
85             isDirty = false;
86         }
87     }
88
89     /*
90     ** Implementation specific methods
91     */

92
93     protected Cacheable getCorrectObject(Object JavaDoc keyValue) throws StandardException {
94
95         Cacheable correctType;
96
97         if (keyValue instanceof Integer JavaDoc) {
98
99             correctType = new T_CachedInteger();
100         //} else if (keyValue instanceof String) {
101
//correctType = new T_CachedString();
102
} else {
103
104             throw T_CacheException.invalidKey();
105         }
106
107         return correctType;
108     }
109
110     protected boolean dummySet(T_Key tkey) throws StandardException {
111
112         // first wait
113
if (tkey.getWait() != 0) {
114             synchronized (this) {
115
116                 try {
117                     wait(tkey.getWait());
118                 } catch (InterruptedException JavaDoc ie) {
119                     // RESOLVE
120
}
121             }
122         }
123
124         if (!tkey.canFind())
125             return false;
126
127         if (tkey.raiseException())
128             throw T_CacheException.identityFail();
129
130         return true;
131     }
132
133     public void setDirty() {
134         synchronized (this) {
135             isDirty = true;
136         }
137     }
138
139     public boolean canRemove() {
140
141         synchronized (this) {
142             if (owner == null)
143                 owner = Thread.currentThread();
144
145             if (owner == Thread.currentThread())
146                 return true;
147             return false;
148         }
149     }
150 }
151
152
Popular Tags