KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > session > SyncSourceState


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.framework.server.session;
19
20 import java.util.HashMap JavaDoc;
21
22 /**
23  * This class keeps the state of the SyncSources under sync. For more details
24  * about SyncSource state management see the architecture document.
25  *
26  * @author Stefano Fornari @ Funambol
27  */

28 public class SyncSourceState {
29     
30     // --------------------------------------------------------------- Constants
31

32     public static final Byte JavaDoc STATE_IDLE = new Byte JavaDoc((byte) 1);
33     public static final Byte JavaDoc STATE_CONFIGURED = new Byte JavaDoc((byte) 2);
34     public static final Byte JavaDoc STATE_SYNCING = new Byte JavaDoc((byte) 3);
35     public static final Byte JavaDoc STATE_COMMITTED = new Byte JavaDoc((byte) 4);
36     public static final Byte JavaDoc STATE_ERROR = new Byte JavaDoc((byte)-1);
37     
38     public static final String JavaDoc STATE_NAME_IDLE = "IDLE" ;
39     public static final String JavaDoc STATE_NAME_CONFIGURED = "CONFIGURED";
40     public static final String JavaDoc STATE_NAME_SYNCING = "SYNCING" ;
41     public static final String JavaDoc STATE_NAME_COMMITTED = "COMMITTED" ;
42     public static final String JavaDoc STATE_NAME_ERROR = "ERROR" ;
43     public static final String JavaDoc STATE_NAME_UNKNOWN = "UNKNOWN" ;
44     
45     // ------------------------------------------------------------ Private data
46

47     /**
48      * All states are stored
49      *
50      */

51     private HashMap JavaDoc states;
52     
53     // ------------------------------------------------------------ Constructors
54

55     /** Creates a new instance of SyncSourceState */
56     public SyncSourceState() {
57         reset();
58     }
59     
60     // ---------------------------------------------------------- Public methods
61

62     /**
63      * Sets the state of the given SyncSource
64      *
65      * @param uri the SyncSource URI
66      */

67     public void setState(final String JavaDoc uri, Byte JavaDoc newState) {
68         states.put(uri, newState);
69     }
70     
71     /**
72      * Returns the state of the given SyncSource. If the SyncSource is not in
73      * <i>states</i>, it throws an IllegalStateException.
74      *
75      * @param uri the SyncSource URI
76      *
77      * @return the SyncSource current state
78      *
79      * @throws IllegalStateException in the case uri is not found in <code>states</code>.
80      */

81     public Byte JavaDoc getState(final String JavaDoc uri)
82     throws IllegalStateException JavaDoc {
83         Byte JavaDoc state = (Byte JavaDoc)states.get(uri);
84         
85         if (state == null) {
86             throw new IllegalStateException JavaDoc("No state associated to the SyncSource " + uri);
87         }
88         
89         return state;
90     }
91     
92     /**
93      * Returns the state of the given SyncSource as int. It simply calls
94      * <code>getState(uri)</code> and converts the result to int.
95      *
96      * @param uri the SyncSource URI
97      *
98      * @return the SyncSource current state
99      *
100      * @throws IllegalStateException in the case uri is not found in <code>states</code>.
101      */

102     public int getStateAsInt(final String JavaDoc uri)
103     throws IllegalStateException JavaDoc {
104         Byte JavaDoc state = getState(uri);
105         
106         return state.intValue();
107     }
108     
109     /**
110      * Returns the state name of the given SyncSource. If the SyncSource is not
111      * in <i>states</i>, it throws an IllegalStateException.
112      *
113      * @param uri the SyncSource URI
114      *
115      * @return the SyncSource current state name
116      */

117     public String JavaDoc getStateName(final String JavaDoc uri) {
118         String JavaDoc name;
119         
120         try {
121             Byte JavaDoc 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 JavaDoc e) {
135             name = STATE_NAME_UNKNOWN;
136         }
137         
138         return name;
139     }
140     
141     // ------------------------------------------------ State transition methods
142

143     /**
144      * Move state to IDLE
145      *
146      * @param uri the SyncSource URI
147      */

148     public void moveToIdle(String JavaDoc uri) {
149         setState(uri, STATE_IDLE);
150     }
151     
152     /**
153      * Move state to CONFIGURED
154      *
155      * @param uri the SyncSource URI
156      */

157     public void moveToConfigured(String JavaDoc uri) {
158         setState(uri, STATE_CONFIGURED);
159     }
160     
161     /**
162      * Move state to SYNCING
163      *
164      * @param uri the SyncSource URI
165      */

166     public void moveToSyncing(String JavaDoc uri) {
167         setState(uri, STATE_SYNCING);
168     }
169     
170     /**
171      * Move state to COMMITTED
172      *
173      * @param uri the SyncSource URI
174      */

175     public void moveToCommitted(String JavaDoc uri) {
176         setState(uri, STATE_COMMITTED);
177     }
178     
179     /**
180      * Move state to ERROR
181      *
182      * @param uri the SyncSource URI
183      */

184     public void moveToError(String JavaDoc uri) {
185         setState(uri, STATE_ERROR);
186     }
187     
188     // -------------------------------------------------- Querying state methods
189

190     /**
191      * Is the given SyncSource in the IDLE state?
192      *
193      * @param uri the SyncSource URI
194      *
195      * @return true if the given SyncSource is in the IDLE state, false otherwise
196      *
197      */

198     public boolean isIdle(String JavaDoc uri) {
199         return STATE_IDLE.equals(states.get(uri));
200     }
201     
202     /**
203      * Is the given SyncSource in the CONFIGURED state?
204      *
205      * @param uri the SyncSource URI
206      *
207      * @return true if the given SyncSource is in the CONFIGURED state, false otherwise
208      *
209      */

210     public boolean isConfigured(String JavaDoc uri) {
211         return STATE_CONFIGURED.equals(states.get(uri));
212     }
213     
214     /**
215      * Is the given SyncSource in the SYNCING state?
216      *
217      * @param uri the SyncSource URI
218      *
219      * @return true if the given SyncSource is in the SYNCING state, false otherwise
220      *
221      */

222     public boolean isSyncing(String JavaDoc uri) {
223         return STATE_SYNCING.equals(states.get(uri));
224     }
225     
226     /**
227      * Is the given SyncSource in the COMMITTED state?
228      *
229      * @param uri the SyncSource URI
230      *
231      * @return true if the given SyncSource is in the COMMITTED state, false otherwise
232      *
233      */

234     public boolean isCommitted(String JavaDoc uri) {
235         return STATE_COMMITTED.equals(states.get(uri));
236     }
237     
238     /**
239      * Is the given SyncSource in the ERROR state?
240      *
241      * @param uri the SyncSource URI
242      *
243      * @return true if the given SyncSource is in the ERROR state, false otherwise
244      *
245      */

246     public boolean isError(String JavaDoc uri) {
247         return STATE_ERROR.equals(states.get(uri));
248     }
249     
250     
251     // --------------------------------------------------------- Private methods
252

253     /**
254      * Resets all states
255      *
256      */

257     private void reset() {
258         this.states = new HashMap JavaDoc();
259     }
260     
261 }
262
Popular Tags