KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > CommandIdGenerator


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
19 package sync4j.framework.protocol;
20
21 import sync4j.framework.protocol.IdGenerator;
22 import sync4j.framework.protocol.SimpleIdGenerator;
23 import sync4j.framework.core.CmdID;
24
25 /**
26  * This class creates command ids. Note that when a new CommandIdentifier is
27  * created, it is stored also in <i>currentId</i>, which is returned by
28  * <i>current()</i>. This is to avoid that <i>current()</i> creates a new
29  * CommandIdentifier object each time is called.
30  * <p>
31  * This class is thread-safe.
32  *
33  * @author Stefano Fornari @ funambol
34  * @version $Id: CommandIdGenerator.java,v 1.8 2005/03/02 20:57:38 harrie Exp $
35  */

36 public class CommandIdGenerator
37         implements java.io.Serializable JavaDoc {
38
39   // ---------------------------------------------------------- Private fields
40

41   /**
42    * The underlying IdGenerator object
43    */

44   private IdGenerator idGenerator = null;
45
46   /**
47    * The current identifier
48    */

49   private CmdID currentId = null;
50
51   // ------------------------------------------------------------ Constructors
52

53   /** Creates a new instance of CommandIdGenerator */
54   public CommandIdGenerator() {
55     this(new SimpleIdGenerator());
56
57   }
58
59   /**
60    * Creates a new instance of CommandIdGenerator given an IdGenerator
61    *
62    * @param idGenerator the idGenerator object - NOT NULL
63    */

64   public CommandIdGenerator(IdGenerator idGenerator) {
65     if (idGenerator == null) {
66       throw new NullPointerException JavaDoc("idGenerator cannot be null!");
67     }
68     this.idGenerator = idGenerator;
69   }
70
71   // ---------------------------------------------------------- Public methods
72

73   /**
74    * Returns a new generated command id.
75    *
76    * @return a new generated command id.
77    */

78   public synchronized CmdID next() {
79     return (currentId = new CmdID(idGenerator.next()));
80   }
81
82   /**
83    * Reset the Id counter
84    */

85   public synchronized void reset() {
86     idGenerator.reset();
87   }
88
89   /**
90    * Returns the last generated command id (which is the current command id).
91    *
92    * @return the last generated command id
93    */

94   public synchronized CmdID current() {
95     return currentId;
96   }
97 }
Popular Tags