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. import java.util.Random;
  18. /** <p>Generates <code>ID</code>'s at random.
  19. * The random number source is <code>java.util.Random</code>.</p>
  20. *
  21. * <p>Random <code>ID</code>'s are very useful if you're inserting
  22. * elements created by <code>Betwixt</code> into a stream with existing
  23. * elements.
  24. * Using random <code>ID</code>'s should reduce the danger of collision
  25. * with existing element <code>ID</code>'s.</p>
  26. *
  27. * <p>This class can generate positive-only ids (the default)
  28. * or it can generate a mix of negative and postive ones.
  29. * This behaviour can be set by {@link #setPositiveIds}
  30. * or by using the {@link #RandomIDGenerator(boolean onlyPositiveIds)}
  31. * constructor.</p>
  32. *
  33. * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
  34. * @version $Revision: 1.7 $
  35. */
  36. public final class RandomIDGenerator extends AbstractIDGenerator {
  37. /** Use simple java.util.Random as the source for our numbers */
  38. private Random random = new Random();
  39. /** Should only positive id's be generated? */
  40. private boolean onlyPositiveIds = true;
  41. /**
  42. * Constructor sets the <code>PositiveIds</code> property to <code>true</code>.
  43. */
  44. public RandomIDGenerator() {}
  45. /**
  46. * Constructor sets <code>PositiveIds</code> property.
  47. *
  48. * @param onlyPositiveIds set <code>PositiveIds</code> property to this value
  49. */
  50. public RandomIDGenerator(boolean onlyPositiveIds) {
  51. setPositiveIds(onlyPositiveIds);
  52. }
  53. /**
  54. * <p>Generates a random <code>ID</code><p>
  55. *
  56. * <p>If the <code>PositiveIds</code> property is true,
  57. * then this method will recursively call itself if the random
  58. * <code>ID</code> is less than zero.</p>
  59. *
  60. * @return a random integer (converted to a string)
  61. */
  62. public String nextIdImpl() {
  63. int next = random.nextInt();
  64. if (onlyPositiveIds && next<0) {
  65. // it's negative and we're ignoring them so get another
  66. return nextIdImpl();
  67. }
  68. return Integer.toString(next);
  69. }
  70. /**
  71. * Gets whether only positive <code>ID</code>'s should be generated
  72. *
  73. * @return whether only positive IDs should be generated
  74. */
  75. public boolean getPositiveIds() {
  76. return onlyPositiveIds;
  77. }
  78. /**
  79. * Sets whether only positive <code>ID</code>'s should be generated
  80. *
  81. * @param onlyPositiveIds pass true if only positive IDs should be generated
  82. */
  83. public void setPositiveIds(boolean onlyPositiveIds) {
  84. this.onlyPositiveIds = onlyPositiveIds;
  85. }
  86. }