Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.stanixgames.main;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- /**
- * Unique ID Generator.
- * How to use it:
- * 1) Select section by using unique word ; If section not exist, it will be created.
- * 2) Generate or Register custom ID ;
- * Default Section is "main".
- * Also, when you using init() method, only current section will be initialized!
- * @author St1nger13
- * date: 05.02.2016
- */
- public class UniqueIDGenerator
- {
- private static String sectionTargetID = "main" ;
- private static Map<String, HashSet<Integer>> idBase = new HashMap<String, HashSet<Integer>>() ;
- /**
- * Initialization method
- */
- public static void init()
- {
- checkSectionID() ;
- HashSet<Integer> idSectionBase = idBase.get(sectionTargetID) ;
- if(idSectionBase == null)
- idSectionBase = new HashSet<Integer>() ;
- else
- idSectionBase.clear() ;
- idBase.put(sectionTargetID, idSectionBase) ;
- }
- public static void setSectionTarget(String sectionID)
- {
- if(sectionID != null && sectionID.length() > 0)
- sectionTargetID = sectionID ;
- if(!idBase.containsKey(sectionID))
- idBase.put(sectionID, new HashSet<Integer>()) ;
- }
- public static String getSectionTargetID()
- {
- return sectionTargetID ;
- }
- /**
- * Method return unique ID which you can use for your
- * NGuiComponent object
- * @return - Registered unique ID
- */
- public static int generateID()
- {
- checkSectionID() ;
- int temp ;
- boolean flag = false ;
- HashSet<Integer> idSectionBase = null ;
- do
- {
- idSectionBase = idBase.get(sectionTargetID) ;
- temp = idSectionBase.size() +1 ;
- flag = idSectionBase.add(temp) ;
- if(flag)
- idBase.put(sectionTargetID, idSectionBase) ;
- }
- while(!flag) ;
- return temp ;
- }
- /**
- * IF you want to use custon ID value for component, you have to
- * register it here. If method returns FALSE, your ID is not unique
- * and you cannot use it. If TRUE - all is okay, you can use ID for component
- * @param id - Custom ID value
- * @return Status of uniqueness
- */
- public static boolean registerID(int id)
- {
- checkSectionID() ;
- return idBase.get(sectionTargetID).add(id) ;
- }
- private static void checkSectionID()
- {
- if(sectionTargetID == null || sectionTargetID.length() < 1)
- sectionTargetID = "main" ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement