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;
This class provides CorrectConstraint implementation that allows to cut length of a string.

Author(s):
Fox Mulder
See also:
MaxLength
ConstraintFactory.maxLength(int)
public final class MaxLengthConstraint extends CorrectConstraint<String> {
    
    
Maximum string length.
    private final int limit;
    
    
Constructs a new MaxLengthConstraint with the specified maximum string length.

Parameters:
limit Maximum string length.
Throws:
java.lang.IllegalArgumentException if the specified limit is negative or zero.
    MaxLengthConstraint(int limit) {
        this. = Assert.notNegativeOrZero(limit"limit");
    }
    
    
Constructs a new MaxLengthConstraint from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines maximum string length that is negative or zero.
    MaxLengthConstraint(MaxLength annotation) {
        this(annotation.value());
    }
    
    
Returns maximum string length.

Returns:
Maximum string length.
    public int getLimit() {
        return ;
    }
    
    
Returns java.lang.String type.

Returns:
java.lang.String type.
    @Override
    public Class<?> getType() {
        return String.class;
    }
    
    
Returns substring of maximum length for the specified string or string as is if its length is not greater than maximum length.

Parameters:
value Source string.
context Validation context.
Returns:
Substring of maximum length for the specified string or string as is if its length is not greater than maximum length.
    @Override
    public <T> String validate(String valueValidationContext<T> context) {
        return value == null ? null : value.length() >  ? value.substring(0, ) : value;
    }
    
New to GrepCode? Check out our FAQ X