1 32 package net.sf.retrotranslator.runtime.java.lang; 33 34 import net.sf.retrotranslator.runtime.impl.*; 35 import java.util.*; 36 37 40 @Advanced 41 public class ThreadLocal_ { 42 43 private static class Key {} 44 45 private static class Container extends ThreadLocal <Map<Key, Object >> { 46 protected Map<Key, Object > initialValue() { 47 return new WeakHashMap<Key, Object >(); 48 } 49 } 50 51 private static final Container container = new Container(); 52 53 private final Key key = new Key(); 54 55 public ThreadLocal_() { 56 } 57 58 public Object get() { 59 Map<Key, Object > map = container.get(); 60 Object value = map.get(key); 61 if (value == null && !map.containsKey(key)) { 62 map.put(key, value = initialValue()); 63 } 64 return value; 65 } 66 67 public void set(Object value) { 68 container.get().put(key, value); 69 } 70 71 public void remove() { 72 container.get().remove(key); 73 } 74 75 protected Object initialValue() { 76 return null; 77 } 78 79 } 80 | Popular Tags |