001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.betwixt.io.id;
018
019 import java.util.Random;
020
021 /** <p>Generates <code>ID</code>'s at random.
022 * The random number source is <code>java.util.Random</code>.</p>
023 *
024 * <p>Random <code>ID</code>'s are very useful if you're inserting
025 * elements created by <code>Betwixt</code> into a stream with existing
026 * elements.
027 * Using random <code>ID</code>'s should reduce the danger of collision
028 * with existing element <code>ID</code>'s.</p>
029 *
030 * <p>This class can generate positive-only ids (the default)
031 * or it can generate a mix of negative and postive ones.
032 * This behaviour can be set by {@link #setPositiveIds}
033 * or by using the {@link #RandomIDGenerator(boolean onlyPositiveIds)}
034 * constructor.</p>
035 *
036 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
037 * @version $Revision: 438373 $
038 */
039 public final class RandomIDGenerator extends AbstractIDGenerator {
040
041 /** Use simple java.util.Random as the source for our numbers */
042 private Random random = new Random();
043 /** Should only positive id's be generated? */
044 private boolean onlyPositiveIds = true;
045
046 /**
047 * Constructor sets the <code>PositiveIds</code> property to <code>true</code>.
048 */
049 public RandomIDGenerator() {}
050
051 /**
052 * Constructor sets <code>PositiveIds</code> property.
053 *
054 * @param onlyPositiveIds set <code>PositiveIds</code> property to this value
055 */
056 public RandomIDGenerator(boolean onlyPositiveIds) {
057 setPositiveIds(onlyPositiveIds);
058 }
059
060 /**
061 * <p>Generates a random <code>ID</code>.</p>
062 *
063 * <p>If the <code>PositiveIds</code> property is true,
064 * then this method will recursively call itself if the random
065 * <code>ID</code> is less than zero.</p>
066 *
067 * @return a random integer (converted to a string)
068 */
069 public String nextIdImpl() {
070 int next = random.nextInt();
071 if (onlyPositiveIds && next<0) {
072 // it's negative and we're ignoring them so get another
073 return nextIdImpl();
074 }
075 return Integer.toString(next);
076 }
077
078 /**
079 * Gets whether only positive <code>ID</code>'s should be generated
080 *
081 * @return whether only positive IDs should be generated
082 */
083 public boolean getPositiveIds() {
084 return onlyPositiveIds;
085 }
086
087 /**
088 * Sets whether only positive <code>ID</code>'s should be generated
089 *
090 * @param onlyPositiveIds pass true if only positive IDs should be generated
091 */
092 public void setPositiveIds(boolean onlyPositiveIds) {
093 this.onlyPositiveIds = onlyPositiveIds;
094 }
095 }