Clover coverage report - Coverage timestamp: Sa Jul 7 2007 09:11:40 CEST FRAMES NO FRAMES |
file stats: | LOC: | 157 |  | Methods: | 10
| NCLOC: | 56 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
EntryUpdateState.java | 50% | 78,3% | 90% | 75,6% |

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 | 4565262 | public boolean isAwaitingUpdate() {
56 | 4565262 | 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 | 4397712 | public boolean isCancelled() {
66 | 4397712 | return state == UPDATE_CANCELLED;
67 |  | }
68 |  |
69 |  | /**
70 |  | * The update of the cache entry has been completed.
71 |  | */
72 | 38 | public boolean isComplete() {
73 | 38 | 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 | 167111 | public boolean isUpdating() {
81 | 167111 | 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 | 4510174 | public int cancelUpdate() {
90 | 4510174 | if (state != UPDATE_IN_PROGRESS) {
91 | 0 | throw new IllegalStateException("Cannot cancel cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS");
92 |  | }
93 |  |
94 | 4510174 | state = UPDATE_CANCELLED;
95 | 4510174 | 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 | 55045 | public int completeUpdate() {
104 | 55045 | if (state != UPDATE_IN_PROGRESS) {
105 | 0 | throw new IllegalStateException("Cannot complete cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS");
106 |  | }
107 |  |
108 | 55045 | state = UPDATE_COMPLETE;
109 | 55045 | 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 | 4565219 | public int startUpdate() {
118 | 4565219 | if ((state != NOT_YET_UPDATING) && (state != UPDATE_CANCELLED)) {
119 | 0 | throw new IllegalStateException("Cannot begin cache update - current state (" + state + ") is not NOT_YET_UPDATING or UPDATE_CANCELLED");
120 |  | }
121 |  |
122 | 4565219 | state = UPDATE_IN_PROGRESS;
123 | 4565219 | return incrementUsageCounter();
124 |  | }
125 |  |
126 |  | /**
127 |  | * Increments the usage counter by one
128 |  | * @return the counter value after the increment
129 |  | */
130 | 8925257 | public synchronized int incrementUsageCounter() {
131 | 8925257 | nbConcurrentUses++;
132 | 8925257 | return nbConcurrentUses;
133 |  | }
134 |  |
135 |  | /**
136 |  | * Gets the current usage counter value
137 |  | * @return a positive number.
138 |  | */
139 | 0 | public synchronized int getUsageCounter() {
140 | 0 | 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 | 9130481 | public synchronized int decrementUsageCounter() {
149 | 9130481 | if (nbConcurrentUses <=0) {
150 | 0 | throw new IllegalStateException("Cannot decrement usage counter, it is already equals to [" + nbConcurrentUses + "]");
151 |  | }
152 | 9130481 | nbConcurrentUses--;
153 | 9130481 | return nbConcurrentUses;
154 |  | }
155 |  |
156 |  |
157 |  | }