One of a kind – Generating UUID with EMF

Eclipse EMF provides the functionality to create universally unique identifier as described in A Universally Unique IDentifier (UUID) URN Namespace

To use this functionality just add a dependency to “org.eclipse.emf.ecore” to your plugin and you can start using it.

package de.vogella.emf.uuid;

import org.eclipse.emf.ecore.util.EcoreUtil;

public class MakeUniqueIds {
	public static void main(String[] args) {
		for (int i=0; i<100; i++){
			System.out.println(EcoreUtil.generateUUID());
		}
	}
}

For example you may get “_Fj9QPC0XEd-zxLXA4jfyXA” as a result.

Eclipse e4 is currently using this functionality to generated unique model Id's. The responsible class for this is "org.eclipse.e4.workbench.ui.internal.E4XMIResource".

Thanks to Remy Suen for providing this info on the e4 mailing list.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse. Bookmark the permalink.

7 Responses to One of a kind – Generating UUID with EMF

  1. Bryan Hunt says:

    You can also use java.util.UUID.randomUUID() to achive the same result without EMF.

  2. Lars Vogel says:

    @Bryan: Thanks for the info!

  3. Dann Martens says:

    I am a bit puzzled by this claim. Being familiar with this RFC, I know it details different versions and variants, which can be particularly relevant for the context the UUID is used in.

    The UUID utility class from the java.util library supports several of these. Why exactly has this wheel been reinvented?

    Ironically, as I consider a project like EMF quite bloated, should I now conclude that part of the cause for that is that standard Java classes have been rewritten?

  4. Lars Vogel says:

    @Dann Sorry, but I don’t know why EMF has re-implemented this. I’m also not aware of the difference between both implementations. If you want to know I suggest to check on the EMF forum.

  5. Dann Martens says:

    @Lars No worries! As a well-known critic of the role of EMF in e4, this type of thing is just ‘Gefundenes Fressen’. I am perfectly happy to see EMF continue its MDD quest, but not as a core part of the framework. I feel that EMF lacks the maturity and the broad appeal it needs to warrant adoption on this level by this community.

    Because EMF is being pushed down our throats, I now have to spend time on criticizing, instead of on helping the Eclipse community to work for everyone. That’s pretty sad.

    EMF wants to be in the driver seat? You’d better buckle up, then :)

  6. I think EMF might have introduced its own variant because EMF did not originally target 1.5. I myself did not know of the java.util.UUID class until I read Bryan’s comment since I’ve primarily been working with 1.4 until e4 came along. :)

  7. Tom Schindl says:

    For those interested I’ve written a small Eclipse Plugin to make generating UUIDs as simple as possible when you are inside your IDE. For more details see: http://tomsondev.bestsolution.at/2010/10/20/uuid-generator-plugin/

Comments are closed.