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 CheckConstraint implementation that checks whether a string contains allowed characters only.

 
 public class LegalCharsetConstraint extends CheckConstraint<String> {
    
    
Allowed character set.
 
     protected final UnicodeSet charset;
    
    
Constructs a new LegalCharsetConstraint with the specified allowed character set.

Parameters:
charset Allowed character set.
Throws:
java.lang.IllegalArgumentException if the specified character set is null or empty.
 
     protected LegalCharsetConstraint(UnicodeSet charset) {
         Assert.assertTrue(!(charset == null || charset.equals(.)), "charset");
         this. = charset;
     }
    
    
Constructs a new LegalCharsetConstraint from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines empty character set.
 
     LegalCharsetConstraint(LegalCharset annotation) {
         this(UnicodeSet.fromElements(annotation.value()));
     }
    
    
Returns java.lang.String type.

Returns:
java.lang.String type.
 
     @Override
     public final Class<?> getType() {
         return String.class;
     }
    
    
Returns allowed character set.

Returns:
Allowed character set.
 
     public final UnicodeSet getCharset() {
         return ;
     }
    
    
Appends charset argument that contains allowed character set.

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("charset".toString());
         return true;
    }
    
    
Checks whether the specified string contains allowed characters only.

Parameters:
value String to be checked.
context Validation context.
Returns:
true if the specified string contains allowed characters only; false otherwise.
    @Override
    protected <T> boolean check(String valueValidationContext<T> context) {
        if (value == null)
            return true;
        int length = value.length();
        for (int i = 0; i < lengthi++)
            if (!.contains(value.charAt(i)))
                return false;
        return true;
    }
    
New to GrepCode? Check out our FAQ X