views:

325

answers:

4

hi

I want to generate unique id's everytime i call methode generateCustumerId(). The generated id must be 8 characters long or less than 8 characters. This requirement is necessary because I need to store it in a data file and schema is determined to be 8 characters long for this id.

Option 1 works fine. Instead of option 1, I want to use UUID. The problem is that UUID generates an id which has to many characters. Does someone know how to generate a unique id which is less then 99999999?

option 1

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class CustomerIdGenerator {

private static Set<String> customerIds = new HashSet<String>();
private static Random random = new Random();

// XXX: replace with java.util.UUID
public static String generateCustumerId() {

    String customerId = null;
    while (customerId == null || customerIds.contains(customerId)) {
        customerId = String.valueOf(random.nextInt(89999999) + 10000000);
    }
    customerIds.add(customerId);
    return customerId;
}

}

option2 generates an unique id which is too long

 public static String generateCustumerId() {
    String ownerId = UUID.randomUUID().toString();
    System.out.println("ownerId " + ownerId);
    return ownerId
}
+1  A: 

Not the best solution in the world, but how about chopping the uuid to the length? It will not be globally unique - just be aware that you will not get a globally unique identifier - but it might work for locally unique.

Shane C. Mason
if you chop, you will always have a chance that the id is not unique anymore
loudiyimo
anytime you restrict yourself to 8 chars - there is a chance that you will not be unique anymore. In fact, there is a chance the GUIDs are not unique - "the probability of the same number being generated twice is extremely small" - but not none.
Shane C. Mason
+2  A: 

Maybe the Commons Id project is of some use. The alphanumeric generator (and their other generators as well) takes a length argument. Of course, that really just supports a sequence (which implies that you'll have to know /determine the last highest identifier on application restarts).

Alternatively, you might try something like the CRC-32 checksum of the localtime or a randomly generated array of bytes (CRC32 is built into the Java Standard Library).

ig0774
+1  A: 

Does it need to be unique or random? If it only needs to be unique, you could load the highest value from the datastore when the application is launched (assuming only one app is writing to the data file). One you have your highest value:

public class IdGenerator{
  private String value;

  public IdGenerator( String initial value ){
    this.value = value;
  }

  public synchronized String nextValue(){
    value = incrementValue( value );
    return value;
  }

  private static String nextValue( String currentValue ){
    // Somehow increment the value.
    return incrementedValue;
  }

}

Depending on what characters you allow in you uuid this can be done various ways. I would read the last character of the current String, check if it's the last allowed character. Is so, increment the 7th character and so on. Else, increment the last character.

Manuel Darveau
+1  A: 

Keep a persistent counter which increments by one every time a new ID is requested.

If the data file is an SQL table you can always select MAX(id) + 1, and use that, but you need to be absolutely certain that you do not run into multitasking issues when two id's are needed at the same time (and they both have the same value from MAX(id)). Note that most databases have a native datatype designed for this purpose and for any non-trivial program you should use that facility.

Thorbjørn Ravn Andersen