KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > 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.protocol;
20
21 import sync4j.framework.protocol.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  * @version $Id: SimpleIdGenerator.java,v 1.10 2005/04/26 16:13:02 luigiafassina Exp $
31  */

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

37     /**
38      * The counter
39      */

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

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

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

65     public void setIncrement(int increment) {
66         this.increment = increment;
67     }
68     
69     /**
70      * The maximum value allowed
71      */

72     private long max = Long.MAX_VALUE;
73     
74     /**
75      * Getter for max
76      *
77      * @return max
78      */

79     public long getMax() {
80         return max;
81     }
82     
83     /**
84      * Setter for max
85      *
86      * @param max new value
87      */

88     public void setMax(long max) {
89         this.max = max;
90     }
91
92     // ------------------------------------------------------------ Constructors
93

94     /**
95      * Creates a new instance of SimpleIdGenerator
96      *
97      * @param counter the starting value
98      * @param increment the increment
99      */

100     public SimpleIdGenerator(long counter, int increment) {
101         this.counter = counter;
102         this.increment = increment;
103     }
104     
105     /**
106      * Like this(counter,1)
107      *
108      * @param counter the starting value
109      */

110     public SimpleIdGenerator(int counter) {
111         this(counter, 1);
112     }
113     
114     /**
115      * Like this(0, 1)
116      *
117      */

118     public SimpleIdGenerator() {
119         this(0, 1);
120     }
121     
122     /**
123      * Reset the generator to 0.
124      */

125     public void reset() {
126         this.counter = 0;
127     }
128     
129     /**
130      * Returns the next value of the counter (incrementing the counter by the
131      * increment)
132      *
133      * @return the next generated value
134      */

135     public synchronized String JavaDoc next() {
136         if (counter == max) {
137             reset();
138         }
139         
140         counter += increment;
141         
142         return String.valueOf(counter);
143     }
144     
145     /**
146      * Returns the last generated id (which is the current id).
147      *
148      * @return the last generated id
149      */

150     public synchronized String JavaDoc current() {
151         return String.valueOf(counter);
152     }
153 }
154
Popular Tags