Skip to content

Commit

Permalink
HV-395 Switching to string for property path instead of Path instances
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Nov 22, 2010
1 parent 13ae33c commit e9a2e5d
Showing 1 changed file with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
public class ConstraintValidatorContextImpl implements ConstraintValidatorContext {

private final List<MessageAndPath> messageAndPaths = new ArrayList<MessageAndPath>( 3 );
private final PathImpl propertyPath;
private final String propertyPath;
private final ConstraintDescriptor<?> constraintDescriptor;
private boolean defaultDisabled;


public ConstraintValidatorContextImpl(PathImpl propertyPath, ConstraintDescriptor<?> constraintDescriptor) {
public ConstraintValidatorContextImpl(String propertyPath, ConstraintDescriptor<?> constraintDescriptor) {
this.propertyPath = propertyPath;
this.constraintDescriptor = constraintDescriptor;
}
Expand Down Expand Up @@ -72,22 +71,19 @@ public final List<MessageAndPath> getMessageAndPathList() {

class ErrorBuilderImpl implements ConstraintViolationBuilder {
private String messageTemplate;
private PathImpl propertyPath;
private String propertyPath;

ErrorBuilderImpl(String template, PathImpl path) {
ErrorBuilderImpl(String template, String path) {
messageTemplate = template;
propertyPath = path;
}

public NodeBuilderDefinedContext addNode(String name) {
PathImpl path;
if ( propertyPath.isRootPath() ) {
path = PathImpl.createNewPath( name );
}
else {
path = PathImpl.createShallowCopy( propertyPath );
path.addNode( new NodeImpl( name ) );
String path = propertyPath;
if ( !PathImpl.ROOT_PATH.equals( propertyPath ) ) {
path += PathImpl.PROPERTY_PATH_SEPARATOR;
}
path += name;
return new NodeBuilderImpl( messageTemplate, path );
}

Expand All @@ -99,15 +95,17 @@ public ConstraintValidatorContext addConstraintViolation() {

class NodeBuilderImpl implements ConstraintViolationBuilder.NodeBuilderDefinedContext {
private String messageTemplate;
private PathImpl propertyPath;
private String propertyPath;

NodeBuilderImpl(String template, PathImpl path) {
NodeBuilderImpl(String template, String path) {
messageTemplate = template;
propertyPath = path;
}

public ConstraintViolationBuilder.NodeBuilderCustomizableContext addNode(String name) {
propertyPath.addNode( new NodeImpl( name ) );
if ( name != null ) {
propertyPath += PathImpl.PROPERTY_PATH_SEPARATOR + name;
}
return new InIterableNodeBuilderImpl( messageTemplate, propertyPath );
}

Expand All @@ -119,20 +117,36 @@ public ConstraintValidatorContext addConstraintViolation() {

class InIterableNodeBuilderImpl implements ConstraintViolationBuilder.NodeBuilderCustomizableContext {
private String messageTemplate;
private PathImpl propertyPath;
private String propertyPath;

InIterableNodeBuilderImpl(String template, PathImpl path) {
InIterableNodeBuilderImpl(String template, String path) {
this.messageTemplate = template;
this.propertyPath = path;
}

public ConstraintViolationBuilder.NodeContextBuilder inIterable() {
this.propertyPath.getLeafNode().setInIterable( true );
return new InIterablePropertiesBuilderImpl( messageTemplate, propertyPath );
int lastPropertyIndex = propertyPath.lastIndexOf( PathImpl.PROPERTY_PATH_SEPARATOR );
StringBuilder builder = new StringBuilder();

if ( lastPropertyIndex != -1 ) {
builder = new StringBuilder();
builder.append( propertyPath.substring( 0, lastPropertyIndex ) );
builder.append( PathImpl.INDEX_OPEN );
builder.append( PathImpl.INDEX_CLOSE );
builder.append( PathImpl.PROPERTY_PATH_SEPARATOR );
builder.append( propertyPath.substring( lastPropertyIndex + 1 ) );
}
else {
builder.append( propertyPath );
builder.append( PathImpl.INDEX_OPEN );
builder.append( PathImpl.INDEX_CLOSE );
}

return new InIterablePropertiesBuilderImpl( messageTemplate, builder.toString() );
}

public ConstraintViolationBuilder.NodeBuilderCustomizableContext addNode(String name) {
propertyPath.addNode( new NodeImpl( name ) );
propertyPath += PathImpl.PROPERTY_PATH_SEPARATOR + name;
return this;
}

Expand All @@ -144,31 +158,45 @@ public ConstraintValidatorContext addConstraintViolation() {

class InIterablePropertiesBuilderImpl implements ConstraintViolationBuilder.NodeContextBuilder {
private String messageTemplate;
private PathImpl propertyPath;
private String propertyPath;

InIterablePropertiesBuilderImpl(String template, PathImpl path) {
InIterablePropertiesBuilderImpl(String template, String path) {
this.messageTemplate = template;
this.propertyPath = path;
}

public ConstraintViolationBuilder.NodeBuilderDefinedContext atKey(Object key) {
propertyPath.getLeafNode().setKey( key );
return new NodeBuilderImpl( messageTemplate, propertyPath );
StringBuilder builder = addKeyOrIndex( key );
return new NodeBuilderImpl( messageTemplate, builder.toString() );
}

public ConstraintViolationBuilder.NodeBuilderDefinedContext atIndex(Integer index) {
propertyPath.getLeafNode().setIndex( index );
return new NodeBuilderImpl( messageTemplate, propertyPath );
StringBuilder builder = addKeyOrIndex( index );
return new NodeBuilderImpl( messageTemplate, builder.toString() );
}

public ConstraintViolationBuilder.NodeBuilderCustomizableContext addNode(String name) {
propertyPath.addNode( new NodeImpl( name ) );
propertyPath += PathImpl.PROPERTY_PATH_SEPARATOR + name;
return new InIterableNodeBuilderImpl( messageTemplate, propertyPath );
}

public ConstraintValidatorContext addConstraintViolation() {
messageAndPaths.add( new MessageAndPath( messageTemplate, propertyPath ) );
return ConstraintValidatorContextImpl.this;
}

private StringBuilder addKeyOrIndex(Object key) {
int index = propertyPath.lastIndexOf( PathImpl.INDEX_OPEN );
StringBuilder builder = new StringBuilder();
builder.append( propertyPath.substring( 0, index ) );
builder.append( PathImpl.INDEX_OPEN );
builder.append( key );
builder.append( PathImpl.INDEX_CLOSE );
if ( propertyPath.lastIndexOf( PathImpl.PROPERTY_PATH_SEPARATOR ) != -1 ) {
builder.append( PathImpl.PROPERTY_PATH_SEPARATOR );
builder.append( propertyPath.substring( index + 3 ) );
}
return builder;
}
}
}

0 comments on commit e9a2e5d

Please sign in to comment.