package org.javarosa.core.reference;


/**
 * Created by willpride on 2/20/18.
 */
public class ReferenceHandler {

    private static ReferenceManager staticManager;

    private static final ThreadLocal<ReferenceManager> threadLocalManager = new ThreadLocal<ReferenceManager>(){
        @Override
        protected ReferenceManager initialValue()
        {
            return new ReferenceManager();
        }
    };

    private static boolean useThreadLocal = false;

    public static void init(boolean force) {
        if (useThreadLocal) {
            if (threadLocalManager.get() == null || force) {
                threadLocalManager.set(new ReferenceManager());
            }
        } else {
            if (staticManager == null || force) {
                staticManager = new ReferenceManager();
            }
        }
    }

    public static void setUseThreadLocalStrategy(boolean useThreadLocal) {
        ReferenceHandler.useThreadLocal = useThreadLocal;
    }

    /**
     * @return Singleton accessor to the global
     * ReferenceManager.
     */
    public static ReferenceManager instance() {
        if (useThreadLocal) {
            return threadLocalManager.get();
        } else {
            return staticManager;
        }
    }
}

@@ -492,163 +492,187 @@
 oid 
-init(boolean force) %7B%0A        if (useThreadLocal) %7B%0A            if (threadLocalManager.get() == null %7C%7C force) %7B
+setUseThreadLocalStrategy(boolean useThreadLocal) %7B%0A        ReferenceHandler.useThreadLocal = useThreadLocal;
 %0A    
+%7D%0A%0A
     
+/**%0A
      
-   threadLocalManager.set(new
+* @return Singleton accessor to the global%0A     *
  Ref
@@ -688,41 +688,93 @@
 ager
-());
+.
 %0A     
+*/%0A
     
-   %7D%0A        %7D else
+public static ReferenceManager instance() %7B%0A        if (useThreadLocal)
  %7B%0A 
@@ -792,29 +792,40 @@
 if (
-static
+threadLocal
 Manager
+.get()
  == null
  %7C%7C 
@@ -824,17 +824,8 @@
 null
- %7C%7C force
 ) %7B%0A
@@ -844,24 +844,31 @@
     
-static
+threadLocal
 Manager
- = 
+.set(
 new 
@@ -877,32 +877,33 @@
 ferenceManager()
+)
 ;%0A            %7D%0A
@@ -914,365 +914,155 @@
     
-%7D%0A
     
-%7D%0A%0A    public static void setUseThreadLocalStrategy(boolean useThreadLocal) %7B%0A        ReferenceHandler.useThreadLocal = useThreadLocal;
+return threadLocalManager.get();%0A        %7D else %7B
 %0A    
-%7D%0A%0A
     
-/**%0A     * @return Singleton accessor to the global%0A     * ReferenceManager.%0A
+    if (staticManager == null) %7B%0A      
      
-*/%0A
     
-public
  static
- ReferenceManager instance() %7B%0A        if (useThreadLocal) %7B%0A            return threadLocal
+Manager = new Reference
 Manager
-.get
 ();%0A
@@ -1065,32 +1065,29 @@
 ();%0A        
-%7D else %7B
+    %7D
 %0A           

initialize ReferenceManager in instance()