1 3 4 package org.ozoneDB; 5 6 import org.ozoneDB.data.SimpleArrayList; 7 8 13 public class CurrentDatabase { 14 15 protected static ThreadLocal instances = new ThreadLocal () { 16 protected Object initialValue() { 17 return new CurrentDatabase(); 18 } 19 }; 20 21 protected SimpleArrayList stack; 22 23 protected CurrentDatabase() { 24 stack = new SimpleArrayList(6); 25 } 26 27 protected void addCurrentDatabase(OzoneInterface newCurrentDatabase) { 28 stack.push(newCurrentDatabase); 29 } 30 31 protected void removeCurrentDatabase() { 32 Object result = stack.pop(); 33 34 assert result!=null; 35 } 36 37 protected OzoneInterface getCurrentDatabase() { 38 OzoneInterface currentDatabase = (OzoneInterface) stack.peek(); 39 40 if (currentDatabase==null) { 42 throw new IllegalStateException ("There is no current database defined for this thread "+Thread.currentThread()+"."); 43 } 44 45 return currentDatabase; 46 } 47 48 73 public static void register(OzoneInterface newCurrentDatabase) { 74 assert newCurrentDatabase!=null; 75 76 ((CurrentDatabase) instances.get()).addCurrentDatabase(newCurrentDatabase); 77 } 78 79 83 public static void unregister() { 84 ((CurrentDatabase) instances.get()).removeCurrentDatabase(); 85 } 86 87 95 public static OzoneInterface get() throws IllegalStateException { 96 return ((CurrentDatabase) instances.get()).getCurrentDatabase(); 97 } 98 } 99 100 | Popular Tags |