PageRenderTime 34ms CodeModel.GetById 29ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/sitebricks/src/main/java/com/google/sitebricks/binding/ConcurrentPropertyCache.java

http://github.com/dhanji/sitebricks
Java | 44 lines | 31 code | 9 blank | 4 comment | 3 complexity | de3bc4870918d786446424df752b4e36 MD5 | raw file
 1package com.google.sitebricks.binding;
 2
 3import java.beans.IntrospectionException;
 4import java.beans.Introspector;
 5import java.beans.PropertyDescriptor;
 6import java.util.LinkedHashMap;
 7import java.util.Map;
 8import java.util.concurrent.ConcurrentHashMap;
 9import java.util.concurrent.ConcurrentMap;
10
11/**
12 * @author Dhanji R. Prasanna (dhanji@gmail.com)
13 */
14class ConcurrentPropertyCache implements PropertyCache {
15    private final ConcurrentMap<Class<?>, Map<String, String>> cache =
16                            new ConcurrentHashMap<Class<?>, Map<String, String>>();
17
18    public boolean exists(String property, Class<?> anObjectClass) {
19
20        Map<String, String> properties = cache.get(anObjectClass);
21
22        //cache bean properties if needed
23        if (null == properties) {
24            PropertyDescriptor[] propertyDescriptors;
25            try {
26                propertyDescriptors = Introspector
27                        .getBeanInfo(anObjectClass)
28                        .getPropertyDescriptors();
29
30            } catch (IntrospectionException e) {
31                throw new IllegalArgumentException();
32            }
33    
34            properties = new LinkedHashMap<String, String>();
35            for (PropertyDescriptor descriptor : propertyDescriptors) {
36                properties.put(descriptor.getName(), descriptor.getName());    //apply labels here as needed
37            }
38
39            cache.putIfAbsent(anObjectClass, properties);
40        }
41
42        return properties.containsKey(property);
43    }
44}