Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * 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 org.foxlabs.validation.constraint;
 
 
 import java.util.Map;
 
 
This class provides base implementation of the CheckConstraint that checks whether the size of a value is within allowed minimum and maximum bounds.

Parameters:
<V> The type of value to be validated
Author(s):
Fox Mulder
 
 public abstract class SizeConstraint<V> extends CheckConstraint<V> {
    
    
Allowed minimum size.
 
     private final int minSize;
    
    
Allowed maximum size.
 
     private final int maxSize;
    
    
Constructs a new SizeConstraint with the specified minimum and maximum sizes.

Parameters:
minSize Minimum allowed size.
maxSize Maximum allowed size.
Throws:
java.lang.IllegalArgumentException if the specified minimum or maximum size is negative.
 
     protected SizeConstraint(int minSizeint maxSize) {
         Assert.notNegative(minSize"minSize");
         Assert.notNegative(maxSize"maxSize");
         
         if (minSize < maxSize) {
             this. = minSize;
             this. = maxSize;
         } else {
             this. = maxSize;
             this. = minSize;
         }
     }
    
    
Returns allowed minimum size.

Returns:
Allowed minimum size.
 
     public final int getMinSize() {
         return ;
     }
    
    
Returns allowed maximum size.

Returns:
Allowed maximum size.
 
     public final int getMaxSize() {
         return ;
     }
    
    
Returns localized error message template.

Parameters:
context Validation context.
Returns:
Localized error message template.
 
     @Override
     public String getMessageTemplate(ValidationContext<?> context) {
         return context.resolveMessage(SizeConstraint.class.getName() +
                 ( > 0 ?  == . ? ".min" : "" :  < . ? ".max" : ""));
     }
    
    
Appends minSize and maxSize arguments that contain minimum and maximum allowed size respectively.

Parameters:
context Validation context.
arguments Arguments to be substituted into the error message template.
Returns:
true.
    @Override
    public boolean appendMessageArguments(ValidationContext<?> contextMap<StringObjectarguments) {
        super.appendMessageArguments(contextarguments);
        arguments.put("minSize");
        arguments.put("maxSize");
        return true;
    }
    
    
Checks whether the size of the specified value is within allowed minimum and maximum bounds.

Parameters:
value Value which size to be checked.
context Validation context.
Returns:
true if the size of the specified value is within allowed bounds; false otherwise.
    @Override
    protected final <T> boolean check(V valueValidationContext<T> context) {
        if (value == null)
            return true;
        int size = getSize(valuecontext);
        return size >=  && size <= 
    }
    
    
Returns size of the specified value.

Parameters:
value Value which size to be returned.
context Validation context.
Returns:
Size of the specified value.
    protected abstract <T> int getSize(V valueValidationContext<T> context);
    
    // StringType
    
    
This class provides SizeConstraint implementation for java.lang.String type.

    public static final class StringType extends SizeConstraint<String> {
        
        
Constructs a new SizeConstraint.StringType with the specified minimum and maximum lengths of the string.

Parameters:
minSize Minimum length of the string.
maxSize Maximum length of the string.
Throws:
java.lang.IllegalArgumentException if the specified minimum or maximum length is negative.
        StringType(int minSizeint maxSize) {
            super(minSizemaxSize);
        }
        
        
Constructs a new SizeConstraint.StringType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum length.
        StringType(MinSize annotation) {
            this(annotation.value(), .);
        }
        
        
Constructs a new SizeConstraint.StringType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative maximum length.
        StringType(MaxSize annotation) {
            this(0, annotation.value());
        }
        
        
Constructs a new SizeConstraint.StringType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum or maximum length.
        StringType(Size annotation) {
            this(annotation.min(), annotation.max());
        }
        
        
Returns java.lang.String type.

Returns:
java.lang.String type.
        @Override
        public Class<?> getType() {
            return String.class;
        }
        
        
Returns length of the specified string.

Parameters:
value String.
context Validation context.
Returns:
Length of the specified string.
        @Override
        protected <T> int getSize(String valueValidationContext<T> context) {
            return value.length();
        }
        
    }
    
    // ArrayType
    
    
This class provides SizeConstraint implementation for all array types.

    public static final class ArrayType extends SizeConstraint<Object> {
        
        
Constructs a new SizeConstraint.ArrayType with the specified minimum and maximum lengths of the array.

Parameters:
minSize Minimum length of the array.
maxSize Maximum length of the array.
Throws:
java.lang.IllegalArgumentException if the specified minimum or maximum length is negative.
        ArrayType(int minint max) {
            super(minmax);
        }
        
        
Constructs a new SizeConstraint.ArrayType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum length.
        ArrayType(MinSize annotation) {
            this(annotation.value(), .);
        }
        
        
Constructs a new SizeConstraint.ArrayType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative maximum length.
        ArrayType(MaxSize annotation) {
            this(0, annotation.value());
        }
        
        
Constructs a new SizeConstraint.ArrayType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum or maximum length.
        ArrayType(Size annotation) {
            this(annotation.min(), annotation.max());
        }
        
        
Returns java.lang.Object type.

Returns:
java.lang.Object type.
        @Override
        public Class<?> getType() {
            return Object.class;
        }
        
        
Returns length of the specified array.

Parameters:
value Array.
context Validation context.
Returns:
Length of the specified array.
        @Override
        protected <T> int getSize(Object valueValidationContext<T> context) {
            return Array.getLength(value);
        }
        
    }
    
    // CollectionType
    
    
This class provides SizeConstraint implementation for all JDK java.util.Collection types.

    public static final class CollectionType extends SizeConstraint<Collection<?>> {
        
        
Constructs a new SizeConstraint.CollectionType with the specified minimum and maximum sizes of the collection.

Parameters:
minSize Minimum size of the collection.
maxSize Maximum size of the collection.
Throws:
java.lang.IllegalArgumentException if the specified minimum or maximum size is negative.
        CollectionType(int minint max) {
            super(minmax);
        }
        
        
Constructs a new SizeConstraint.CollectionType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum size.
        CollectionType(MinSize annotation) {
            this(annotation.value(), .);
        }
        
        
Constructs a new SizeConstraint.CollectionType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative maximum size.
        CollectionType(MaxSize annotation) {
            this(0, annotation.value());
        }
        
        
Constructs a new SizeConstraint.CollectionType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum or maximum size.
        CollectionType(Size annotation) {
            this(annotation.min(), annotation.max());
        }
        
        
Returns java.util.Collection type.

Returns:
java.util.Collection type.
        @Override
        public Class<?> getType() {
            return Collection.class;
        }
        
        
Returns size of the specified collection.

Parameters:
value Collection.
context Validation context.
Returns:
Size of the specified collection.
        @Override
        protected <T> int getSize(Collection<?> valueValidationContext<T> context) {
            return value.size();
        }
        
    }
    
    // MapType
    
    
This class provides SizeConstraint implementation for all JDK java.util.Map types.

    public static final class MapType extends SizeConstraint<Map<?, ?>> {
        
        
Constructs a new SizeConstraint.MapType with the specified minimum and maximum sizes of the map.

Parameters:
minSize Minimum size of the map.
maxSize Maximum size of the map.
Throws:
java.lang.IllegalArgumentException if the specified minimum or maximum size is negative.
        MapType(int minint max) {
            super(minmax);
        }
        
        
Constructs a new SizeConstraint.MapType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum size.
        MapType(MinSize annotation) {
            this(annotation.value(), .);
        }
        
        
Constructs a new SizeConstraint.MapType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative maximum size.
        MapType(MaxSize annotation) {
            this(0, annotation.value());
        }
        
        
Constructs a new SizeConstraint.MapType from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines negative minimum or maximum size.
        MapType(Size annotation) {
            this(annotation.min(), annotation.max());
        }
        
        
Returns java.util.Map type.

Returns:
java.util.Map type.
        @Override
        public Class<?> getType() {
            return Map.class;
        }
        
        
Returns size of the specified map.

Parameters:
value Map.
context Validation context.
Returns:
Size of the specified map.
        @Override
        protected <T> int getSize(Map<?, ?> valueValidationContext<T> context) {
            return value.size();
        }
        
    }
    
New to GrepCode? Check out our FAQ X