@Override public Collection<T> findForKey(final String attributeName, final Object key) { final Index<T> index = attribName2Index.get(attributeName); return index == null ? new ArrayList<T>() : index.get(key); }
@Override public Collection<T> getAllFromCache() { if (primaryKeyAttributeName == null) { return Collections.emptyList(); } final Index<T> index = attribName2Index.get(primaryKeyAttributeName); return index.getAllValues(); }
@Override public void removeFromCache(final T cacheable2Remove) throws IllegalAccessException, InvocationTargetException { for (final Map.Entry<String, Method> entry : attribName2Method.entrySet()) { final String attrName = entry.getKey(); final Method readMethod = entry.getValue(); final Object key = readMethod.invoke(cacheable2Remove); final Index<T> index = attribName2Index.get(attrName); index.remove(key, cacheable2Remove); } }
/** * Adds the speciafied cacheable to the cache * * @param cacheable cacheable to be added */ @Override public void fillCache(final T cacheable) throws IllegalAccessException, InvocationTargetException { final boolean annotationPresent = cacheable.getClass().isAnnotationPresent(Cacheable.class); if (annotationPresent) { final Cacheable annotation = cacheable.getClass().getAnnotation(Cacheable.class); primaryKeyAttributeName = annotation.primaryKeyAttributeName(); for (final Map.Entry<String, Method> attribute : attribName2Method.entrySet()) { final String attribName = attribute.getKey(); Index<T> index = attribName2Index.get(attribName); if (index == null) { index = new Index<>(); attribName2Index.put(attribName, index); } final Method readMethod = attribute.getValue(); final Object key = readMethod.invoke(cacheable); index.putSingleOrMultiple(key, cacheable); } } else { if (logger.isDebugEnabled()) { logger.debug("Class has no Annotation Cacheable"); } } }
@Override public Collection<T> findForKeys(final String attributeName, final Collection<? extends Object> keys) { final Index<T> index = attribName2Index.get(attributeName); if (index == null) { return Collections.emptyList(); } final Set<T> result = new HashSet<>(); for (final Object key : keys) { result.addAll(index.get(key)); } return result; }