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.converter;
 
 import java.util.Map;
 
 
This class provides Converter implementation for the java.lang.Boolean type.

 
 public final class BooleanConverter extends AbstractConverter<Boolean> {
    
    
BooleanConverter primitive type instance.
 
     public static final BooleanConverter SIMPLE = new BooleanConverter(
             .Boolean.class.getName());
    
    
BooleanConverter object type instance.
 
     public static final BooleanConverter OBJECT = new BooleanConverter(
             Boolean.classBoolean.class.getName());
    
    
Default string representations of boolean constants (odd elements are TRUE constants, even elements are FALSE constants).
 
     private static final String[] DEFAULT_CONSTANTS = new String[]{
         "true""false""yes""no""y""n""on""off""1""0"
     };
    
    
Boolean type (can be either primitive or object type).
 
     private final Class<Booleantype;
    
    
Message key for TRUE constant.
 
     private final String trueKey;
    
    
Message key for FALSE constant.
 
     private final String falseKey;
    
    
Constructs a new BooleanConverter with the specified boolean type and prefix.

Parameters:
type Boolean type.
prefix Prefix of message keys of boolean constants.
Throws:
java.lang.IllegalArgumentException if the specified type or prefix is null.
 
     BooleanConverter(Class<BooleantypeString prefix) {
         Assert.notNull(prefix"prefix");
         this. = Assert.notNull(type"type");
         this. = prefix + ".TRUE";
         this. = prefix + ".FALSE";
     }
    
    
Returns java.lang.Boolean type.

Returns:
java.lang.Boolean type.
 
     @Override
     public Class<BooleangetType() {
         return ;
     }
    
    
Appends constants argument that contains set of allowed boolean constants.

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("constants"context.isLocalizedConvert()
                ? new String[]{doEncode(.context), doEncode(.context)}
                : );
        return true;
    }
    
    
Converts string representation of boolean value into java.lang.Boolean object.

Parameters:
value String representation of boolean value.
context Validation context.
Returns:
Decoded java.lang.Boolean object.
Throws:
MalformedValueException if the specified string could not be parsed as a boolean.
    @Override
    protected <T> Boolean doDecode(String valueValidationContext<T> context) {
        if (context.isLocalizedConvert()) {
            if (value.equalsIgnoreCase(doEncode(.context)))
                return .;
            if (value.equalsIgnoreCase(doEncode(.context)))
                return .;
        }
        
        for (int i = 0; i < .i++)
            if ([i].equalsIgnoreCase(value))
                return i % 2 == 0 ? . : .;
        
        throw new MalformedValueException(thiscontextvalue);
    }
    
    
Converts java.lang.Boolean object into string representation.

Parameters:
value java.lang.Boolean object to be encoded.
context Validation context.
Returns:
String representation of boolean value.
    @Override
    protected <T> String doEncode(Boolean valueValidationContext<T> context) {
        if (context.isLocalizedConvert()) {
            try {
                return context.resolveMessage(value ?  : );
            } catch (MissingResourceException e) {}
        }
        
        return [value ? 0 : 1];
    }
    
New to GrepCode? Check out our FAQ X