Subversion Repository Public Repository

litesoft

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.core;

import org.litesoft.commonfoundation.typeutils.Objects;

import java.util.*;

public class ClassToInstanceMapperImpl implements ClassToInstanceMapper
{
    public ClassToInstanceMapperImpl()
    {
    }

    private Map<Class<?>, NoInstanceFactory<?>> mFactoriesByClass = new HashMap<Class<?>, NoInstanceFactory<?>>();
    private Map<Class<?>, Object> mInstanceByClass = new HashMap<Class<?>, Object>();

    /**
     * Get a List (!null) of all the currently "set" Factories
     */
    public synchronized List<NoInstanceFactory<?>> getNoInstanceFactories()
    {
        return new ArrayList<NoInstanceFactory<?>>( 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
     */
    public void setNoInstanceFactory( NoInstanceFactory<?> pFactory )
    {
        Objects.assertNotNull( "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 set / replaced
     *
     * @throws IllegalArgumentException if pClass is null
     */
    public <T> T checkGet( Class<T> pClass )
            throws IllegalArgumentException
    {
        assertClass( pClass );
        synchronized ( this )
        {
            //noinspection unchecked
            return (T) mInstanceByClass.get( pClass );
        }
    }

    /**
     * Get the previously 'registered' (set &/ 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
     */
    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 set &/ 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'
     */
    public <T, V extends T> void set( 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 set &/ 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'
     */
    public <T, V extends T> void replace( 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 replace for: " + pClass );
    }

    /**
     * Remove the instance (pInstance) of pClass, that IS currently 'registered' (previously set &/ 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
     */
    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 );
    }

    public synchronized void clearAllInstances()
    {
        mInstanceByClass.clear();
    }

    private void assertClass( Class<?> pClass )
    {
        Objects.assertNotNull( "Class (key)", pClass );
    }

    private void assertClassAndInstance( Class<?> pClass, Object pInstance )
    {
        assertClass( pClass );
        Objects.assertNotNull( "pInstance (value)", pInstance );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/ClassToInstanceMapperImpl.java

Diff revisions: vs.
Revision Author Commited Message
947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000