KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > base > EntryUpdateState


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.base;
6
7
8 /**
9  * Holds the state of a Cache Entry that is in the process of being (re)generated.
10  * This is not synchronized; the synchronization must be handled by the calling
11  * classes.
12  *
13  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
14  * @author Author: $
15  * @version Revision: $
16  */

17 public class EntryUpdateState {
18     /**
19      * The initial state when this object is first created
20      */

21     public static final int NOT_YET_UPDATING = -1;
22
23     /**
24      * Update in progress state
25      */

26     public static final int UPDATE_IN_PROGRESS = 0;
27
28     /**
29      * Update complete state
30      */

31     public static final int UPDATE_COMPLETE = 1;
32
33     /**
34      * Update cancelled state
35      */

36     public static final int UPDATE_CANCELLED = 2;
37
38     /**
39      * Current update state
40      */

41     int state = NOT_YET_UPDATING;
42     
43     /**
44      * A counter of the number of threads that are coordinated through this instance. When this counter gets to zero, then the reference to this
45      * instance may be released from the Cache instance.
46      * This is counter is protected by the EntryStateUpdate instance monitor.
47      */

48     private int nbConcurrentUses = 1;
49
50     /**
51      * This is the initial state when an instance this object is first created.
52      * It indicates that a cache entry needs updating, but no thread has claimed
53      * responsibility for updating it yet.
54      */

55     public boolean isAwaitingUpdate() {
56         return state == NOT_YET_UPDATING;
57     }
58
59     /**
60      * The thread that was responsible for updating the cache entry (ie, the thread
61      * that managed to grab the update lock) has decided to give up responsibility
62      * for performing the update. OSCache will notify any other threads that are
63      * waiting on the update so one of them can take over the responsibility.
64      */

65     public boolean isCancelled() {
66         return state == UPDATE_CANCELLED;
67     }
68
69     /**
70      * The update of the cache entry has been completed.
71      */

72     public boolean isComplete() {
73         return state == UPDATE_COMPLETE;
74     }
75
76     /**
77      * The cache entry is currently being generated by the thread that got hold of
78      * the update lock.
79      */

80     public boolean isUpdating() {
81         return state == UPDATE_IN_PROGRESS;
82     }
83
84     /**
85      * Updates the state to <code>UPDATE_CANCELLED</code>. This should <em>only<em>
86      * be called by the thread that managed to get the update lock.
87      * @return the counter value after the operation completed
88      */

89     public int cancelUpdate() {
90         if (state != UPDATE_IN_PROGRESS) {
91             throw new IllegalStateException JavaDoc("Cannot cancel cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS");
92         }
93
94         state = UPDATE_CANCELLED;
95         return decrementUsageCounter();
96     }
97
98     /**
99      * Updates the state to <code>UPDATE_COMPLETE</code>. This should <em>only</em>
100      * be called by the thread that managed to get the update lock.
101      * @return the counter value after the operation completed
102      */

103     public int completeUpdate() {
104         if (state != UPDATE_IN_PROGRESS) {
105             throw new IllegalStateException JavaDoc("Cannot complete cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS");
106         }
107
108         state = UPDATE_COMPLETE;
109         return decrementUsageCounter();
110     }
111
112     /**
113      * Attempt to change the state to <code>UPDATE_IN_PROGRESS</code>. Calls
114      * to this method must be synchronized on the EntryUpdateState instance.
115      * @return the counter value after the operation completed
116      */

117     public int startUpdate() {
118         if ((state != NOT_YET_UPDATING) && (state != UPDATE_CANCELLED)) {
119             throw new IllegalStateException JavaDoc("Cannot begin cache update - current state (" + state + ") is not NOT_YET_UPDATING or UPDATE_CANCELLED");
120         }
121
122         state = UPDATE_IN_PROGRESS;
123         return incrementUsageCounter();
124     }
125
126     /**
127      * Increments the usage counter by one
128      * @return the counter value after the increment
129      */

130     public synchronized int incrementUsageCounter() {
131         nbConcurrentUses++;
132         return nbConcurrentUses;
133     }
134     
135     /**
136      * Gets the current usage counter value
137      * @return a positive number.
138      */

139     public synchronized int getUsageCounter() {
140         return nbConcurrentUses;
141     }
142     
143     
144     /**
145      * Decrements the usage counter by one. This method may only be called when the usage number is greater than zero
146      * @return the counter value after the decrement
147      */

148     public synchronized int decrementUsageCounter() {
149         if (nbConcurrentUses <=0) {
150             throw new IllegalStateException JavaDoc("Cannot decrement usage counter, it is already equals to [" + nbConcurrentUses + "]");
151         }
152         nbConcurrentUses--;
153         return nbConcurrentUses;
154     }
155
156     
157 }
158
Popular Tags