KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > tools > 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.tools;
20
21 import sync4j.framework.tools.IdGenerator;
22 import sync4j.framework.tools.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  */

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

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

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

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

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

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

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

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

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

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