I'm working on a school project that's like a simpler Youtube so to say. 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 tought 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?
Thanks in advance!
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.
Aucun commentaire:
Enregistrer un commentaire