1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.id.serial;
19
20 import org.apache.commons.id.AbstractStringIdentifierGenerator;
21
22 import java.io.Serializable;
23
24 /**
25 * <p><code>NumericIdentifierGenerator</code> is an Identifier Generator
26 * that generates an incrementing number as a String object.</p>
27 *
28 * <p>If the <code>wrap</code> argument passed to the constructor is set to
29 * <code>true</code>, the sequence will wrap, returning negative values when
30 * {@link Long#MAX_VALUE} reached; otherwise an {@link IllegalStateException}
31 * will be thrown.</p>
32 *
33 * @author Commons-Id team
34 * @version $Id: NumericGenerator.java 480488 2006-11-29 08:57:26Z bayard $
35 */
36 public class NumericGenerator extends AbstractStringIdentifierGenerator implements Serializable {
37
38 /**
39 * <code>serialVersionUID</code> is the serializable UID for the binary version of the class.
40 */
41 private static final long serialVersionUID = 20060121L;
42
43 /** Should the counter wrap. */
44 private boolean wrapping;
45 /** The counter. */
46 private long count = 0;
47
48 /**
49 * <p>Constructor.</p>
50 *
51 * @param wrap should the factory wrap when it reaches the maximum
52 * long value (or throw an exception)
53 * @param initialValue the initial long value to start at
54 */
55 public NumericGenerator(boolean wrap, long initialValue) {
56 super();
57 this.wrapping = wrap;
58 this.count = initialValue;
59 }
60
61 /**
62 * Returns the maximum length (number or characters) for an identifier
63 * from this sequence.
64 *
65 * <p>The maximum value is determined from the length of the string
66 * representation of {@link Long#MAX_VALUE}.</p>
67 *
68 * @return the maximum identifier length
69 */
70 public long maxLength() {
71 return AbstractStringIdentifierGenerator.MAX_LONG_NUMERIC_VALUE_LENGTH;
72 }
73
74 /**
75 * <p>Returns the minimum length (number of characters) for an identifier
76 * from this sequence.</p>
77 *
78 * @return the minimum identifier length: <code>1</code>
79 */
80 public long minLength() {
81 return 1;
82 }
83
84 /**
85 * Getter for property wrap.
86 *
87 * @return <code>true</code> if this generator is set up to wrap.
88 *
89 */
90 public boolean isWrap() {
91 return wrapping;
92 }
93
94 /**
95 * Sets the wrap property.
96 *
97 * @param wrap value for the wrap property
98 *
99 */
100 public void setWrap(boolean wrap) {
101 this.wrapping = wrap;
102 }
103
104 public String nextStringIdentifier() {
105 long value = 0;
106 if (wrapping) {
107 synchronized (this) {
108 value = count++;
109 }
110 } else {
111 synchronized (this) {
112 if (count == Long.MAX_VALUE) {
113 throw new IllegalStateException
114 ("The maximum number of identifiers has been reached");
115 }
116 value = count++;
117 }
118 }
119 return Long.toString(value);
120 }
121 }