1 package org.opentrafficsim.core.idgenerator; 2 3 import java.io.Serializable; 4 5 /** 6 * Generate names for any kind of object. 7 * <p> 8 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 9 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>. 10 * <p> 11 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 4, 2016 <br> 12 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 13 */ 14 public class IdGenerator implements Serializable 15 { 16 /** */ 17 private static final long serialVersionUID = 1L; 18 19 /** All generated names start with this string. */ 20 private final String baseName; 21 22 /** Number of the last generated id. */ 23 private long last = 0; 24 25 /** 26 * Construct a new IdGenerator. 27 * @param baseName String; all generated names start with this string 28 */ 29 public IdGenerator(final String baseName) 30 { 31 this.baseName = baseName; 32 } 33 34 /** 35 * Generate an id. 36 * @return String; the generated id 37 */ 38 public final synchronized String nextId() 39 { 40 long number; 41 synchronized (this) 42 { 43 number = ++this.last; 44 } 45 return this.baseName + number; 46 } 47 48 /** {@inheritDoc} */ 49 @Override 50 public final String toString() 51 { 52 return "IdGenerator [baseName=" + this.baseName + ", last=" + this.last + "]"; 53 } 54 55 }