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

org.swiftboot.data.model.id.SnowflakeIdGenerator Maven / Gradle / Ivy

package org.swiftboot.data.model.id;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.swiftboot.data.model.entity.IdPersistable;

/**
 * ID generator with snowflake algorithm.
 * Default result is 32 bytes numeric string begin with snowflake ID and pad with random string,
 * set isPadTo32 = false if you want to keep original 19 bytes snowflake id.
 *
 * @author swiftech
 * @since 2.0.0
 */
public class SnowflakeIdGenerator implements IdGenerator {

    private final Sequence sequence = new Sequence(0);

    /**
     * Whether padding snowflake id with random numerics after.
     */
    private boolean isPadTo32 = true;

    public SnowflakeIdGenerator() {
    }

    public SnowflakeIdGenerator(boolean isPadTo32) {
        this.isPadTo32 = isPadTo32;
    }

    @Override
    public String generate(IdPersistable object) {
        String snowflakeId = String.valueOf(sequence.nextId());
        if (isPadTo32){
            snowflakeId = StringUtils.rightPad(snowflakeId, 32, RandomStringUtils.randomNumeric(32 - snowflakeId.length()));
        }
        return snowflakeId;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy