001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import java.util.concurrent.atomic.AtomicLong;
005
006/**
007 * Generator of unique identifiers.
008 * @since 15820
009 */
010public final class UniqueIdGenerator {
011
012    private final AtomicLong idCounter = new AtomicLong(0);
013
014    /**
015     * Generates a new primitive unique id.
016     * @return new primitive unique (negative) id
017     * @since 16108 (made public)
018     */
019    public long generateUniqueId() {
020        return idCounter.decrementAndGet();
021    }
022
023    /**
024     * Returns the current primitive unique id.
025     * @return the current primitive unique (negative) id (last generated)
026     */
027    public long currentUniqueId() {
028        return idCounter.get();
029    }
030
031    /**
032     * Advances the current primitive unique id to skip a range of values.
033     * @param newId new unique id
034     * @throws IllegalArgumentException if newId is greater than current unique id
035     */
036    public void advanceUniqueId(long newId) {
037        if (newId > currentUniqueId()) {
038            throw new IllegalArgumentException("Cannot modify the id counter backwards");
039        }
040        idCounter.set(newId);
041    }
042}