如何为给定字符串创建唯一的序列号?

时间:2016-05-11 17:03:45

标签: java unique factory

所以我正在尝试创建一个代表给定人员的虚构许可证号的类。许可证编号由以下内容构成:

  • 该人的姓氏和姓氏的缩写
  • 颁发许可证的那一年
  • 随机任意序列号

因此,例如,Maggie Smith的许可证是在1990年颁发的,许可证编号可能是MS-1990-11,其中11是序列号。但是,马克桑德斯可能在同年签发了他的执照,这意味着他的执照开始也可能是MS-1990。

所以这就是问题发生的地方。我需要确保此人的序列号与Maggie的序列号不同。因此,我必须检查具有相同首字母和发行年份的任何记录,然后生成新的唯一序列号。到目前为止,这是我的代码:

public class LicenceNumber {

private final Name driverName;
private final Date issueDate;
private static final Map<String, LicenceNumber> LICENCENOS = new HashMap<String, LicenceNumber>();

public LicenceNumber(Name driverName, Date issueDate){

    this.driverName = driverName;
    this.issueDate = issueDate;
}

public static LicenceNumber getInstance(Name driverName, Date issueDate){
    Calendar tempCal = Calendar.getInstance();
    tempCal.setTime(issueDate);
    String issueYear = String.valueOf(tempCal.get(Calendar.YEAR));
    int serialNo = 1;
    String k = driverName.getForename().substring(0, 1) + driverName.getSurname().substring(0, 1) + "-" + issueYear + "-" + serialNo;

    if(!LICENCENOS.containsKey(k)){
        LICENCENOS.put(k, new LicenceNumber(driverName,issueDate));
    }

    return LICENCENOS.get(k);
}

public boolean isUnique(){
    return true;
}

public Name getDriverName() {
    return driverName;
}

public Date getIssueDate() {
    return issueDate;
}
}

以及如何实例化它的片段:

public final class DrivingLicence {

private final Name driverName;
private final Date driverDOB;
private final Date issueDate;
private final LicenceNumber licenceNo;
private final boolean isFull;

public DrivingLicence(Name driverName, Date driverDOB, Date issueDate, boolean isFull){
    //TO-DO validate inputs
    this.driverName = driverName;
    this.driverDOB = driverDOB;
    this.issueDate = issueDate;
    this.licenceNo = LicenceNumber.getInstance(driverName, issueDate);
    //this.licenceNo = new LicenceNumber(driverName, issueDate);//instantiate a licence number using the driverName and dateOfIssue
    this.isFull = isFull;
}
}

我基于一些讨论如何使用工厂实现独特性的讲义。我还不确定是否应该使用getInstance创建LicenceNumber或创建新的Object。有没有人知道我可以检查给定字符串的序列号,例如XX-XXXX已经存在?

1 个答案:

答案 0 :(得分:3)

这是一种为程序的持续时间创建增量数字的方法。由于没有后备数据库,程序的每次运行都将重置。

通过使用AtomicInteger来确保唯一性。我使用ConcurrentMap来利用线程安全性以及.putIfAbsent方法。但是,它可以很容易地转换为使用标准Map。我也只使用了String,但更好的方法是使用真正的域对象。它足以处理OP的问题并用于说明目的。

// a Map for holding the sequencing
private ConcurrentMap<String, AtomicInteger> _sequence = 
        new ConcurrentHashMap<>();

/**
 * Returns a unique, incrementing sequence, formatted to
 * 0 prefixed, 3 places, based upon the User's initials
 * and the registration year
 */
public String getSequence(String initials, String year)
{
    String key = makePrefix(initials, year);
    AtomicInteger chk = new AtomicInteger(0);
    AtomicInteger ai = _sequence.putIfAbsent(key, chk);
    if (ai == null) {
        ai = chk;
    }

    int val = ai.incrementAndGet();

    String fmt = String.format("%03d", val);

    return fmt;
}

/**
 * A helper method to make the prefix, which is the
 * concatintion of the initials, a "-", and a year.
 */
private String makePrefix (String initials, String year)
{
    return initials + "-" + year;
}

测试示例:

public static void main(String[] args)
{
    LicensePlate_37169055 lp = new LicensePlate_37169055();
    System.out.println("ko, 1999: " + lp.getSequence("ko", "1999"));
    System.out.println("ac, 1999: " + lp.getSequence("ac", "1999"));
    System.out.println("ko, 1999: " + lp.getSequence("ko", "1999"));
    System.out.println("ac, 1999: " + lp.getSequence("ac", "1999"));
    System.out.println("ms, 1999: " + lp.getSequence("ms", "1999"));
    System.out.println("ko, 2001: " + lp.getSequence("ko", "2001"));

}

示例输出:

  

ko,1999:001
  ac,1999:001
  ko,1999:002
  ac,1999:002
  ms,1999:001
  ko,2001:001

要整合到OP的代码中,以下修改是暗示性的:

public static LicenceNumber getInstance(Name driverName, Date issueDate){
  Calendar tempCal = Calendar.getInstance();
  tempCal.setTime(issueDate);
  String issueYear = String.valueOf(tempCal.get(Calendar.YEAR));

  // ** get the initials; I would actually move this functionality to be
  //   a method on the Name class
  String initials = driverName.getForename().substring(0, 1) + driverName.getSurname().substring(0, 1);

  // get the unique serial number
  String serial = getSequence(initials, issueYear);

  // make the full licenseplate String
  String k = makePrefix(initials, issueYear) + "-" + serial;

if(!LICENCENOS.containsKey(k)){
    LICENCENOS.put(k, new LicenceNumber(driverName,issueDate));
}

return LICENCENOS.get(k);

}