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