1 18 19 package sync4j.framework.protocol; 20 21 import sync4j.framework.protocol.IdGenerator; 22 23 32 public final class SimpleIdGenerator 33 implements IdGenerator, java.io.Serializable { 34 35 37 40 private long counter = 0; 41 42 public long getCounter() { 43 return counter; 44 } 45 46 49 public void setCounter(long counter) { 50 this.counter = counter; 51 } 52 53 56 private int increment = 1; 57 58 public int getIncrement() { 59 return increment; 60 } 61 62 65 public void setIncrement(int increment) { 66 this.increment = increment; 67 } 68 69 72 private long max = Long.MAX_VALUE; 73 74 79 public long getMax() { 80 return max; 81 } 82 83 88 public void setMax(long max) { 89 this.max = max; 90 } 91 92 94 100 public SimpleIdGenerator(long counter, int increment) { 101 this.counter = counter; 102 this.increment = increment; 103 } 104 105 110 public SimpleIdGenerator(int counter) { 111 this(counter, 1); 112 } 113 114 118 public SimpleIdGenerator() { 119 this(0, 1); 120 } 121 122 125 public void reset() { 126 this.counter = 0; 127 } 128 129 135 public synchronized String next() { 136 if (counter == max) { 137 reset(); 138 } 139 140 counter += increment; 141 142 return String.valueOf(counter); 143 } 144 145 150 public synchronized String current() { 151 return String.valueOf(counter); 152 } 153 } 154 | Popular Tags |