All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.commons.UUIDUtils Maven / Gradle / Ivy

Go to download

Various utility classes. Except for very rare exceptions (annotation-based validation) this will not require any dependencies beyond the JRE

The newest version!
package com.aeontronix.commons;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.UUID;

public class UUIDUtils {
    private static UUIDFactory shared;

    /**
     * Return an UUID using a global UUIDFactory loaded through {@link ServiceLoader}
     *
     * @return UUID
     */
    public static UUID generate() {
        return getFactory().create();
    }

    public static UUIDFactory getFactory() {
        if (shared == null) {
            Iterator serviceLoader = ServiceLoader.load(UUIDFactory.class).iterator();
            if (serviceLoader.hasNext()) {
                UUIDFactory factory = serviceLoader.next();
                if (serviceLoader.hasNext()) {
                    throw new IllegalStateException("More than one UUIDFactory implementation found");
                }
                shared = factory;
            } else {
                shared = new UUIDFactoryRandomImpl();
            }
        }
        return shared;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy