Coverage Report - org.jtheque.core.utils.ui.constraints.MaxLengthConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
MaxLengthConstraint
0 %
0/15
0 %
0/2
1.167
 
 1  
 package org.jtheque.core.utils.ui.constraints;
 2  
 
 3  
 import org.jtheque.core.managers.error.JThequeError;
 4  
 import org.jtheque.core.utils.ui.ValidationUtils;
 5  
 
 6  
 import java.util.Collection;
 7  
 
 8  
 /*
 9  
  * This file is part of JTheque.
 10  
  *
 11  
  * JTheque is free software: you can redistribute it and/or modify
 12  
  * it under the terms of the GNU General Public License as published by
 13  
  * the Free Software Foundation, either version 3 of the License.
 14  
  *
 15  
  * JTheque is distributed in the hope that it will be useful,
 16  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  
  * GNU General Public License for more details.
 19  
  *
 20  
  * You should have received a copy of the GNU General Public License
 21  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 22  
  */
 23  
 
 24  
 /**
 25  
  * A constraint to set a max length.
 26  
  *
 27  
  * @author Baptiste Wicht
 28  
  */
 29  
 public final class MaxLengthConstraint implements Constraint {
 30  
     private final int maxLength;
 31  
     private final String fieldName;
 32  
     private final boolean canBenNull;
 33  
     private final boolean numerical;
 34  
 
 35  
     /**
 36  
      * Construct a new MaxLengthConstraint.
 37  
      *
 38  
      * @param maxLength  The maximum length of the field.
 39  
      * @param fieldName  The field name.
 40  
      * @param canBenNull Can the field be null ?
 41  
      * @param numerical  Must the field be numerical ?
 42  
      */
 43  
     public MaxLengthConstraint(int maxLength, String fieldName, boolean canBenNull, boolean numerical) {
 44  0
         super();
 45  
 
 46  0
         this.maxLength = maxLength;
 47  0
         this.fieldName = fieldName;
 48  0
         this.canBenNull = canBenNull;
 49  0
         this.numerical = numerical;
 50  0
     }
 51  
 
 52  
     @Override
 53  
     public int maxLength() {
 54  0
         return maxLength;
 55  
     }
 56  
 
 57  
     @Override
 58  
     public boolean mustBeNumerical() {
 59  0
         return numerical;
 60  
     }
 61  
 
 62  
     @Override
 63  
     public boolean canBeNullOrEmpty() {
 64  0
         return canBenNull;
 65  
     }
 66  
 
 67  
     @Override
 68  
     public boolean mustControlLength() {
 69  0
         return true;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public void validate(Object field, Collection<JThequeError> errors) {
 74  0
         CharSequence str = (CharSequence) field;
 75  
 
 76  0
         if (!canBenNull) {
 77  0
             ValidationUtils.rejectIfEmpty(str, fieldName, errors);
 78  
         }
 79  
 
 80  0
         ValidationUtils.rejectIfLongerThan(str, fieldName, maxLength, errors);
 81  0
     }
 82  
 }