com.google.gwt.validation.client.impl.ConstraintValidatorContextImpl Maven / Gradle / Ivy
/*
* Copyright 2010 Google Inc.
*
* 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 com.google.gwt.validation.client.impl;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext;
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext;
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeContextBuilder;
import javax.validation.ConstraintViolation;
import javax.validation.metadata.ConstraintDescriptor;
/**
* EXPERIMENTAL and subject to change. Do not use this in
* production code.
*
* GWT safe immutable implementation of {@link ConstraintValidatorContext}
*
* These objects are very short lived.
*
* @param the constraint being validated
* @param the type of object being validated
*/
public class ConstraintValidatorContextImpl implements
ConstraintValidatorContext {
/**
* Builder for {@link ConstraintValidatorContextImpl}.
*/
public final class ConstraintViolationBuilderImpl implements
ConstraintViolationBuilder {
private ConstraintValidatorContextImpl context;
private String messageTemplate;
/**
* @param constraintValidatorContextImpl
* @param messageTemplate
*/
public ConstraintViolationBuilderImpl(
ConstraintValidatorContextImpl constraintValidatorContextImpl,
String messageTemplate) {
this.context = constraintValidatorContextImpl;
this.messageTemplate = messageTemplate;
}
public ConstraintValidatorContext addConstraintViolation() {
messages.add(new MessageAndPath(context.basePath, messageTemplate));
return context;
}
public NodeBuilderDefinedContext addNode(String name) {
return new NodeBuilderDefinedContextImpl(this, messageTemplate,
basePath.append(name));
}
}
/**
* Immutable GWT safe {@link NodeBuilderCustomizableContext}.
*/
public final class NodeBuilderCustomizableContextImpl implements
NodeBuilderCustomizableContext {
private final String messageTemplate;
private final ConstraintViolationBuilderImpl parent;
private final PathImpl path;
/**
* @param parent
* @param messageTemplate
* @param append
*/
public NodeBuilderCustomizableContextImpl(
ConstraintViolationBuilderImpl parent, String messageTemplate,
PathImpl path) {
this.parent = parent;
this.messageTemplate = messageTemplate;
this.path = path;
}
public ConstraintValidatorContext addConstraintViolation() {
return null;
}
public NodeBuilderCustomizableContext addNode(String name) {
return this;
}
public NodeContextBuilder inIterable() {
return new NodeContextBuilderImpl(path, messageTemplate, parent);
}
}
/**
* Immutable GWT safe {@link NodeBuilderDefinedContext}.
*/
public final class NodeBuilderDefinedContextImpl implements
NodeBuilderDefinedContext {
private final String messageTemplate;
private final ConstraintViolationBuilderImpl parent;
private final PathImpl path;
/**
* @param constraintViolationBuilderImpl
* @param messageTemplate
* @param append
*/
public NodeBuilderDefinedContextImpl(ConstraintViolationBuilderImpl parent,
String messageTemplate, PathImpl path) {
this.parent = parent;
this.messageTemplate = messageTemplate;
this.path = path;
}
public ConstraintValidatorContext addConstraintViolation() {
ConstraintViolation violation = ConstraintViolationImpl. builder().setPropertyPath(
path).setMessageTemplate(messageTemplate).build();
parent.context.violations.add(violation);
return parent.context;
}
public NodeBuilderCustomizableContext addNode(String name) {
return new NodeBuilderCustomizableContextImpl(parent, messageTemplate,
path.append(name));
}
}
/**
* Immutable GWT safe {@link NodeContextBuilder}.
*/
public final class NodeContextBuilderImpl implements NodeContextBuilder {
private final String messageTemplate;
private final ConstraintViolationBuilderImpl parent;
private final PathImpl path;
public NodeContextBuilderImpl(PathImpl path, String messageTemplate,
ConstraintViolationBuilderImpl parent) {
super();
this.path = path;
this.messageTemplate = messageTemplate;
this.parent = parent;
}
public ConstraintValidatorContext addConstraintViolation() {
return null;
}
public NodeBuilderCustomizableContext addNode(String name) {
return new NodeBuilderCustomizableContextImpl(parent, messageTemplate,
path.append(name));
}
public NodeBuilderDefinedContext atIndex(Integer index) {
return new NodeBuilderDefinedContextImpl(parent, messageTemplate,
path.appendIndex(null, index.intValue()));
}
public NodeBuilderDefinedContext atKey(Object key) {
return new NodeBuilderDefinedContextImpl(parent, messageTemplate,
path.appendKey(null, key));
}
}
private final PathImpl basePath;
private final ConstraintDescriptor descriptor;
private boolean disableDefault;
private final Set> violations = new HashSet>();
private final HashSet messages = new HashSet();
public ConstraintValidatorContextImpl(PathImpl path,
ConstraintDescriptor descriptor) {
super();
this.basePath = path;
this.descriptor = descriptor;
}
public ConstraintViolationBuilder buildConstraintViolationWithTemplate(
String messageTemplate) {
ConstraintViolationBuilderImpl builder = new ConstraintViolationBuilderImpl(
this, messageTemplate);
return builder;
}
public void disableDefaultConstraintViolation() {
disableDefault = true;
}
public String getDefaultConstraintMessageTemplate() {
return (String) descriptor.getAttributes().get("message");
}
public Set getMessageAndPaths() {
if (!disableDefault) {
messages.add(new MessageAndPath(this.basePath, this
.getDefaultConstraintMessageTemplate()));
}
return messages;
}
public Set> getViolations() {
return violations;
}
}