br.com.anteros.bean.validation.model.FeaturesCapable 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.model;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantLock;
import br.com.anteros.core.utils.ArrayUtils;
/**
* Description: abstract superclass of meta objects that support a map of
* features.
*/
public abstract class FeaturesCapable implements Serializable {
private static final long serialVersionUID = -4045110242904814218L;
private ConcurrentMap features = createFeaturesMap();
/** key = validation id, value = the validation */
private Validation[] validations = new Validation[0];
private volatile boolean locking;
private ReentrantLock lock = new ReentrantLock(true);
/**
* Create a new FeaturesCapable instance.
*/
public FeaturesCapable() {
super();
}
/**
* Get the (live) map of features.
*
* @return Map
*/
public Map getFeatures() {
return features;
}
/**
* Set whether to optimize read operations by accessing the features map in
* an unsynchronized manner.
*
* @param fast
*/
public void optimizeRead(boolean fast) {
lock.lock();
try {
this.locking = !fast;
} finally {
lock.unlock();
}
}
/**
* Get the specified feature.
*
* @param
* @param key
* @return T
*/
public T getFeature(String key) {
return getFeature(key, (T) null);
}
/**
* Get the specified feature, returning defaultValue
if
* undeclared.
*
* @param
* @param key
* @param defaultValue
* @return T
*/
@SuppressWarnings("unchecked")
public T getFeature(String key, T defaultValue) {
boolean release = acquireLockIfNeeded();
try {
return features.containsKey(key) ? (T) features.get(key) : defaultValue;
} finally {
if (release) {
lock.unlock();
}
}
}
/**
* Convenience method to set a particular feature value.
*
* @param
* @param key
* @param value
*/
public void putFeature(String key, T value) {
boolean release = acquireLockIfNeeded();
try {
features.put(key, value);
} finally {
if (release) {
lock.unlock();
}
}
}
/**
* Create a deep copy (copy receiver and copy properties).
*
* @param
* @return new T instance
*/
public T copy() {
try {
@SuppressWarnings("unchecked")
final T self = (T) clone();
copyInto(self);
return self;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException("cannot clone() " + this, e);
}
}
/**
* Copy this {@link FeaturesCapable} into another {@link FeaturesCapable}
* instance.
*
* @param target
*/
protected void copyInto(FeaturesCapable target) {
target.features = target.createFeaturesMap();
target.features.putAll(features);
target.validations = ArrayUtils.clone(validations);
}
/**
* Get any validations set for this {@link FeaturesCapable}.
*
* @return Validation array
*/
public Validation[] getValidations() {
return ArrayUtils.clone(validations);
}
/**
* Set the validations for this {@link FeaturesCapable}.
*
* @param validations
*/
public void setValidations(Validation[] validations) {
this.validations = ArrayUtils.clone(validations);
}
/**
* Add a validation to this {@link FeaturesCapable}.
*
* @param validation
* to add
*/
public void addValidation(Validation validation) {
if (this.validations == null) {
this.validations = new Validation[] { validation };
} else {
Validation[] newValidations = new Validation[this.validations.length + 1];
System.arraycopy(this.validations, 0, newValidations, 0, this.validations.length);
newValidations[validations.length] = validation;
this.validations = newValidations;
}
}
/**
* Search for an equivalent validation among those configured.
*
* @param aValidation
* @return true if found
*/
public boolean hasValidation(Validation aValidation) {
if (validations == null)
return false;
for (Validation validation : validations) {
if (validation.equals(aValidation))
return true;
}
return false;
}
/**
* Create a features map for this {@link FeaturesCapable} object.
* @return ConcurrentMap
*/
protected ConcurrentMap createFeaturesMap() {
return new ConcurrentHashMap();
}
private boolean acquireLockIfNeeded() {
if (locking) {
lock.lock();
// double read
if (locking) {
return true;
}
lock.unlock();
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy