Mercurial Hosting > luan
comparison src/luan/modules/ThreadLuan.java @ 1099:a5406f076726
improve Thread.global
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 05 Apr 2017 16:24:02 -0600 |
parents | 0d884377e923 |
children | 772d16c89056 |
comparison
equal
deleted
inserted
replaced
1098:bae624e455e2 | 1099:a5406f076726 |
---|---|
69 luan.registry().put(c,c); // prevent gc | 69 luan.registry().put(c,c); // prevent gc |
70 luan.onClose(c); | 70 luan.onClose(c); |
71 } | 71 } |
72 | 72 |
73 | 73 |
74 public static class StringMap { | 74 public static class GlobalMap { |
75 | 75 |
76 private static class Value { | 76 private static class Value { |
77 final long time = System.currentTimeMillis(); | 77 final long time = System.currentTimeMillis(); |
78 final String s; | 78 final Object v; |
79 | 79 |
80 Value(String s) { | 80 Value(Object v) { |
81 this.s = s; | 81 this.v = v; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 public long timeout = 60000L; // one minute | 85 public long timeout = 60000L; // one minute |
86 private Map<String,Value> map = new LinkedHashMap<String,Value>() { | 86 private Map<String,Value> map = new LinkedHashMap<String,Value>() { |
87 protected boolean removeEldestEntry(Map.Entry<String,Value> eldest) { | 87 protected boolean removeEldestEntry(Map.Entry<String,Value> eldest) { |
88 return eldest.getValue().time < System.currentTimeMillis() - timeout; | 88 return eldest.getValue().time < System.currentTimeMillis() - timeout; |
89 } | 89 } |
90 }; | 90 }; |
91 | 91 |
92 public synchronized String get(String key) { | 92 public synchronized Object get(String key) { |
93 Value val = map.get(key); | 93 Value val = map.get(key); |
94 return val==null ? null : val.s; | 94 return val==null ? null : val.v; |
95 } | 95 } |
96 | 96 |
97 public synchronized void set(String key,String s) { | 97 public synchronized Object put(String key,Object v) throws LuanException { |
98 map.put(key,new Value(s)); | 98 Value val; |
99 if( v == null ) { | |
100 val = map.remove(key); | |
101 } else { | |
102 if( !(v instanceof String || v instanceof Boolean || v instanceof Number) ) | |
103 throw new LuanException("can't assign type "+Luan.type(v)+" to Thread.global"); | |
104 val = map.put(key,new Value(v)); | |
105 } | |
106 return val==null ? null : val.v; | |
99 } | 107 } |
100 } | 108 } |
101 } | 109 } |