comparison core/src/luan/LuanState.java @ 527:ef0336efe33c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 03:29:04 -0600
parents 8a217fe5b4f3
children f1601a4ce1aa
comparison
equal deleted inserted replaced
526:f24d824e9fe5 527:ef0336efe33c
1 package luan; 1 package luan;
2 2
3 import java.io.Closeable;
4 import java.io.IOException;
3 import java.lang.ref.Reference; 5 import java.lang.ref.Reference;
4 import java.lang.ref.WeakReference; 6 import java.lang.ref.WeakReference;
5 import java.util.List; 7 import java.util.List;
6 import java.util.ArrayList; 8 import java.util.ArrayList;
7 import java.util.Map; 9 import java.util.Map;
13 public abstract class LuanState implements DeepCloneable { 15 public abstract class LuanState implements DeepCloneable {
14 16
15 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); 17 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
16 18
17 private Map registry; 19 private Map registry;
18 private final List<Reference<Runnable>> onClose = new ArrayList<Reference<Runnable>>(); 20 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
19 21
20 protected LuanState() { 22 protected LuanState() {
21 registry = new HashMap(); 23 registry = new HashMap();
22 } 24 }
23 25
33 35
34 public final Map registry() { 36 public final Map registry() {
35 return registry; 37 return registry;
36 } 38 }
37 39
38 public void onClose(Runnable fn) { 40 public void onClose(Closeable c) {
39 onClose.add(new WeakReference<Runnable>(fn)); 41 onClose.add(new WeakReference<Closeable>(c));
40 } 42 }
41 43
42 public void close() { 44 public void close() throws IOException {
43 for( Reference<Runnable> ref : onClose ) { 45 for( Reference<Closeable> ref : onClose ) {
44 Runnable r = ref.get(); 46 Closeable c = ref.get();
45 if( r != null ) 47 if( c != null )
46 r.run(); 48 c.close();
47 } 49 }
48 onClose.clear(); 50 onClose.clear();
49 } 51 }
50 52
51 public static LuanState newInstance() { 53 public static LuanState newInstance() {