/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
- package com.google.sitebricks.binding;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ConcurrentMap;
- /**
- * @author Dhanji R. Prasanna (dhanji@gmail.com)
- */
- class ConcurrentPropertyCache implements PropertyCache {
- private final ConcurrentMap<Class<?>, Map<String, String>> cache =
- new ConcurrentHashMap<Class<?>, Map<String, String>>();
- public boolean exists(String property, Class<?> anObjectClass) {
- Map<String, String> properties = cache.get(anObjectClass);
- //cache bean properties if needed
- if (null == properties) {
- PropertyDescriptor[] propertyDescriptors;
- try {
- propertyDescriptors = Introspector
- .getBeanInfo(anObjectClass)
- .getPropertyDescriptors();
- } catch (IntrospectionException e) {
- throw new IllegalArgumentException();
- }
-
- properties = new LinkedHashMap<String, String>();
- for (PropertyDescriptor descriptor : propertyDescriptors) {
- properties.put(descriptor.getName(), descriptor.getName()); //apply labels here as needed
- }
- cache.putIfAbsent(anObjectClass, properties);
- }
- return properties.containsKey(property);
- }
- }