* @param <T>\r
*/\r
public class RefreshableThreadObject<T extends Creatable<T>> {\r
- private Map<Thread,T> objs;\r
- private long refreshed;\r
- private Constructor<T> cnst;\r
- \r
- /**\r
- * The passed in class <b>must</b> implement the constructor\r
- * <pre>\r
- * public MyClass(Env env) {\r
- * ...\r
- * }\r
- * </pre>\r
- * @param clss\r
- * @throws APIException\r
- */\r
- public RefreshableThreadObject(Class<T> clss) throws APIException {\r
- objs = new ConcurrentHashMap<Thread,T>();\r
- try {\r
- cnst = clss.getConstructor(new Class[]{Env.class} );\r
- } catch (Exception e) {\r
- throw new APIException(e);\r
- }\r
- }\r
- \r
- /**\r
- * Get the "T" class from the current thread\r
- * \r
- * @param env\r
- * @return T\r
- * @throws APIException\r
- */\r
- public T get(Env env) throws APIException {\r
- Thread t = Thread.currentThread();\r
- T obj = objs.get(t);\r
- if(obj==null || refreshed>obj.created()) {\r
- try {\r
- obj = cnst.newInstance(new Object[]{env});\r
- } catch (InvocationTargetException e) {\r
- throw new APIException(e.getTargetException());\r
- } catch (Exception e) {\r
- throw new APIException(e);\r
- }\r
- T destroyMe = objs.put(t,obj);\r
- if(destroyMe!=null) {\r
- destroyMe.destroy(env);\r
- }\r
- } \r
- return obj;\r
- }\r
- \r
- /**\r
- * Mark the timestamp of refreshed.\r
- * \r
- * @param env\r
- */\r
- public void refresh(Env env) {\r
- refreshed = System.currentTimeMillis();\r
- }\r
- \r
- /**\r
- * Remove the object from the Thread instances\r
- * @param env\r
- */\r
- public void remove(Env env) {\r
- T obj = objs.remove(Thread.currentThread());\r
- if(obj!=null)\r
- obj.destroy(env);\r
- }\r
+ private Map<Thread,T> objs;\r
+ private long refreshed;\r
+ private Constructor<T> cnst;\r
+ \r
+ /**\r
+ * The passed in class <b>must</b> implement the constructor\r
+ * <pre>\r
+ * public MyClass(Env env) {\r
+ * ...\r
+ * }\r
+ * </pre>\r
+ * @param clss\r
+ * @throws APIException\r
+ */\r
+ public RefreshableThreadObject(Class<T> clss) throws APIException {\r
+ objs = new ConcurrentHashMap<>();\r
+ try {\r
+ cnst = clss.getConstructor(new Class[]{Env.class} );\r
+ } catch (Exception e) {\r
+ throw new APIException(e);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Get the "T" class from the current thread\r
+ * \r
+ * @param env\r
+ * @return T\r
+ * @throws APIException\r
+ */\r
+ public T get(Env env) throws APIException {\r
+ Thread t = Thread.currentThread();\r
+ T obj = objs.get(t);\r
+ if (obj==null || refreshed>obj.created()) {\r
+ try {\r
+ obj = cnst.newInstance(new Object[]{env});\r
+ } catch (InvocationTargetException e) {\r
+ throw new APIException(e.getTargetException());\r
+ } catch (Exception e) {\r
+ throw new APIException(e);\r
+ }\r
+ T destroyMe = objs.put(t,obj);\r
+ if (destroyMe!=null) {\r
+ destroyMe.destroy(env);\r
+ }\r
+ } \r
+ return obj;\r
+ }\r
+ \r
+ /**\r
+ * Mark the timestamp of refreshed.\r
+ * \r
+ * @param env\r
+ */\r
+ public void refresh(Env env) {\r
+ refreshed = System.currentTimeMillis();\r
+ }\r
+ \r
+ /**\r
+ * Remove the object from the Thread instances\r
+ * @param env\r
+ */\r
+ public void remove(Env env) {\r
+ T obj = objs.remove(Thread.currentThread());\r
+ if (obj!=null)\r
+ obj.destroy(env);\r
+ }\r
}\r