A
download AutoValueMap.java
Language: Java
Copyright: Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
LOC: 100
Project Info
klaro - Klaro Projects(klaro)
Server: Google
Type: svn
...default\de\klaro\base\util\
   ...ractFileNameFilter.java
   AutoValueFactory.java
   AutoValueMap.java
   Bits.java
   Booleans.java
   Classes.java
   Combination.java
   ...igurationException.java
   Const.java
   Conversion.java
   Dates.java
   ...sionFileNameFilter.java
   Files.java
   ...archicalProperties.java
   Locales.java
   LoggingSystem.java
   ...gPropertyException.java
   ...plementedException.java
   Numbers.java
   PatternFileNameFilter.java
   Requests.java
   ResourceProperties.java
   Resources.java
   StartupProperties.java
   StopWatch.java
   Streams.java
   StringComparator.java
   StringProvider.java
   Strings.java
   StringTokenProperties.java
   Util.java

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*\
 * Copyright  2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
 * _____________________________________________________________________________________________________________________
 * 
 * This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
 * under the terms of the BSD License. The license text is appended to the end of this file.
\*/

package de.klaro.base.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;


/**
 * Wrapper fr Maps, mit dem fehlende Schlssel mit Hilfe einer Factory automatisch erzeugt werden.
 * 
 * Diese Implementation erweitert existierende Map-Instanzen, indem alle Aufrufe an die die im Konstruktor
 * angegebene Map delegiert werden. Zustzlich gibt es eine neue Methode obtain, die wie die Methode get arbeitet,
 * zustzlich aber die genannten Eigenschaften hat.
 * 
 * @param <K> Der Typ der Schlssel
 * @param <V> Der Typ der Werte
 */

public class AutoValueMap<K extends Object, V extends Object> extends Object implements Serializable, Map<K,V>
{

    /* ______________________________________________________________________________________________________________ *\
    \* Konstanten                                                                                                     */


    /* ______________________________________________________________________________________________________________ *\
    \* Klassenvariablen                                                                                               */


    /* ______________________________________________________________________________________________________________ *\
    \* Instanzvariablen                                                                                               */

    
    private Map<K,V> map$;
    private AutoValueFactory<V> factory$;
    

    /* ______________________________________________________________________________________________________________ *\
    \* Konstruktoren                                                                                                  */


    /**
     * Erzeugt eine Instanz dieser Klasse.
     * 
     * Dieser Konstruktor erzeugt intern eine Factory, 
     *
     * @param map               Die Map, an die die Methodenaufrufe delegiert werden 
     * @param clazz 
     */

    public AutoValueMap(Map <K,V> map,Class<? extends V> clazz)
    {
        this(map,new SimpleAutoHashMapFactory<V>(clazz));
    }


    /**
     * Erzeugt eine Instanz dieser Klasse.
     *
     * @param map               Die Map, an die die Methodenaufrufe delegiert werden 
     * @param factory           Die Factory fr Werte fr den Fall, dass ein Wert nicht vorhanden ist
     */

    public AutoValueMap(Map <K,V> map,AutoValueFactory<V> factory)
    {
        super();
        
        map$ = map;
        factory$ = factory;
    }


    /* ______________________________________________________________________________________________________________ *\
    \* Instanzmethoden                                                                                                */
    

    /**
     * Liefert die Anzahl der Eintrge.
     *
     * @return                  Die Anzahl der Eintrge
     */
    
    public int size()
    {
        return map$.size();
    }
    

    /**
     * Prft, ob die Map leer ist.
     *
     * @return                  Die Map ist leer
     */
    
    public boolean isEmpty()
    {
        return map$.isEmpty();
    }

    
    /**
     * Prft, ob der Eintrag mit dem angegebenen Schlssel existiert.
     *
     * @param  key              Der Schlssel
     * @return                  Der Eintrag existiert
     */
    
    public boolean containsKey(Object key)
    {
        return map$.containsKey(key);
    }

    
    /**
     * Prft, ob der Eintrag mit dem angegebenen Wert existiert.
     *
     * @param  value            Der Wert
     * @return                  Der Eintrag existiert
     */
    
    public boolean containsValue(Object value)
    {
        return map$.containsValue(value);
    }

    
    /**
     * Liefert den Eintrag mit dem angegebenen Schlssel.
     *
     * @param  key              Der Schlssel
     * @return                  Der Eintrag, oder 'null' wenn der Eintrag nicht existiert
     */
    
    public V get(Object key)
    {
        return map$.get(key);
    }

    
    /**
     * Liefert den Wert eines Schlssels.
     * 
     * Es kann bestimmt werden, ob ein Wert mit Hilfe der Factory erzeugt werden soll, wenn fr den Schlssel noch
     * kein Eintrag vorhanden ist.
     *
     * @param key               Der Schlssel
     * @param autoCreate        Der Eintrag wird erzeugt, wenn noch nicht vorhanden 
     * @return                  Der Wert
     */
    
    public V get(K key, boolean autoCreate)
    {
        V value = map$.get(key);
        
        if (autoCreate && value == null)
        {
            try
            {
                value = factory$.newInstance(key);
            }
            catch (IllegalAccessException e)
            {
                // Keine Aktion ntig, der Wert soll 'null' bleiben
            }
            catch (InstantiationException e)
            {
                // Keine Aktion ntig, der Wert soll 'null' bleiben
            }
            
            map$.put(key,value);
        }
        
        return value;
    }

    
    /**
     * Fgt einen neuen Eintrag hinzu.
     *
     * @param  key              Der Schlssel
     * @param  value            Der Wert
     * @return                  Der Wert des zuvor unter dem Schlssel gespeicherten Wertes, oder 'null'
     */
    
    public V put(K key, V value)
    {
        return map$.put(key,value);
    }

    
    /**
     * Lscht den Eintrag mit dem angegebenen Schlssel.
     *
     * @param  key              Der Schlssel
     * @return                  Der Wert des zuvor unter dem Schlssel gespeicherten Wertes, oder 'null'
     */
    
    public V remove(Object key)
    {
        return map$.remove(key);
    }


    /**
     * Kopiert die Eintrge der angegebenen Map.
     *
     * @param map               Die Map
     */
    
    public void putAll(Map<? extends K, ? extends V> map)
    {
        map$.putAll(map);
    }

    
    /**
     * Lscht alle Eintrge.
     */
    
    public void clear()
    {
        map$.clear();
    }


    /**
     * Liefert die Schlssel aller Eintrge.
     *
     * @return                  Die Schlssel
     */
    
    public Set<K> keySet()
    {
        return map$.keySet();
    }

    
    /**
     * Liefert die Werte aller Eintrge.
     *
     * @return                  Die Werte
     */
    
    public Collection<V> values()
    {
        return map$.values();
    }

    
    /**
     * Liefert alle Eintrge.
     *
     * @return                  Die Eintrge
     */
    
    public Set<Map.Entry<K, V>> entrySet()
    {
        return map$.entrySet();
    }


    /* ______________________________________________________________________________________________________________ *\
    \* Klassenmethoden                                                                                                */


    /* ______________________________________________________________________________________________________________ *\
    \* Klassen                                                                                                        */

    
    /**
     * Factory, die Objekte per Standard-Konstruktor erzeugt.
     * 
     * @param  <V1>             Der Typ des Wertes, die die Factory erzeugt
     */
    
    private static class SimpleAutoHashMapFactory<V1 extends Object> extends Object implements AutoValueFactory<V1>
    {
        private Class<? extends V1> clazz$;
        
        
        /**
         * Erzeugt eine Factory.
         *
         * @param clazz             Die Klasse, fr die Objekte erzeugt werden sollen
         */
        public SimpleAutoHashMapFactory(Class<? extends V1> clazz)
        {
            super();
            
            clazz$ = clazz;
        }

        
        /**
         * Erzeugt eine Instanz, indem der Standard-Konstruktor benutzt wird.
         * 
         * @param key               Der Schlssel
         * @return                  Der Wert
         * @throws IllegalAccessException Keine Zugriffrechte auf die Klasse
         * @throws InstantiationException Fehler whrend des Konstruktoraufrufs
         */
        
        public V1 newInstance(Object key) throws IllegalAccessException, InstantiationException
        {
            return clazz$.newInstance();
        }
    }

}

/*\
 * _____________________________________________________________________________________________________________________
 * 
 * This software is distributed under the terms of the BSD License:
 * 
 * Copyright  2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 *   disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * _____________________________________________________________________________________________________________________
\*/

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us