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

org.hibernate.validator.internal.metadata.location.AbstractPropertyConstraintLocation Maven / Gradle / Ivy

There is a newer version: 8.0.1.Final
Show newest version
/*
 * 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.metadata.location;

import java.lang.reflect.Type;

import org.hibernate.validator.internal.engine.path.PathImpl;
import org.hibernate.validator.internal.properties.Property;
import org.hibernate.validator.internal.properties.PropertyAccessor;
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;

/**
 * An abstract property constraint location.
 *
 * @author Marko Bekhta
 * @author Guillaume Smet
 */
public abstract class AbstractPropertyConstraintLocation implements ConstraintLocation {

	/**
	 * The property the constraint was defined on.
	 */
	private final T property;

	private final PropertyAccessor propertyAccessor;

	AbstractPropertyConstraintLocation(T property) {
		this.property = property;
		this.propertyAccessor = property.createAccessor();
	}

	@Override
	public Class getDeclaringClass() {
		return property.getDeclaringClass();
	}

	@Override
	public T getConstrainable() {
		return property;
	}

	public String getPropertyName() {
		return property.getPropertyName();
	}

	@Override
	public Type getTypeForValidatorResolution() {
		return property.getTypeForValidatorResolution();
	}

	@Override
	public void appendTo(ExecutableParameterNameProvider parameterNameProvider, PathImpl path) {
		path.addPropertyNode( property.getPropertyName() );
	}

	@Override
	public Object getValue(Object parent) {
		return propertyAccessor.getValueFrom( parent );
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + " [property=" + property + "]";
	}

	@Override
	public boolean equals(Object o) {
		if ( this == o ) {
			return true;
		}
		if ( o == null || getClass() != o.getClass() ) {
			return false;
		}

		AbstractPropertyConstraintLocation that = (AbstractPropertyConstraintLocation) o;

		if ( !property.equals( that.property ) ) {
			return false;
		}

		return true;
	}

	@Override
	public int hashCode() {
		return property.hashCode();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy