1 26 27 package net.sourceforge.groboutils.util.classes.v1; 28 29 30 31 39 public abstract class AbstractSingleStore 40 { 41 private Class instanceOf; 42 private Object singleton; 43 44 45 51 public AbstractSingleStore( Class instanceOf ) 52 { 53 this.instanceOf = instanceOf; 54 } 55 56 57 64 public Object getSingleton() 65 { 66 synchronized( this ) 67 { 68 if (this.singleton == null) 69 { 70 setDefaultSingleton(); 71 if (this.singleton == null) 72 { 73 throw new IllegalStateException ( "No singleton created." ); 74 } 75 } 76 } 77 return this.singleton; 78 } 79 80 81 88 public synchronized void setSingleton( Object singleton ) 89 { 90 if (singleton == null) 91 { 92 throw new IllegalArgumentException ("no null arguments"); 93 } 94 if (this.instanceOf != null && 95 !this.instanceOf.isInstance( singleton )) 96 { 97 throw new IllegalArgumentException ( "Passed-in singleton "+ 98 singleton+" is not assignable to class "+ 99 this.instanceOf.getName()+", but is of class "+ 100 singleton.getClass().getName() ); 101 } 102 this.singleton = singleton; 103 } 104 105 106 110 protected abstract void setDefaultSingleton(); 111 112 113 126 protected static Object createFromProperty( String key, 127 Class defaultClass ) 128 { 129 if (key == null) 130 { 131 throw new IllegalArgumentException ("no null args"); 132 } 133 ClassLoadHelper clh = new ClassLoadHelper(); 134 return clh.createObjectFromProperty( key, defaultClass, false ); 135 } 136 } 137 138 | Popular Tags |