1 5 package com.opensymphony.oscache.base; 6 7 8 17 public class EntryUpdateState { 18 21 public static final int NOT_YET_UPDATING = -1; 22 23 26 public static final int UPDATE_IN_PROGRESS = 0; 27 28 31 public static final int UPDATE_COMPLETE = 1; 32 33 36 public static final int UPDATE_CANCELLED = 2; 37 38 41 int state = NOT_YET_UPDATING; 42 43 48 private int nbConcurrentUses = 1; 49 50 55 public boolean isAwaitingUpdate() { 56 return state == NOT_YET_UPDATING; 57 } 58 59 65 public boolean isCancelled() { 66 return state == UPDATE_CANCELLED; 67 } 68 69 72 public boolean isComplete() { 73 return state == UPDATE_COMPLETE; 74 } 75 76 80 public boolean isUpdating() { 81 return state == UPDATE_IN_PROGRESS; 82 } 83 84 89 public int cancelUpdate() { 90 if (state != UPDATE_IN_PROGRESS) { 91 throw new IllegalStateException ("Cannot cancel cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS"); 92 } 93 94 state = UPDATE_CANCELLED; 95 return decrementUsageCounter(); 96 } 97 98 103 public int completeUpdate() { 104 if (state != UPDATE_IN_PROGRESS) { 105 throw new IllegalStateException ("Cannot complete cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS"); 106 } 107 108 state = UPDATE_COMPLETE; 109 return decrementUsageCounter(); 110 } 111 112 117 public int startUpdate() { 118 if ((state != NOT_YET_UPDATING) && (state != UPDATE_CANCELLED)) { 119 throw new IllegalStateException ("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 130 public synchronized int incrementUsageCounter() { 131 nbConcurrentUses++; 132 return nbConcurrentUses; 133 } 134 135 139 public synchronized int getUsageCounter() { 140 return nbConcurrentUses; 141 } 142 143 144 148 public synchronized int decrementUsageCounter() { 149 if (nbConcurrentUses <=0) { 150 throw new IllegalStateException ("Cannot decrement usage counter, it is already equals to [" + nbConcurrentUses + "]"); 151 } 152 nbConcurrentUses--; 153 return nbConcurrentUses; 154 } 155 156 157 } 158 | Popular Tags |