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

com.google.gwt.validation.client.impl.ConstraintDescriptorImpl Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * 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.lang.annotation.ElementType;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintValidator;
import javax.validation.Payload;
import javax.validation.metadata.ConstraintDescriptor;

/**
 * A immutable GWT implementation of {@link ConstraintDescriptor}.
 * 
 * @param  the constraint annotation to describe.
 */
public final class ConstraintDescriptorImpl implements
    ConstraintDescriptor {

  /**
   * Builder for {@link ConstraintDescriptorImpl}.
   *
   * @param  the constraint annotation to describe.
   */
  public static class Builder {
    private T annotation;
    private Set> groups;
    private Set> payload;
    private List>> constraintValidatorClasses;
    private Map attributes;
    private Set> composingConstraints =
        new HashSet>();
    private boolean reportAsSingleViolation;
    private ElementType elementType;
    private ConstraintOrigin definedOn;

    public Builder addComposingConstraint(
        ConstraintDescriptor composingConstraint) {
      this.composingConstraints.add(composingConstraint);
      return this;
    }

    public ConstraintDescriptorImpl build() {
      return new ConstraintDescriptorImpl(//
          annotation, //
          groups, //
          payload, //
          constraintValidatorClasses, //
          attributes, //
          composingConstraints, //
          reportAsSingleViolation, //
          elementType, //
          definedOn);
    }

    public Builder setAnnotation(T annotation) {
      this.annotation = annotation;
      return this;
    }

    public Builder setAttributes(Map attributes) {
      this.attributes = attributes;
      return this;
    }

    public Builder setConstraintValidatorClasses(
        Class>[] constraintValidatorClasses) {
      List>> list = Arrays.asList(constraintValidatorClasses);
      setConstraintValidatorClasses(list);
      return this;
    }

    public Builder setConstraintValidatorClasses(
        List>> constraintValidatorClasses) {
      this.constraintValidatorClasses = constraintValidatorClasses;
      return this;
    }

    public Builder setDefinedOn(ConstraintOrigin definedOn) {
      this.definedOn = definedOn;
      return this;
    }

    public Builder setElementType(ElementType elementType) {
      this.elementType = elementType;
      return this;
    }

    public Builder setGroups(Class[] classes) {
      setGroups(new HashSet>(Arrays.asList(classes)));
      return this;
    }

    public Builder setGroups(Set> groups) {
      this.groups = groups;
      return this;
    }

    public Builder setPayload(Class[] classes) {
      setPayload(new HashSet>(Arrays.asList(classes)));
      return this;
    }

    public Builder setPayload(Set> payload) {
      this.payload = payload;
      return this;
    }

    public Builder setReportAsSingleViolation(boolean reportAsSingleViolation) {
      this.reportAsSingleViolation = reportAsSingleViolation;
      return this;
    }
  }

  public static  Builder builder() {
    return new Builder();
  }

  private final T annotation;
  private final Set> groups;
  private final Set> payload;
  private final List>> constraintValidatorClasses;
  private final Map attributes;
  private final Set> composingConstraints;
  private final boolean reportAsSingleViolation;
  private final ElementType elementType;
  private final ConstraintOrigin definedOn;

  private ConstraintDescriptorImpl(
      T annotation,
      Set> groups,
      Set> payload,
      List>> constraintValidatorClasses,
      Map attributes,
      Set> composingConstraints,
      boolean reportAsSingleViolation,
      ElementType elementType,
      ConstraintOrigin definedOn) {
    super();
    this.annotation = annotation;
    this.groups = groups;
    this.payload = payload;
    this.constraintValidatorClasses = constraintValidatorClasses;
    this.attributes = attributes;
    this.composingConstraints = composingConstraints;
    this.reportAsSingleViolation = reportAsSingleViolation;
    this.elementType = elementType;
    this.definedOn = definedOn;
  }

  @Override
  public T getAnnotation() {
    return annotation;
  }

  @Override
  public Map getAttributes() {
    return attributes;
  }

  @Override
  public Set> getComposingConstraints() {
    return composingConstraints;
  }

  @Override
  public List>> getConstraintValidatorClasses() {
    return constraintValidatorClasses;
  }

  public ConstraintOrigin getDefinedOn() {
    return definedOn;
  }

  public ElementType getElementType() {
    return elementType;
  }

  @Override
  public Set> getGroups() {
    return groups;
  }

  @Override
  public Set> getPayload() {
    return payload;
  }

  @Override
  public boolean isReportAsSingleViolation() {
    return reportAsSingleViolation;
  }

  /**
   * For debugging only. Do not rely on the format. It can change at any time.
   */
  @Override
  public String toString() {
    return String.valueOf(annotation);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy