All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.commons.cache.AnnotationUsageCache Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phloc.commons.cache;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.string.ToStringGenerator;

/**
 * A simple cache for the usage of a certain annotation class at other classes.
* Note: cannot use {@link com.phloc.commons.cache.AbstractNotifyingCache} * because it would need a Class<?> as a key and this would * be a hard wired reference. * * @author Philip Helger */ @ThreadSafe public class AnnotationUsageCache implements Serializable { private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock (); private final Class m_aAnnotationClass; @GuardedBy ("m_aRWLock") private final Map m_aMap = new HashMap (); /** * Constructor * * @param aAnnotationClass * The annotation class to store the existence of. It must have the * {@link RetentionPolicy#RUNTIME} to be usable within this class! */ public AnnotationUsageCache (@Nonnull final Class aAnnotationClass) { ValueEnforcer.notNull (aAnnotationClass, "AnnotationClass"); // Check retention policy final Retention aRetention = aAnnotationClass.getAnnotation (Retention.class); final RetentionPolicy eRetentionPolicy = aRetention == null ? RetentionPolicy.CLASS : aRetention.value (); if (eRetentionPolicy != RetentionPolicy.RUNTIME) throw new IllegalArgumentException ("RetentionPolicy must be of type RUNTIME to be used within this cache. The current value ist " + eRetentionPolicy); // Save to members m_aAnnotationClass = aAnnotationClass; } /** * @return The annotation class passed in the constructor. Never * null. */ @Nonnull public final Class getAnnotationClass () { return m_aAnnotationClass; } public boolean hasAnnotation (@Nonnull final Object aObject) { ValueEnforcer.notNull (aObject, "Object"); return hasAnnotation (aObject.getClass ()); } public boolean hasAnnotation (@Nonnull final Class aClass) { ValueEnforcer.notNull (aClass, "Class"); final String sClassName = aClass.getName (); Boolean aIs; m_aRWLock.readLock ().lock (); try { aIs = m_aMap.get (sClassName); } finally { m_aRWLock.readLock ().unlock (); } if (aIs == null) { m_aRWLock.writeLock ().lock (); try { // Try again in write-lock aIs = m_aMap.get (sClassName); if (aIs == null) { aIs = Boolean.valueOf (aClass.getAnnotation (m_aAnnotationClass) != null); m_aMap.put (sClassName, aIs); } } finally { m_aRWLock.writeLock ().unlock (); } } return aIs.booleanValue (); } public void setAnnotation (@Nonnull final Class aClass, final boolean bHasAnnotation) { ValueEnforcer.notNull (aClass, "Class"); final String sClassName = aClass.getName (); m_aRWLock.writeLock ().lock (); try { m_aMap.put (sClassName, Boolean.valueOf (bHasAnnotation)); } finally { m_aRWLock.writeLock ().unlock (); } } public void clearCache () { m_aRWLock.writeLock ().lock (); try { m_aMap.clear (); } finally { m_aRWLock.writeLock ().unlock (); } } @Override public String toString () { return new ToStringGenerator (this).append ("annotationClass", m_aAnnotationClass) .append ("map", m_aMap) .toString (); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy