Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package net.stanixgames.main;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. /**
  9.  * Unique ID Generator.
  10.  * How to use it:
  11.  * 1) Select section by using unique word ; If section not exist, it will be created.
  12.  * 2) Generate or Register custom ID ;
  13.  * Default Section is "main".
  14.  * Also, when you using init() method, only current section will be initialized!
  15.  * @author St1nger13
  16.  * date: 05.02.2016
  17.  */
  18. public class UniqueIDGenerator
  19. {
  20.     private static String sectionTargetID = "main" ;
  21.     private static Map<String, HashSet<Integer>> idBase = new HashMap<String, HashSet<Integer>>() ;
  22.  
  23.    
  24.     /**
  25.      * Initialization method
  26.      */
  27.     public static void init()
  28.     {
  29.         checkSectionID() ;
  30.        
  31.         HashSet<Integer> idSectionBase = idBase.get(sectionTargetID) ;
  32.         if(idSectionBase == null)
  33.             idSectionBase = new HashSet<Integer>() ;
  34.         else
  35.             idSectionBase.clear() ;
  36.         idBase.put(sectionTargetID, idSectionBase) ;
  37.     }
  38.    
  39.     public static void setSectionTarget(String sectionID)
  40.     {
  41.         if(sectionID != null && sectionID.length() > 0)
  42.             sectionTargetID = sectionID ;
  43.        
  44.         if(!idBase.containsKey(sectionID))
  45.             idBase.put(sectionID, new HashSet<Integer>()) ;
  46.     }
  47.    
  48.     public static String getSectionTargetID()
  49.     {
  50.         return sectionTargetID ;
  51.     }
  52.    
  53.     /**
  54.      * Method return unique ID which you can use for your
  55.      * NGuiComponent object
  56.      * @return - Registered unique ID
  57.      */
  58.     public static int generateID()
  59.     {
  60.         checkSectionID() ;
  61.        
  62.         int temp ;
  63.         boolean flag = false ;
  64.         HashSet<Integer> idSectionBase = null ;
  65.         do
  66.         {
  67.             idSectionBase = idBase.get(sectionTargetID) ;
  68.             temp = idSectionBase.size() +1 ;
  69.             flag = idSectionBase.add(temp) ;
  70.            
  71.             if(flag)
  72.                 idBase.put(sectionTargetID, idSectionBase) ;
  73.         }
  74.         while(!flag) ;
  75.        
  76.         return temp ;
  77.     }
  78.    
  79.     /**
  80.      * IF you want to use custon ID value for component, you have to
  81.      * register it here. If method returns FALSE, your ID is not unique
  82.      * and you cannot use it. If TRUE - all is okay, you can use ID for component
  83.      * @param id - Custom ID value
  84.      * @return Status of uniqueness
  85.      */
  86.     public static boolean registerID(int id)
  87.     {
  88.         checkSectionID() ;
  89.         return idBase.get(sectionTargetID).add(id) ;
  90.     }
  91.    
  92.     private static void checkSectionID()
  93.     {
  94.         if(sectionTargetID == null || sectionTargetID.length() < 1)
  95.             sectionTargetID = "main" ;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement