1 18 package sync4j.framework.server.session; 19 20 import java.util.HashMap ; 21 22 28 public class SyncSourceState { 29 30 32 public static final Byte STATE_IDLE = new Byte ((byte) 1); 33 public static final Byte STATE_CONFIGURED = new Byte ((byte) 2); 34 public static final Byte STATE_SYNCING = new Byte ((byte) 3); 35 public static final Byte STATE_COMMITTED = new Byte ((byte) 4); 36 public static final Byte STATE_ERROR = new Byte ((byte)-1); 37 38 public static final String STATE_NAME_IDLE = "IDLE" ; 39 public static final String STATE_NAME_CONFIGURED = "CONFIGURED"; 40 public static final String STATE_NAME_SYNCING = "SYNCING" ; 41 public static final String STATE_NAME_COMMITTED = "COMMITTED" ; 42 public static final String STATE_NAME_ERROR = "ERROR" ; 43 public static final String STATE_NAME_UNKNOWN = "UNKNOWN" ; 44 45 47 51 private HashMap states; 52 53 55 56 public SyncSourceState() { 57 reset(); 58 } 59 60 62 67 public void setState(final String uri, Byte newState) { 68 states.put(uri, newState); 69 } 70 71 81 public Byte getState(final String uri) 82 throws IllegalStateException { 83 Byte state = (Byte )states.get(uri); 84 85 if (state == null) { 86 throw new IllegalStateException ("No state associated to the SyncSource " + uri); 87 } 88 89 return state; 90 } 91 92 102 public int getStateAsInt(final String uri) 103 throws IllegalStateException { 104 Byte state = getState(uri); 105 106 return state.intValue(); 107 } 108 109 117 public String getStateName(final String uri) { 118 String name; 119 120 try { 121 Byte state = getState(uri); 122 123 if (STATE_IDLE.equals(state)) { 124 name = STATE_NAME_IDLE; 125 } else if (STATE_CONFIGURED.equals(state)) { 126 name = STATE_NAME_CONFIGURED; 127 } else if (STATE_SYNCING.equals(state)) { 128 name = STATE_NAME_SYNCING; 129 } else if (STATE_COMMITTED.equals(state)) { 130 name = STATE_NAME_COMMITTED; 131 } else { 132 name = STATE_NAME_UNKNOWN; 133 } 134 } catch (IllegalStateException e) { 135 name = STATE_NAME_UNKNOWN; 136 } 137 138 return name; 139 } 140 141 143 148 public void moveToIdle(String uri) { 149 setState(uri, STATE_IDLE); 150 } 151 152 157 public void moveToConfigured(String uri) { 158 setState(uri, STATE_CONFIGURED); 159 } 160 161 166 public void moveToSyncing(String uri) { 167 setState(uri, STATE_SYNCING); 168 } 169 170 175 public void moveToCommitted(String uri) { 176 setState(uri, STATE_COMMITTED); 177 } 178 179 184 public void moveToError(String uri) { 185 setState(uri, STATE_ERROR); 186 } 187 188 190 198 public boolean isIdle(String uri) { 199 return STATE_IDLE.equals(states.get(uri)); 200 } 201 202 210 public boolean isConfigured(String uri) { 211 return STATE_CONFIGURED.equals(states.get(uri)); 212 } 213 214 222 public boolean isSyncing(String uri) { 223 return STATE_SYNCING.equals(states.get(uri)); 224 } 225 226 234 public boolean isCommitted(String uri) { 235 return STATE_COMMITTED.equals(states.get(uri)); 236 } 237 238 246 public boolean isError(String uri) { 247 return STATE_ERROR.equals(states.get(uri)); 248 } 249 250 251 253 257 private void reset() { 258 this.states = new HashMap (); 259 } 260 261 } 262 | Popular Tags |