title: How can you create a unique serial number for a given string?
body: <p>So I am trying to create a class that represents a fictional Licence Number for a given person. The Licence Number is constructed from the following:</p>
  
  <ul>
  <li>The initials of the person's forename and surname</li>
  <li>The year the licence was issued</li>
  <li>A random arbitrary serial number</li>
  </ul>
  
  <p>So for example, Maggie Smith whose licence was issued in 1990, might have a Licence Number of MS-1990-11, with 11 being the serial number. However, Mark Sanders might have had his licence issued the same year meaning the beginning of his licence may be MS-1990 also.</p>
  
  <p>So this is where the problem occurs. I need to make sure that the serial number for this person is not the same as Maggie's. So I'd have to check through any records that have the same initials and issue year, then generate a new unique serial number. Here's my code so far:</p>
  
  <pre><code>public class LicenceNumber {
  
  private final Name driverName;
  private final Date issueDate;
  private static final Map&lt;String, LicenceNumber&gt; LICENCENOS = new HashMap&lt;String, LicenceNumber&gt;();
  
  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;
  }
  }
  </code></pre>
  
  <p>And a snippet of how it may be instantiated:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>I've based it on some lecture notes that discuss how you can use factories for uniqueness. I'm also unsure if I should create a LicenceNumber using getInstance or by creating a new Object. Does anyone know a way I could check if a serial number for a given string, e.g. XX-XXXX already exists?</p>