1 package org.opentrafficsim.core.idgenerator;
2
3 import java.util.function.Supplier;
4
5 /**
6 * Supply names for any kind of object.
7 * <p>
8 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10 * </p>
11 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
12 */
13 public class IdSupplier implements Supplier<String>
14 {
15 /** All supplied names start with this string. */
16 private final String baseName;
17
18 /** Number of the last generated id. */
19 private long last = 0;
20
21 /**
22 * Construct a new supplier.
23 * @param baseName all generated names start with this string
24 */
25 public IdSupplier(final String baseName)
26 {
27 this.baseName = baseName;
28 }
29
30 /**
31 * Supply an id.
32 * @return the id
33 */
34 @Override
35 public final synchronized String get()
36 {
37 long number;
38 synchronized (this)
39 {
40 number = ++this.last;
41 }
42 return this.baseName + number;
43 }
44
45 @Override
46 public final String toString()
47 {
48 return "IdSupplier [baseName=" + this.baseName + ", last=" + this.last + "]";
49 }
50
51 }