KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > cache > FieldCache


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal.cache;
19
20 import java.lang.reflect.Field JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
24
25 import org.apache.beehive.netui.util.logging.Logger;
26
27 /**
28  * @exclude
29  */

30 public class FieldCache
31 {
32     private static final Logger _log = Logger.getInstance( FieldCache.class );
33
34     private final InternalConcurrentHashMap _fieldCache;
35     private final InternalConcurrentHashMap _declaredFieldCache;
36
37     public FieldCache()
38     {
39         _fieldCache = new InternalConcurrentHashMap();
40         _declaredFieldCache = new InternalConcurrentHashMap();
41     }
42
43     public final Field JavaDoc getField( Class JavaDoc type, String JavaDoc fieldName )
44     {
45         if ( _log.isDebugEnabled() ) _log.debug( "getFields for: " + type );
46         
47         HashMap JavaDoc fields = ( HashMap JavaDoc ) _fieldCache.get( type );
48         
49         if ( fields == null )
50         {
51             Field JavaDoc[] fieldArray = type.getFields();
52             fields = new HashMap JavaDoc();
53             
54             for ( int i = 0; i < fieldArray.length; i++ )
55             {
56                 Field JavaDoc field = fieldArray[i];
57                 fields.put( field.getName(), field );
58             }
59             
60             _fieldCache.put( type, fields );
61         }
62         
63         return ( Field JavaDoc ) fields.get( fieldName );
64     }
65     
66     public final Field JavaDoc getDeclaredField( Class JavaDoc type, String JavaDoc fieldName )
67     {
68         if ( _log.isDebugEnabled() ) _log.debug( "getDeclaredFields for: " + type );
69
70         HashMap JavaDoc fields = ( HashMap JavaDoc ) _declaredFieldCache.get( type );
71
72         if ( fields == null )
73         {
74             Field JavaDoc[] fieldArray = type.getDeclaredFields();
75             fields = new HashMap JavaDoc();
76             
77             for ( int i = 0; i < fieldArray.length; i++ )
78             {
79                 Field JavaDoc field = fieldArray[i];
80                 field.setAccessible( true );
81                 fields.put( field.getName(), field );
82             }
83             
84             _declaredFieldCache.put( type, fields );
85         }
86
87         return ( Field JavaDoc ) fields.get( fieldName );
88     }
89 }
90
Popular Tags