001package conexp.fx.core.util;
002
003import java.util.Map;
004import java.util.concurrent.ConcurrentHashMap;
005import java.util.concurrent.atomic.AtomicLong;
006
007/*
008 * #%L
009 * Concept Explorer FX
010 * %%
011 * Copyright (C) 2010 - 2023 Francesco Kriegel
012 * %%
013 * This program is free software: you can redistribute it and/or modify
014 * it under the terms of the GNU General Public License as
015 * published by the Free Software Foundation, either version 3 of the
016 * License, or (at your option) any later version.
017 * 
018 * This program is distributed in the hope that it will be useful,
019 * but WITHOUT ANY WARRANTY; without even the implied warranty of
020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 * GNU General Public License for more details.
022 * 
023 * You should have received a copy of the GNU General Public
024 * License along with this program.  If not, see
025 * <http://www.gnu.org/licenses/gpl-3.0.html>.
026 * #L%
027 */
028
029public final class IdGenerator {
030
031  private static final IdGenerator             defaultKey = new IdGenerator();
032  private static final Map<Object, AtomicLong> nextIds    = new ConcurrentHashMap<Object, AtomicLong>();
033
034  private IdGenerator() {}
035
036  public static final long getNextId() {
037    return getNextId(defaultKey);
038  }
039
040  public static final long getNextId(final Object key) {
041    synchronized (key) {
042      return nextIds.computeIfAbsent(key, __ -> new AtomicLong(0l)).getAndIncrement();
043    }
044  }
045}