litesoft
@ 955
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.core; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; public class ClassToInstanceMapperImpl implements ClassToInstanceMapper { public ClassToInstanceMapperImpl() { } private Map<Class<?>, NoInstanceFactory<?>> mFactoriesByClass = Maps.newHashMap(); private Map<Class<?>, Object> mInstanceByClass = Maps.newHashMap(); /** * Get a List (!null) of all the currently "register" Factories */ @Override public synchronized List<NoInstanceFactory<?>> getNoInstanceFactories() { return Lists.newArrayList(mFactoriesByClass.values() ); } /** * Set a Factory into this Mapper. This Factory is used to create an Instance (and register it) when "get" is called and no Instance is currently registered. * * @param pFactory !null * * @throws IllegalArgumentException if pFactory is null * @throws IllegalStateException if this is a duplicate Factory */ @Override public void register( NoInstanceFactory<?> pFactory ) { Confirm.isNotNull( "NoInstanceFactory", pFactory ); Class<?> zClass = pFactory.getFor(); assertClass( zClass ); synchronized ( this ) { NoInstanceFactory<?> zFactory = mFactoriesByClass.get( zClass ); if ( zFactory == null ) { mFactoriesByClass.put( zClass, pFactory ); return; } } throw new IllegalStateException( "Duplicate Factory for: " + zClass ); } /** * @param pClass !null * * @return null if not found or current instance register / replaced * * @throws IllegalArgumentException if pClass is null */ @Override public <T> T checkGet( Class<T> pClass ) throws IllegalArgumentException { assertClass( pClass ); synchronized ( this ) { //noinspection unchecked return (T) mInstanceByClass.get( pClass ); } } /** * Get the previously 'registered' (register &/ replaced) instance of pClass. * NOTE: if there is no instance currently registered, then one is attempted to be added using a previously registered NoInstanceFactory. * * @param pClass !null * * @return instance of pClass * * @throws IllegalArgumentException if pClass is null * @throws IllegalStateException if no instance previously 'registered' for pClass */ @Override public <T> T get( Class<T> pClass ) throws IllegalArgumentException, IllegalStateException { assertClass( pClass ); Object zInstance; synchronized ( this ) { if ( null == (zInstance = mInstanceByClass.get( pClass )) ) { NoInstanceFactory<?> zFactory = mFactoriesByClass.get( pClass ); if ( zFactory != null ) { if ( null != (zInstance = zFactory.createInstance()) ) { mInstanceByClass.put( pClass, zInstance ); } } } } if ( zInstance == null ) { throw new IllegalStateException( "No instance found for: " + pClass ); } //noinspection unchecked return (T) zInstance; } /** * Set or register an instance (pInstance) of pClass, that is NOT currently 'registered' (previously register &/ replaced, but not subsequently removed) * * @param pClass !null * @param pInstance !null * * @throws IllegalArgumentException if either pClass or pInstance is null * @throws IllegalStateException if an instance for pClass IS currently 'registered' */ @Override public <T, V extends T> void register( Class<T> pClass, V pInstance ) throws IllegalArgumentException, IllegalStateException { assertClassAndInstance( pClass, pInstance ); synchronized ( this ) { Object zInstance = mInstanceByClass.get( pClass ); if ( zInstance == null ) { mInstanceByClass.put( pClass, pInstance ); return; } } throw new IllegalStateException( "Already an instance for: " + pClass ); } /** * Replace or re-register an instance (pInstance) of pClass, that IS currently 'registered' (previously register &/ replaced, but not subsequently removed) * * @param pClass !null * @param pInstance !null * * @throws IllegalArgumentException if either pClass or pInstance is null * @throws IllegalStateException if an instance for pClass is NOT currently 'registered' */ @Override public <T, V extends T> void forceRegister( Class<T> pClass, V pInstance ) throws IllegalArgumentException, IllegalStateException { assertClassAndInstance( pClass, pInstance ); synchronized ( this ) { Object zInstance = mInstanceByClass.get( pClass ); if ( zInstance != null ) { mInstanceByClass.put( pClass, pInstance ); return; } } throw new IllegalStateException( "No instance to forceRegister for: " + pClass ); } /** * Remove the instance (pInstance) of pClass, that IS currently 'registered' (previously register &/ replaced, but not subsequently removed) * * @param pClass !null * @param pInstance !null * * @throws IllegalArgumentException if either pClass or pInstance is null * @throws IllegalStateException if pInstance of pClass is NOT currently 'registered' for pClass */ @Override public <T, V extends T> void remove( Class<T> pClass, V pInstance ) throws IllegalArgumentException, IllegalStateException { assertClassAndInstance( pClass, pInstance ); Object zInstance; synchronized ( this ) { zInstance = mInstanceByClass.get( pClass ); if ( pInstance == zInstance ) { mInstanceByClass.remove( pClass ); return; } } if ( zInstance == null ) { throw new IllegalStateException( "No instance currently exists for: " + pClass ); } throw new IllegalStateException( "Instance to remove '" + pInstance + "' mismatchs found '" + zInstance + "' for: " + pClass ); } @Override public synchronized void clearAllInstances() { mInstanceByClass.clear(); } private void assertClass( Class<?> pClass ) { Confirm.isNotNull( "Class (key)", pClass ); } private void assertClassAndInstance( Class<?> pClass, Object pInstance ) { assertClass( pClass ); Confirm.isNotNull( "pInstance (value)", pInstance ); } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/ClassToInstanceMapperImpl.java
Revision | Author | Commited | Message |
---|---|---|---|
955 Diff | GeorgeS | Sun 22 Jun, 2014 18:11:25 +0000 | Add Instance... |
953 Diff | GeorgeS | Fri 20 Jun, 2014 23:12:43 +0000 | Drop Nas-Video. |
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |