Alpha numeric sequence generator

huangapple go评论75阅读模式
英文:

Alpha numeric sequence generator

问题

我可以生成类似以下格式的字母数字ID:Rxt-AA0001、Rxt-AA0002、Rxt-AA0003、...、Rxt-AA9999、Rxt-AB0001、Rxt-AB0002等。

以下是用于生成这种ID的代码示例:

public class UserIdGenerator extends SequenceStyleGenerator {
 
    public static final String VALUE_PREFIX_PARAMETER = "valuePrefix";
    public static final String VALUE_PREFIX_DEFAULT = "";
    private String valuePrefix;
 
    public static final String NUMBER_FORMAT_PARAMETER = "numberFormat";
    public static final String NUMBER_FORMAT_DEFAULT = "%04d";
    private String numberFormat;
 
    @Override
    public Serializable generate(SharedSessionContractImplementor session,
            Object object) throws HibernateException {
        return valuePrefix + String.format(numberFormat, super.generate(session, object));
    }
 
    @Override
    public void configure(Type type, Properties params,
            ServiceRegistry serviceRegistry) throws MappingException {
        super.configure(LongType.INSTANCE, params, serviceRegistry);
        valuePrefix = ConfigurationHelper.getString(VALUE_PREFIX_PARAMETER,
                params, VALUE_PREFIX_DEFAULT);
        numberFormat = ConfigurationHelper.getString(NUMBER_FORMAT_PARAMETER,
                params, NUMBER_FORMAT_DEFAULT);
    }
 
}

然后,在您的用户模型中,可以使用此生成器生成ID,如下所示:

@Id
@GeneratedValue(generator = "user_seq_generator", strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "user_seq_generator", strategy = "user.util.UserIdGenerator", parameters = {
        @Parameter(name = UserIdGenerator.INCREMENT_PARAM, value = "1"),
        @Parameter(name = UserIdGenerator.VALUE_PREFIX_PARAMETER, value = "RxT-AA"),
        @Parameter(name = UserIdGenerator.NUMBER_FORMAT_PARAMETER, value = "%04d") })
@Column(name = "user_id")
private String Id;

这将生成以"RxT-AA"为前缀,后接四位数字的ID,数字部分会逐渐递增。

英文:

How can I generate Alphanumeric ID efficiently like <br>
Rxt-AA0001 <br>
Rxt-AA0002 <br>
Rxt-AA0003 <br>
.. <br>
.. <br>
Rxt-AA9999 <br>
Rxt-AB0001 <br>
Rxt-AB0002 <br>

With this

public class UserIdGenerator extends SequenceStyleGenerator {
 
    public static final String VALUE_PREFIX_PARAMETER = &quot;valuePrefix&quot;;
    public static final String VALUE_PREFIX_DEFAULT = &quot;&quot;;
    private String valuePrefix;
 
    public static final String NUMBER_FORMAT_PARAMETER = &quot;numberFormat&quot;;
    public static final String NUMBER_FORMAT_DEFAULT = &quot;%d&quot;;
    private String numberFormat;
 
    @Override
    public Serializable generate(SharedSessionContractImplementor session,
            Object object) throws HibernateException {
        return valuePrefix + String.format(numberFormat, super.generate(session, object));
    }
 
    @Override
    public void configure(Type type, Properties params,
            ServiceRegistry serviceRegistry) throws MappingException {
        super.configure(LongType.INSTANCE, params, serviceRegistry);
        valuePrefix = ConfigurationHelper.getString(VALUE_PREFIX_PARAMETER,
                params, VALUE_PREFIX_DEFAULT);
        numberFormat = ConfigurationHelper.getString(NUMBER_FORMAT_PARAMETER,
                params, NUMBER_FORMAT_DEFAULT);
    }
 
}

here is my user id in model

    @Id
    @GeneratedValue(generator = &quot;user_seq_generator&quot;, strategy = GenerationType.SEQUENCE)
    @GenericGenerator(name = &quot;user_seq_generator&quot;, strategy = &quot;user.util.UserIdGenerator&quot;, parameters = {
            @Parameter(name = UserIdGenerator.INCREMENT_PARAM, value = &quot;1&quot;),
            @Parameter(name = UserIdGenerator.VALUE_PREFIX_PARAMETER, value = &quot;RxT-AA&quot;),
            @Parameter(name = UserIdGenerator.NUMBER_FORMAT_PARAMETER, value = &quot;%04d&quot;) })
    @Column(name = &quot;user_id&quot;)
    private String Id;

I am able to generate Alpha numeric id but I am not sure how can I increment characters

答案1

得分: 1

通常在创建ID时,最佳实践是不要递增它们。这会使它们不安全。这个视频包含了一个很好的解释:https://www.youtube.com/watch?v=gocwRvLhDf8

Java有一个内置的UUID类用于生成ID。
这是它的javadoc:https://docs.oracle.com/javase/7/docs/api/java/util/UUID.html

英文:

Generally when creating IDs it is best practice to not increment them. This makes them insecure. This video contains a good explanation: https://www.youtube.com/watch?v=gocwRvLhDf8

Java has a build in UUID class for generating ids.
This is the javadoc for it: https://docs.oracle.com/javase/7/docs/api/java/util/UUID.html

答案2

得分: 1

以下是代码的翻译部分:

可以非常简单地完成 - 请参阅下面的类

但是根据目的使用UUID可能更有益@Someone所建议

import java.util.Arrays;

public class AlphaNumber {
    public static char[] ALPHA_NUMBER_CHARS = "0123ABC".toCharArray(); // 在这里添加您的字符
    private int[] number = new int[32]; // 在这里控制数字的最大大小
    private int size = 0; // 在这里我们控制数字的实际大小

    public AlphaNumber() {
        Arrays.fill(number, 0);
    }

    public String inc() {
        for (int i = 0; i <= size; i++) {
            number[i]++;
            if (number[i] < ALPHA_NUMBER_CHARS.length) {
                break;
            }
            number[i] = 0;
            size = Math.max(i + 1, size);
        }
        return toString();
    }

    public String toString() {
        StringBuilder r = new StringBuilder();
        for (int i = size; i >= 0; i--) {
            r.append(ALPHA_NUMBER_CHARS[number[i]]);
        }
        return new String(r);
    }

    public static void main(String[] args) {
        AlphaNumber a = new AlphaNumber();
        for (int i = 0; i < 100; i++) {
            System.out.println(a.inc());
        }
    }
}

请注意,我已经将代码中的HTML实体 &quot; 替换为正常的双引号。

英文:

It can be done quite simply - see the class below.

However depends on the purpose it might be more beneficial to use the UUID, as suggested by @Someone

import java.util.Arrays;
public class AlphaNumber {
public static char[] ALPHA_NUMBER_CHARS = &quot;0123ABC&quot;.toCharArray(); // add here your characters
private int[] number = new int[32]; // here you control the maximum size of the number
private int size = 0; // here we control the actual size of the number
public AlphaNumber() {
Arrays.fill(number,0);
}
public String inc() {
for(int i=0;i&lt;=size;i++) {
number[i]++;
if (number[i] &lt; ALPHA_NUMBER_CHARS.length) {
break;
}
number[i]=0;
size = Math.max(i+1,size);
}
return toString();
}
public String toString() {
StringBuilder r = new StringBuilder();
for (int i = size; i&gt;=0; i--) {
r.append(ALPHA_NUMBER_CHARS[number[i]]);
}
return new String(r);
}
public static void main(String[] args) {
AlphaNumber a = new AlphaNumber();
for (int i = 0; i &lt; 100; i++) {
System.out.println(a.inc());
}
}
}

huangapple
  • 本文由 发表于 2020年8月9日 06:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定