br.com.anteros.bean.validation.MetaBeanCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Anteros-Bean-Validation Show documentation
Show all versions of Anteros-Bean-Validation Show documentation
Anteros Bean Validation for Java.
/*******************************************************************************
* Copyright 2012 Anteros Tecnologia
*
* 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 br.com.anteros.bean.validation;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import br.com.anteros.bean.validation.model.MetaBean;
/**
* Description: a cache to hold metabeans by id and by class.
*/
public class MetaBeanCache implements MetaBeanFinder, Serializable {
private static final long serialVersionUID = 1L;
/**
* Cache keyed by id.
*/
protected final ConcurrentMap cacheById = new ConcurrentHashMap();
/**
* Cache keyed by class.
*/
protected final ConcurrentMap, MetaBean> cacheByClass = new ConcurrentHashMap, MetaBean>();
/**
* Create a new MetaBeanCache instance.
*/
public MetaBeanCache() {
super();
}
/**
* Create a new MetaBeanCache instance.
* @param beans
*/
public MetaBeanCache(Map beans) {
this();
for (MetaBean bean : beans.values()) {
cache(bean);
}
}
/**
* Clear the cache.
*/
public void clear() {
cacheById.clear();
cacheByClass.clear();
}
/**
* {@inheritDoc}
*/
public MetaBean findForId(String beanInfoId) {
return cacheById.get(beanInfoId);
}
/**
* {@inheritDoc}
*/
public MetaBean findForClass(Class> clazz) {
return cacheByClass.get(clazz);
}
/**
* Return all cached MetaBeans by id.
* @return live map
*/
public Map findAll() {
return cacheById;
}
/**
* Cache the specified MetaBean.
* @param beanInfo
*/
public void cache(MetaBean beanInfo) {
cacheById.put(beanInfo.getId(), beanInfo);
if (beanInfo.getBeanClass() != null &&
beanInfo.getId().equals(beanInfo.getBeanClass().getName())) {
cacheByClass.putIfAbsent(beanInfo.getBeanClass(), beanInfo);
}
}
/**
* Remove a single MetaBean from the cache.
* @param beanInfo
*/
public void removeFromCache(MetaBean beanInfo) {
cacheById.remove(beanInfo.getId());
if (beanInfo.getBeanClass() != null &&
beanInfo.getId().equals(beanInfo.getBeanClass().getName())) {
cacheByClass.remove(beanInfo.getBeanClass());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy