org.hibernate.validator.internal.cfg.context.ConstraintMappingContextImplBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bean-validator Show documentation
Show all versions of bean-validator Show documentation
JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or .
*/
package org.hibernate.validator.internal.cfg.context;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.core.MetaConstraints;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
/**
* Base class for implementations of constraint mapping creational context types.
*
* @author Gunnar Morling
*/
abstract class ConstraintMappingContextImplBase extends ConstraintContextImplBase {
private final Set> constraints;
ConstraintMappingContextImplBase(DefaultConstraintMapping mapping) {
super( mapping );
this.constraints = newHashSet();
}
/**
* Returns the type of constraints hosted on the element configured by this creational context.
*
* @return the type of constraints hosted on the element configured by this creational context
*/
protected abstract ConstraintType getConstraintType();
protected DefaultConstraintMapping getConstraintMapping() {
return mapping;
}
/**
* Adds a constraint to the set of constraints managed by this creational context.
*
* @param constraint the constraint to add
*/
protected void addConstraint(ConfiguredConstraint constraint) {
constraints.add( constraint );
}
protected Set> getConstraints(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper,
ValueExtractorManager valueExtractorManager) {
if ( constraints == null ) {
return Collections.emptySet();
}
Set> metaConstraints = newHashSet();
for ( ConfiguredConstraint configuredConstraint : constraints ) {
metaConstraints.add( asMetaConstraint( configuredConstraint, constraintHelper, typeResolutionHelper, valueExtractorManager ) );
}
return metaConstraints;
}
private MetaConstraint asMetaConstraint(ConfiguredConstraint config, ConstraintHelper constraintHelper,
TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager) {
ConstraintDescriptorImpl constraintDescriptor = new ConstraintDescriptorImpl(
constraintHelper,
config.getLocation().getMember(),
config.createAnnotationDescriptor(),
config.getElementType(),
getConstraintType()
);
return MetaConstraints.create( typeResolutionHelper, valueExtractorManager, constraintDescriptor, config.getLocation() );
}
}