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 CollectionConverter<V> extends SequenceConverter<Collection<V>> {
    
    
Collection type.
 
     private final Class<Collection<V>> type;
    
    
Converter of collection elements.
 
     private final Converter<V> converter;
    
    
Constructs a new CollectionConverter with the specified collection type, converter and tokenizer of collection elements.

Parameters:
type Collection type.
converter Converter of collection elements.
tokenizer Tokenizer of collection elements.
Throws:
java.lang.IllegalArgumentException if the specified type or converter or tokenizer is null.
 
     CollectionConverter(Class<? extends Collection<?>> type,
             Converter<V> converterTokenizer tokenizer) {
         super(tokenizer);
         this. = Types.cast(Assert.notNull(type"type"));
         this. = Assert.notNull(converter"converter");
     }
    
    
Returns collection type.

Returns:
Collection type.
 
     @Override
     public Class<Collection<V>> getType() {
         return ;
     }
    
    
Appends converter argument that contains converter of collection 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 collection elements into collection.

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

Parameters:
value Collection to be encoded.
context Validation context.
Returns:
String representation of collection elements.
    @Override
    public <T> String encode(Collection<V> valueValidationContext<T> context) {
        int count = value == null ? 0 : value.size();
        if (count == 0)
            return .encode(.context);
        String[] tokens = new String[count];
        Iterator<V> itr = value.iterator();
        for (int i = 0; i < tokens.lengthi++)
            tokens[i] = .encode(itr.next(), context);
        return .encode(tokenscontext);
    }
    
New to GrepCode? Check out our FAQ X