1 16 package org.apache.commons.betwixt.io.id; 17 18 import java.util.Random ; 19 20 38 public final class RandomIDGenerator extends AbstractIDGenerator { 39 40 41 private Random random = new Random (); 42 43 private boolean onlyPositiveIds = true; 44 45 48 public RandomIDGenerator() {} 49 50 55 public RandomIDGenerator(boolean onlyPositiveIds) { 56 setPositiveIds(onlyPositiveIds); 57 } 58 59 68 public String nextIdImpl() { 69 int next = random.nextInt(); 70 if (onlyPositiveIds && next<0) { 71 return nextIdImpl(); 73 } 74 return Integer.toString(next); 75 } 76 77 82 public boolean getPositiveIds() { 83 return onlyPositiveIds; 84 } 85 86 91 public void setPositiveIds(boolean onlyPositiveIds) { 92 this.onlyPositiveIds = onlyPositiveIds; 93 } 94 } 95 | Popular Tags |