Amakesh

Untitled

Apr 2nd, 2022 (edited)
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import javax.validation.ConstraintValidator;
  2. import javax.validation.ConstraintValidatorContext;
  3. import java.util.List;
  4. import java.util.Objects;
  5.  
  6.  
  7. public class CapacityValidator implements ConstraintValidator<Capacity, List<Integer>> {
  8.  
  9.     private int min;
  10.    // private int max;
  11.  
  12.     @Override
  13.     public void initialize(Capacity constraintAnnotation) {
  14.         this.min = constraintAnnotation.min();
  15.         System.out.println("Init");
  16.        // this.max = constraintAnnotation.max();
  17.     }
  18.  
  19.     @Override
  20.     public boolean isValid(List<Integer> list, ConstraintValidatorContext constraintValidatorContext) {
  21.  
  22.  
  23.            if(list.size() != this.min){
  24.               return false;
  25.            }
  26.            if(list.stream().noneMatch(Objects::nonNull)){
  27.                return false;
  28.            }
  29.            if(list.size() == this.min && list.stream().allMatch(Objects::nonNull)){
  30.                return true;
  31.            }
  32.    
  33.         return false;
  34.     }
  35. }
  36.  
Add Comment
Please, Sign In to add comment