/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

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