KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > tools > SimpleIdGenerator


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
23 /**
24  * This class works as a simple id generator that after being initialized with a
25  * numeric value, increments the counter each time the next method is called.
26  * <p>
27  * SimpleIdGenerator is thread-safe
28  *
29  * @author Stefano Fornari @ funambol
30  */

31 public final class SimpleIdGenerator
32         implements IdGenerator, java.io.Serializable JavaDoc {
33
34   // -------------------------------------------------------------- Properties
35

36   /**
37    * The counter
38    */

39   private long counter = 0;
40
41   public long getCounter()
42   {
43     return counter;
44   }
45
46   /**
47    * Standard setter method
48    */

49   public void setCounter(long counter)
50   {
51     this.counter = counter;
52   }
53
54   /**
55    * The units the counter must be incremented each time next is called
56    */

57   private int increment = 1;
58
59   public int getIncrement()
60   {
61     return increment;
62   }
63
64   /**
65    * Standard setter method
66    */

67   public void setIncrement(int increment)
68   {
69     this.increment = increment;
70   }
71
72   // ------------------------------------------------------------ Constructors
73

74   /**
75    * Creates a new instance of SimpleIdGenerator
76    *
77    * @param counter the starting value
78    * @param increment the increment
79    */

80   public SimpleIdGenerator(long counter, int increment)
81   {
82     this.counter = counter;
83     this.increment = increment;
84   }
85
86   /**
87    * Like this(counter,1)
88    *
89    * @param counter the starting value
90    */

91   public SimpleIdGenerator(int counter)
92   {
93     this(counter, 1);
94   }
95
96   /**
97    * Reset the generator to 0.
98    */

99   public void reset()
100   {
101     this.counter = 0;
102   }
103
104   /**
105    * Like this(0,1)
106    */

107   public SimpleIdGenerator()
108   {
109     this(0, 1);
110   }
111
112   /**
113    * Returns the next value of the counter (incrementing the counter by the
114    * increment)
115    *
116    * @return the next generated value
117    */

118   public synchronized String JavaDoc next()
119   {
120     counter += increment;
121
122     return String.valueOf(counter);
123   }
124
125   /**
126    * Returns the last generated id (which is the current id).
127    *
128    * @return the last generated id
129    */

130   public synchronized String JavaDoc current()
131   {
132     return String.valueOf(counter);
133   }
134 }
Popular Tags