01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.commons.betwixt.io.id;
18:
19: import java.util.Random;
20:
21: /** <p>Generates <code>ID</code>'s at random.
22: * The random number source is <code>java.util.Random</code>.</p>
23: *
24: * <p>Random <code>ID</code>'s are very useful if you're inserting
25: * elements created by <code>Betwixt</code> into a stream with existing
26: * elements.
27: * Using random <code>ID</code>'s should reduce the danger of collision
28: * with existing element <code>ID</code>'s.</p>
29: *
30: * <p>This class can generate positive-only ids (the default)
31: * or it can generate a mix of negative and postive ones.
32: * This behaviour can be set by {@link #setPositiveIds}
33: * or by using the {@link #RandomIDGenerator(boolean onlyPositiveIds)}
34: * constructor.</p>
35: *
36: * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
37: * @version $Revision: 438373 $
38: */
39: public final class RandomIDGenerator extends AbstractIDGenerator {
40:
41: /** Use simple java.util.Random as the source for our numbers */
42: private Random random = new Random();
43: /** Should only positive id's be generated? */
44: private boolean onlyPositiveIds = true;
45:
46: /**
47: * Constructor sets the <code>PositiveIds</code> property to <code>true</code>.
48: */
49: public RandomIDGenerator() {
50: }
51:
52: /**
53: * Constructor sets <code>PositiveIds</code> property.
54: *
55: * @param onlyPositiveIds set <code>PositiveIds</code> property to this value
56: */
57: public RandomIDGenerator(boolean onlyPositiveIds) {
58: setPositiveIds(onlyPositiveIds);
59: }
60:
61: /**
62: * <p>Generates a random <code>ID</code>.</p>
63: *
64: * <p>If the <code>PositiveIds</code> property is true,
65: * then this method will recursively call itself if the random
66: * <code>ID</code> is less than zero.</p>
67: *
68: * @return a random integer (converted to a string)
69: */
70: public String nextIdImpl() {
71: int next = random.nextInt();
72: if (onlyPositiveIds && next < 0) {
73: // it's negative and we're ignoring them so get another
74: return nextIdImpl();
75: }
76: return Integer.toString(next);
77: }
78:
79: /**
80: * Gets whether only positive <code>ID</code>'s should be generated
81: *
82: * @return whether only positive IDs should be generated
83: */
84: public boolean getPositiveIds() {
85: return onlyPositiveIds;
86: }
87:
88: /**
89: * Sets whether only positive <code>ID</code>'s should be generated
90: *
91: * @param onlyPositiveIds pass true if only positive IDs should be generated
92: */
93: public void setPositiveIds(boolean onlyPositiveIds) {
94: this.onlyPositiveIds = onlyPositiveIds;
95: }
96: }
|