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.List;
 import java.util.Map;
 
 
 
 public final class ArrayConverter<V> extends SequenceConverter<Object> {
    
    
Array type.
 
     private final Class<Objecttype;
    
    
Converter of array elements.
 
     private final Converter<V> converter;
    
    
Constructs a new ArrayConverter with the specified converter and tokenizer of array elements.

Parameters:
converter Converter of array elements.
tokenizer Tokenizer of array elements.
Throws:
java.lang.IllegalArgumentException if the specified converter or tokenizer is null.
 
     ArrayConverter(Converter<V> converterTokenizer tokenizer) {
         super(tokenizer);
         this. = Assert.notNull(converter"converter");
         this. = Types.arrayTypeOf(converter.getType());
     }
    
    
Returns array type.

Returns:
Array type.
 
     @Override
     public Class<ObjectgetType() {
         return ;
     }
    
    
Appends converter argument that contains converter of array elements.

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("converter");
         return true;
     }
    
    
Converts string representations of array elements into array.

Parameters:
tokens String representations of array elements.
context Validation context.
Returns:
Decoded array.
Throws:
MalformedValueException if conversion of array elements fails.
    @Override
    protected <T> Object doDecode(String[] tokensValidationContext<T> context,
            List<MalformedValueExceptionviolations) {
        Object array = Array.newInstance(.getType(), tokens.length);
        for (int i = 0; i < tokens.lengthi++) {
            try {
                context.setCurrentIndex(i);
                Array.set(arrayi.decode(tokens[i], context));
            } catch (MalformedValueException e) {
                violations.add(e);
            }
        }
        return array;
    }
    
    
Converts array into string representation of array elements.

Parameters:
value Array to be encoded.
context Validation context.
Returns:
String representation of array elements.
    @Override
    @SuppressWarnings("unchecked")
    public <T> String encode(Object valueValidationContext<T> context) {
        int count = value == null ? 0 : Array.getLength(value);
        if (count == 0)
            return .encode(.context);
        String[] tokens = new String[count];
        for (int i = 0; i < counti++)
            tokens[i] = .encode((V) Array.get(valuei), context);
        return .encode(tokenscontext);
    }
    
New to GrepCode? Check out our FAQ X