Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
RandomIDGenerator |
|
| 1.4;1.4 |
1 | /* |
|
2 | * Copyright 2001-2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.betwixt.io.id; |
|
17 | ||
18 | import java.util.Random; |
|
19 | ||
20 | /** <p>Generates <code>ID</code>'s at random. |
|
21 | * The random number source is <code>java.util.Random</code>.</p> |
|
22 | * |
|
23 | * <p>Random <code>ID</code>'s are very useful if you're inserting |
|
24 | * elements created by <code>Betwixt</code> into a stream with existing |
|
25 | * elements. |
|
26 | * Using random <code>ID</code>'s should reduce the danger of collision |
|
27 | * with existing element <code>ID</code>'s.</p> |
|
28 | * |
|
29 | * <p>This class can generate positive-only ids (the default) |
|
30 | * or it can generate a mix of negative and postive ones. |
|
31 | * This behaviour can be set by {@link #setPositiveIds} |
|
32 | * or by using the {@link #RandomIDGenerator(boolean onlyPositiveIds)} |
|
33 | * constructor.</p> |
|
34 | * |
|
35 | * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a> |
|
36 | * @version $Revision: 190509 $ |
|
37 | */ |
|
38 | public final class RandomIDGenerator extends AbstractIDGenerator { |
|
39 | ||
40 | /** Use simple java.util.Random as the source for our numbers */ |
|
41 | 0 | private Random random = new Random(); |
42 | /** Should only positive id's be generated? */ |
|
43 | 0 | private boolean onlyPositiveIds = true; |
44 | ||
45 | /** |
|
46 | * Constructor sets the <code>PositiveIds</code> property to <code>true</code>. |
|
47 | */ |
|
48 | 0 | public RandomIDGenerator() {} |
49 | ||
50 | /** |
|
51 | * Constructor sets <code>PositiveIds</code> property. |
|
52 | * |
|
53 | * @param onlyPositiveIds set <code>PositiveIds</code> property to this value |
|
54 | */ |
|
55 | 0 | public RandomIDGenerator(boolean onlyPositiveIds) { |
56 | 0 | setPositiveIds(onlyPositiveIds); |
57 | 0 | } |
58 | ||
59 | /** |
|
60 | * <p>Generates a random <code>ID</code>.</p> |
|
61 | * |
|
62 | * <p>If the <code>PositiveIds</code> property is true, |
|
63 | * then this method will recursively call itself if the random |
|
64 | * <code>ID</code> is less than zero.</p> |
|
65 | * |
|
66 | * @return a random integer (converted to a string) |
|
67 | */ |
|
68 | public String nextIdImpl() { |
|
69 | 0 | int next = random.nextInt(); |
70 | 0 | if (onlyPositiveIds && next<0) { |
71 | // it's negative and we're ignoring them so get another |
|
72 | 0 | return nextIdImpl(); |
73 | } |
|
74 | 0 | return Integer.toString(next); |
75 | } |
|
76 | ||
77 | /** |
|
78 | * Gets whether only positive <code>ID</code>'s should be generated |
|
79 | * |
|
80 | * @return whether only positive IDs should be generated |
|
81 | */ |
|
82 | public boolean getPositiveIds() { |
|
83 | 0 | return onlyPositiveIds; |
84 | } |
|
85 | ||
86 | /** |
|
87 | * Sets whether only positive <code>ID</code>'s should be generated |
|
88 | * |
|
89 | * @param onlyPositiveIds pass true if only positive IDs should be generated |
|
90 | */ |
|
91 | public void setPositiveIds(boolean onlyPositiveIds) { |
|
92 | 0 | this.onlyPositiveIds = onlyPositiveIds; |
93 | 0 | } |
94 | } |