You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequiredFieldConfigurator.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.lang.annotation.Annotation;
  18. import javax.validation.constraints.NotNull;
  19. import javax.validation.constraints.Size;
  20. import com.vaadin.server.SerializablePredicate;
  21. /**
  22. * This interface represents a predicate which returns {@code true} if bound
  23. * field should be configured to have required indicator via
  24. * {@link HasValue#setRequiredIndicatorVisible(boolean)}.
  25. *
  26. * @see BeanValidationBinder
  27. * @see BeanValidationBinder#setRequiredConfigurator(RequiredFieldConfigurator)
  28. *
  29. * @author Vaadin Ltd
  30. * @since 8.0
  31. *
  32. */
  33. public interface RequiredFieldConfigurator
  34. extends SerializablePredicate<Annotation> {
  35. /**
  36. * Configurator which is aware of {@literal @NotNull} annotation presence
  37. * for a property.
  38. */
  39. public RequiredFieldConfigurator NOT_NULL = annotation -> annotation
  40. .annotationType().equals(NotNull.class);
  41. /**
  42. * Configurator which is aware of {@literal @NotEmpty} annotation presence
  43. * for a property.
  44. */
  45. public RequiredFieldConfigurator NOT_EMPTY = annotation -> annotation
  46. .annotationType().getName()
  47. .equals("org.hibernate.validator.constraints.NotEmpty") || annotation
  48. .annotationType().getName().equals("javax.validation.constraints.NotEmpty");
  49. /**
  50. * Configurator which is aware of {@literal Size} annotation with
  51. * {@code min()> 0} presence for a property.
  52. */
  53. public RequiredFieldConfigurator SIZE = annotation -> annotation
  54. .annotationType().equals(Size.class)
  55. && ((Size) annotation).min() > 0;
  56. /**
  57. * Default configurator which is combination of {@link #NOT_NULL},
  58. * {@link #NOT_EMPTY} and {@link #SIZE} configurators.
  59. */
  60. public RequiredFieldConfigurator DEFAULT = NOT_NULL.chain(NOT_EMPTY)
  61. .chain(SIZE);
  62. public default RequiredFieldConfigurator chain(
  63. RequiredFieldConfigurator configurator) {
  64. return descriptor -> test(descriptor) || configurator.test(descriptor);
  65. }
  66. }