br.com.anteros.bean.validation.ConstraintFinderImpl 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.lang.annotation.ElementType;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import br.com.anteros.bean.validation.groups.Group;
import br.com.anteros.bean.validation.groups.Groups;
import br.com.anteros.bean.validation.groups.GroupsComputer;
import br.com.anteros.bean.validation.model.MetaBean;
import br.com.anteros.validation.api.metadata.ConstraintDescriptor;
import br.com.anteros.validation.api.metadata.ElementDescriptor;
import br.com.anteros.validation.api.metadata.Scope;
import br.com.anteros.validation.api.metadata.ElementDescriptor.ConstraintFinder;
/**
* Description: Implementation of the fluent {@link ConstraintFinder} interface.
*/
final class ConstraintFinderImpl implements ElementDescriptor.ConstraintFinder {
private final MetaBean metaBean;
private final Set findInScopes;
private Set> constraintDescriptors;
/**
* Create a new ConstraintFinderImpl instance.
*
* @param metaBean
* @param constraintDescriptors
*/
ConstraintFinderImpl(MetaBean metaBean, Set> constraintDescriptors) {
this.metaBean = metaBean;
this.constraintDescriptors = constraintDescriptors;
this.findInScopes = new HashSet(Arrays.asList(Scope.values()));
}
/**
* {@inheritDoc}
*/
public ElementDescriptor.ConstraintFinder unorderedAndMatchingGroups(Class>... groups) {
Set> matchingDescriptors =
new HashSet>(constraintDescriptors.size());
Groups groupChain = new GroupsComputer().computeGroups(groups);
for (Group group : groupChain.getGroups()) {
if (group.isDefault()) {
// If group is default, check if it gets redefined
List expandedDefaultGroup = metaBean.getFeature(Jsr303Features.Bean.GROUP_SEQUENCE);
for (Group defaultGroupMember : expandedDefaultGroup) {
for (ConstraintValidation> descriptor : constraintDescriptors) {
if (isInScope(descriptor) && isInGroup(descriptor, defaultGroupMember)) {
matchingDescriptors.add(descriptor);
}
}
}
} else {
for (ConstraintValidation> descriptor : constraintDescriptors) {
if (isInScope(descriptor) && isInGroup(descriptor, group)) {
matchingDescriptors.add(descriptor);
}
}
}
}
return thisWith(matchingDescriptors);
}
/**
* {@inheritDoc}
*/
public ElementDescriptor.ConstraintFinder lookingAt(Scope scope) {
if (scope.equals(Scope.LOCAL_ELEMENT)) {
findInScopes.remove(Scope.HIERARCHY);
for (Iterator> it = constraintDescriptors.iterator(); it.hasNext();) {
ConstraintValidation> cv = it.next();
if (cv.getOwner() != metaBean.getBeanClass()) {
it.remove();
}
}
}
return this;
}
/**
* {@inheritDoc}
*/
public ElementDescriptor.ConstraintFinder declaredOn(ElementType... elementTypes) {
Set> matchingDescriptors =
new HashSet>(constraintDescriptors.size());
for (ElementType each : elementTypes) {
for (ConstraintValidation> descriptor : constraintDescriptors) {
if (isInScope(descriptor) && isAtElement(descriptor, each)) {
matchingDescriptors.add(descriptor);
}
}
}
return thisWith(matchingDescriptors);
}
private boolean isAtElement(ConstraintValidation> descriptor, ElementType each) {
return descriptor.getAccess().getElementType() == each;
}
private boolean isInScope(ConstraintValidation> descriptor) {
if (findInScopes.size() == Scope.values().length)
return true; // all scopes
if (metaBean != null) {
Class> owner = descriptor.getOwner();
for (Scope scope : findInScopes) {
switch (scope) {
case LOCAL_ELEMENT:
if (owner.equals(metaBean.getBeanClass()))
return true;
break;
case HIERARCHY:
if (!owner.equals(metaBean.getBeanClass()))
return true;
break;
}
}
}
return false;
}
private boolean isInGroup(ConstraintValidation> descriptor, Group group) {
return descriptor.getGroups().contains(group.getGroup());
}
private ElementDescriptor.ConstraintFinder thisWith(Set> matchingDescriptors) {
constraintDescriptors = matchingDescriptors;
return this;
}
/**
* {@inheritDoc}
*/
public Set> getConstraintDescriptors() {
return constraintDescriptors.isEmpty() ? Collections.> emptySet() : Collections
.> unmodifiableSet(constraintDescriptors);
}
/**
* {@inheritDoc}
*/
public boolean hasConstraints() {
return !constraintDescriptors.isEmpty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy