Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HV-1928] reduces calls to isSubPathOf #1333

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,21 @@ private static boolean isValidJavaIdentifier(String identifier) {
}
return true;
}

public boolean isSubPathOrContains(PathImpl other) {

// prefetch contant return values
int oSize = other.nodeList.size();
// calling Math.min will reduce speed significantly
int mySize = nodeList.size() < oSize
? nodeList.size()
: oSize;

for ( int i = 0; i < mySize; i++ ) {
if ( !nodeList.get( i ).equals( other.nodeList.get( i ) ) ) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -48,6 +47,7 @@
* @author Gunnar Morling
* @author Guillaume Smet
* @author Marko Bekhta
* @author Thomas Strauß
*/
abstract class AbstractValidationContext<T> implements BaseBeanValidationContext<T> {

Expand Down Expand Up @@ -343,31 +343,22 @@ private boolean isAlreadyValidatedForPath(Object value, PathImpl path) {
return false;
}

if ( path.isRootPath() ) {
return true;
}

// Since this isAlreadyValidatedForPath(..) is only applicable for an object that is about to be cascaded into,
// it means that the new path we are testing cannot be a root path; also since we are cascading into inner
// objects, i.e. going further from the object tree root, it means that the new path cannot be shorter than
// the ones we've already encountered.
for ( PathImpl p : pathSet ) {
if ( path.isRootPath() || p.isRootPath() || isSubPathOf( path, p ) || isSubPathOf( p, path ) ) {
if ( p.isSubPathOrContains( path ) ) {
thst71 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}

return false;
}

private boolean isSubPathOf(Path p1, Path p2) {
Iterator<Path.Node> p1Iter = p1.iterator();
Iterator<Path.Node> p2Iter = p2.iterator();
while ( p1Iter.hasNext() ) {
Path.Node p1Node = p1Iter.next();
if ( !p2Iter.hasNext() ) {
return false;
}
Path.Node p2Node = p2Iter.next();
if ( !p1Node.equals( p2Node ) ) {
return false;
}
}
return true;
}

private boolean isAlreadyValidatedForCurrentGroup(Object value, Class<?> group) {
return getInitializedProcessedGroupUnits().contains( new BeanGroupProcessedUnit( value, group ) );
}
Expand Down Expand Up @@ -434,6 +425,7 @@ private static final class BeanPathMetaConstraintProcessedUnit {

@Override
public boolean equals(Object o) {
// null check intentionally left out
if ( this == o ) {
return true;
}
Expand Down Expand Up @@ -482,6 +474,7 @@ private static final class BeanGroupProcessedUnit {

@Override
public boolean equals(Object o) {
// null check intentionally left out
if ( this == o ) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Hardy Ferentschik
* @author Gunnar Morling
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
* @author Thomas Strauß
*/
public class PathImplTest {

Expand Down Expand Up @@ -166,6 +167,28 @@ public void testEmptyString() {
assertTrue( path.iterator().hasNext() );
}

@Test
public void testIsSubPathOrContains() {
PathImpl rootPath = PathImpl.createPathFromString( "" );
PathImpl subPath = PathImpl.createPathFromString( "annotation" );
PathImpl middlePath = PathImpl.createPathFromString( "annotation.property" );
PathImpl middlePath2 = PathImpl.createPathFromString( "annotation.property[2]" );
PathImpl middlePath3 = PathImpl.createPathFromString( "annotation.property[3]" );
PathImpl fullPath3 = PathImpl.createPathFromString( "annotation.property[3].element" );
PathImpl fullPath4 = PathImpl.createPathFromString( "annotation.property[4].element" );

assertTrue( rootPath.isSubPathOrContains( middlePath ), "root path is in every path" );
assertTrue( middlePath.isSubPathOrContains( rootPath ), "every path contains the root path" );
assertTrue( subPath.isSubPathOrContains( middlePath ), "bean is subpath of its properties" );
assertTrue( middlePath.isSubPathOrContains( subPath ), "a property is an extension of its bean" );
assertTrue( subPath.isSubPathOrContains( fullPath3 ), "bean is subpath of its tree" );
assertTrue( subPath.isSubPathOrContains( fullPath4 ), "bean is subpath of its tree, for every array index" );
assertFalse( middlePath.isSubPathOrContains( fullPath3 ), "property is not a subpath of an array" );
assertFalse( middlePath3.isSubPathOrContains( fullPath3 ), "array property is not a subpath of a property of a bean in an array" );
assertFalse( middlePath2.isSubPathOrContains( fullPath3 ), "array element is not a subpath of another element's children" );
assertFalse( fullPath3.isSubPathOrContains( fullPath4 ), "different array elements are not subpaths of the other" );
}

@Test
public void testNonStringMapKey() {
Validator validator = ValidatorUtil.getValidator();
Expand Down