1 package org.opentrafficsim.base;
2
3 import java.util.function.Supplier;
4
5 import org.djutils.exceptions.Throw;
6
7 /**
8 * Id generator that produces A, B, C, ... X, Y, Z, AA, AB, AC, ... AX, AY, AZ, BA, BB, BC, etc., with possible prefix.
9 * <p>
10 * Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12 * </p>
13 * @author Wouter Schakel
14 */
15 public class AlphabeticIdGenerator implements Supplier<String>
16 {
17
18 /** Prefix. */
19 private final String prefix;
20
21 /** Id counter. */
22 private int counter = 1;
23
24 /**
25 * Constructor setting no prefix.
26 */
27 public AlphabeticIdGenerator()
28 {
29 this.prefix = "";
30 }
31
32 /**
33 * Constructor setting id prefix.
34 * @param prefix prefix
35 */
36 public AlphabeticIdGenerator(final String prefix)
37 {
38 Throw.whenNull(prefix, "prefix should not be null.");
39 this.prefix = prefix;
40 }
41
42 @Override
43 public String get()
44 {
45 StringBuilder sb = new StringBuilder();
46 int n = this.counter++;
47 while (n > 0)
48 {
49 n--; // make remainder 0..25 map to 'A'..'Z'
50 long rem = n % 26;
51 sb.append((char) ('A' + rem));
52 n /= 26;
53 }
54 return sb.reverse().insert(0, this.prefix).toString();
55 }
56
57 }