com.google.gwt.validation.client.impl.GwtBeanDescriptorImpl 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 com.google.gwt.validation.client.impl.metadata.BeanMetadata;
import com.google.gwt.validation.client.impl.metadata.ValidationGroupsMetadata;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
/**
* Abstract BeanDescriptor for use by generated {@link GwtBeanDescriptor}.
*
* Subclasses are expected to call setDescriptorMap from the constructor.
*
* @param the bean Type
*/
public final class GwtBeanDescriptorImpl implements GwtBeanDescriptor {
/**
* Builder for {@link GwtBeanDescriptor}s.
*
* @param the bean Type
*/
public static final class Builder {
private final Class clazz;
private final Map descriptorMap =
new HashMap();
private final Set> constraints =
new HashSet>();
private boolean isConstrained;
private BeanMetadata beanMetadata;
private Builder(Class clazz) {
this.clazz = clazz;
}
public Builder add(
ConstraintDescriptorImpl extends Annotation> constraintDescriptor) {
constraints.add(constraintDescriptor);
return this;
}
public GwtBeanDescriptorImpl build() {
return new GwtBeanDescriptorImpl(clazz, isConstrained, descriptorMap, beanMetadata,
constraints);
}
public Builder put(String key, PropertyDescriptorImpl value) {
descriptorMap.put(key, value.shallowCopy());
return this;
}
public Builder setBeanMetadata(BeanMetadata beanMetadata) {
this.beanMetadata = beanMetadata;
return this;
}
public Builder setConstrained(boolean isConstrained) {
this.isConstrained = isConstrained;
return this;
}
}
public static Builder builder(Class clazz) {
return new Builder(clazz);
}
private final Class clazz;
private final Set> constraints = new HashSet>();
private final Map descriptorMap = new HashMap();
private final boolean isBeanConstrained;
private final BeanMetadata beanMetadata;
private ValidationGroupsMetadata validationGroupsMetadata;
private GwtBeanDescriptorImpl(Class clazz, boolean isConstrained,
Map descriptorMap, BeanMetadata beanMetadata,
Set> constraints) {
this.clazz = clazz;
this.isBeanConstrained = isConstrained;
this.beanMetadata = beanMetadata;
this.descriptorMap.putAll(descriptorMap);
this.constraints.addAll(constraints);
}
@Override
public ConstraintFinder findConstraints() {
return new ConstraintFinderImpl(beanMetadata, validationGroupsMetadata, constraints);
}
@Override
public Set getConstrainedProperties() {
Collection props = descriptorMap.values();
for (PropertyDescriptorImpl prop : props) {
prop.setValidationGroupsMetadata(validationGroupsMetadata);
}
return new HashSet(props);
}
@Override
public Set> getConstraintDescriptors() {
return findConstraints().getConstraintDescriptors();
}
@Override
public PropertyDescriptor getConstraintsForProperty(String propertyName) {
PropertyDescriptorImpl propDesc = descriptorMap.get(propertyName);
if (propDesc != null) {
propDesc.setValidationGroupsMetadata(validationGroupsMetadata);
}
return propDesc;
}
@Override
public Class> getElementClass() {
return clazz;
}
@Override
public boolean hasConstraints() {
return !constraints.isEmpty();
}
@Override
public boolean isBeanConstrained() {
return isBeanConstrained;
}
@Override
public void setValidationGroupsMetadata(ValidationGroupsMetadata validationGroupsMetadata) {
// TODO(idol) Find some way to pass this via the constructor rather than after creation
this.validationGroupsMetadata = validationGroupsMetadata;
}
}