Catralitos
Catralitos

Reputation: 55

Java: An UUID generating Singleton

I'm working on a school project that's like a simpler Youtube clone. I know one of the attributes of the Video and Playlist classes is a String code, which I can generate in whatever way I want, as long as it's always unique. So I tougth to use UUID (tough I'm fairly new to it), and thing is a video and a playlist can have the same code, because they are in different libraries in my code, so in theory I'd want 2 UUID generators, one for videos, one for playlists, so the codes can overlap that way.

I thought to make a singleton so there's no more generators than those I need, and I can always assure I get unique codes. Here's what I got.

import java.util.UUID;

public class CodeGenerator {

    private static CodeGenerator singleInstance = null;

    private UUID videoGen;
    private UUID listGen;

    private CodeGenerator() {
    }

    public static CodeGenerator getInstance() {
        if (singleInstance == null) 
            singleInstance = new CodeGenerator(); 

        return singleInstance;
    }

    public String getCodeVideo() {
        return videoGen.randomUUID().toString();
    }

    public String getCodePlaylist() {
        return listGen.randomUUID().toString();
    }
}

But Eclipse is warning me in both of my gets that I should change this instance-reference to a static reference and I've never really been good at static stuff, and since I want to get rid of all these code warning for my project, can anyone tell me a fix that does what I'm looking for?

P.S I try to generate some codes in a Test class with the two different methods, and they always come out different and such as expected. The code is woring fine and I can see, but I'm scared down the line it might give me problems.

Upvotes: 1

Views: 1198

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503459

The problem isn't anything to do with your singleton - it's because you're calling UUID.randomUUID(), which is a static method, as if it were an instance method.

Here's a shorter example which I'd expect to give the same warning:

import java.util.UUID;

public class Test {
    public static void main(String[] args) {
        UUID ignored = null;
        UUID generated = ignored.randomUUID();
    }
}

Fundamentally, your fields are pointless - you're never assigning a value to them anyway.

It's not clear to me whether you expect getCodeVideo() to always return the same string for the lifetime of your process. If that is the case, you should change the code to something like:

import java.util.UUID;

public class CodeGenerator {

    private static CodeGenerator singleInstance = new CodeGenerator();

    private String video;
    private String codePlayList;

    private CodeGenerator() {
        video = UUID.randomUUID().toString();
        codePlayList = UUID.randomUUID().toString();
    }

    public static CodeGenerator getInstance() {
        return singleInstance;
    }

    public String getCodeVideo() {
        return video;
    }

    public String getCodePlayList() {
        return codePlayList;
    }
}

If you actually want to generate a new string each time you call the method, you don't need a singleton or anything like it:

import java.util.UUID;

public class CodeGenerator {
    public static String generateCode() {
        return UUID.randomUUID().toString();
    }

}

Upvotes: 4

Related Questions