org.opentcs.kernel.workingset.PrefixedUlidObjectNameProvider Maven / Gradle / Ivy
/**
* Copyright (c) The openTCS Authors.
*
* This program is free software and subject to the MIT license. (For details,
* see the licensing information (LICENSE.txt) you should have received with
* this copy of the software.)
*/
package org.opentcs.kernel.workingset;
import static java.util.Objects.requireNonNull;
import de.huxhorn.sulky.ulid.ULID;
import java.util.Optional;
import org.opentcs.access.to.CreationTO;
import org.opentcs.components.kernel.ObjectNameProvider;
/**
* Provides names for objects based on ULIDs, prefixed with the name taken from a given
* {@link CreationTO}.
*/
public class PrefixedUlidObjectNameProvider
implements
ObjectNameProvider {
/**
* Generates ULIDs for us.
*/
private final ULID ulid = new ULID();
/**
* The previously generated ULID value.
*/
private ULID.Value previousUlid = ulid.nextValue();
/**
* Creates a new instance.
*/
public PrefixedUlidObjectNameProvider() {
}
@Override
public String apply(CreationTO to) {
requireNonNull(to, "to");
Optional newValue = ulid.nextStrictlyMonotonicValue(previousUlid);
while (newValue.isEmpty()) {
newValue = ulid.nextStrictlyMonotonicValue(previousUlid);
}
previousUlid = newValue.get();
return to.getName() + newValue.get().toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy