1 package org.apache.commons.ognl.internal;
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 import org.apache.commons.ognl.ClassCacheInspector;
27
28 import java.util.Arrays;
29
30
31
32
33 public class ClassCacheImpl<V>
34 implements ClassCache<V>
35 {
36
37
38 private static final int TABLE_SIZE = 512;
39
40
41 private static final int TABLE_SIZE_MASK = TABLE_SIZE - 1;
42
43 private Entry<Class<?>, V>[] table = new Entry[TABLE_SIZE];
44
45 private ClassCacheInspector classInspector;
46
47 private int size = 0;
48
49
50
51
52 public void setClassInspector( ClassCacheInspector inspector )
53 {
54 classInspector = inspector;
55 }
56
57
58
59
60 public void clear()
61 {
62 for ( int i = 0; i < table.length; i++ )
63 {
64 table[i] = null;
65 }
66
67 size = 0;
68 }
69
70
71
72
73 public int getSize()
74 {
75 return size;
76 }
77
78
79
80
81 public final V get( Class<?> key )
82 throws CacheException
83 {
84 int i = key.hashCode() & TABLE_SIZE_MASK;
85
86 Entry<Class<?>, V> entry = table[i];
87
88 while ( entry != null )
89 {
90 if ( key == entry.getKey() )
91 {
92 return entry.getValue();
93 }
94
95 entry = entry.getNext();
96 }
97 return null;
98 }
99
100
101
102
103 public final V put( Class<?> key, V value )
104 {
105 if ( classInspector != null && !classInspector.shouldCache( key ) )
106 {
107 return value;
108 }
109
110 V result = null;
111 int i = key.hashCode() & TABLE_SIZE_MASK;
112
113 Entry<Class<?>, V> entry = table[i];
114
115 if ( entry == null )
116 {
117 table[i] = new Entry<Class<?>, V>( key, value );
118 size++;
119 }
120 else
121 {
122 if ( key == entry.getKey() )
123 {
124 result = entry.getValue();
125 entry.setValue( value );
126 }
127 else
128 {
129 while ( true )
130 {
131 if ( key == entry.getKey() )
132 {
133
134 result = entry.getValue();
135 entry.setValue( value );
136 break;
137 }
138
139 if ( entry.getNext() == null )
140 {
141
142 entry.setNext( new Entry<Class<?>, V>( key, value ) );
143 break;
144 }
145
146 entry = entry.getNext();
147 }
148 }
149 }
150
151 return result;
152 }
153
154
155
156
157 @Override
158 public String toString()
159 {
160 return "ClassCacheImpl[" + "_table=" + ( table == null ? null : Arrays.asList( table ) ) + '\n'
161 + ", _classInspector=" + classInspector + '\n' + ", _size=" + size + '\n' + ']';
162 }
163
164 }