com.vaadin.data.util.converter
Class StringToCollectionConverter

java.lang.Object
  extended by com.vaadin.data.util.converter.StringToCollectionConverter
All Implemented Interfaces:
Converter<java.lang.String,java.util.Collection>, java.io.Serializable

public class StringToCollectionConverter
extends java.lang.Object
implements Converter<java.lang.String,java.util.Collection>

A converter that converts from String to Collection of tokens and back.

Allows to break a string into tokens using delimiter. Each token can be converted to its own model using provided converter.

Default constructor uses ", " as delimiter string and String for token types. Other constructors allow to configure delimiter and token types.

Since:
7.5.0
Author:
Vaadin Ltd
See Also:
Serialized Form

Nested Class Summary
static interface StringToCollectionConverter.CollectionFactory
          Collection factory.
static class StringToCollectionConverter.DefaultCollectionFactory
          Default collection factory implementation.
 
Nested classes/interfaces inherited from interface com.vaadin.data.util.converter.Converter
Converter.ConversionException
 
Constructor Summary
StringToCollectionConverter()
          Creates converter with ", " as delimiter and String as token model type in collection.
StringToCollectionConverter(Converter<java.lang.String,?> tokenConverter, java.lang.Class<?> tokenType)
          Creates converter with given tokenConverter for convert tokens and expected tokenType.
StringToCollectionConverter(java.lang.String delimiter)
          Creates converter with given delimiter and String as token model type in collection.
StringToCollectionConverter(java.lang.String delimiter, Converter<java.lang.String,?> tokenConverter, java.lang.Class<?> tokenClass)
          Creates converter with given tokenConverter for convert tokens and expected tokenType.
StringToCollectionConverter(java.lang.String delimiter, Converter<java.lang.String,?> tokenConverter, java.lang.Class<?> tokenClass, StringToCollectionConverter.CollectionFactory factory)
          Creates converter with given tokenConverter for convert tokens and expected tokenType.
 
Method Summary
 java.util.Collection convertToModel(java.lang.String value, java.lang.Class<? extends java.util.Collection> targetType, java.util.Locale locale)
          Converts the given value from target type to source type.
 java.lang.String convertToPresentation(java.util.Collection value, java.lang.Class<? extends java.lang.String> targetType, java.util.Locale locale)
          Converts the given value from source type to target type.
 java.lang.Class<java.util.Collection> getModelType()
          The source type of the converter.
 java.lang.Class<java.lang.String> getPresentationType()
          The target type of the converter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringToCollectionConverter

public StringToCollectionConverter()
Creates converter with ", " as delimiter and String as token model type in collection.


StringToCollectionConverter

public StringToCollectionConverter(java.lang.String delimiter)
Creates converter with given delimiter and String as token model type in collection.

Parameters:
delimiter - custom delimiter

StringToCollectionConverter

public StringToCollectionConverter(Converter<java.lang.String,?> tokenConverter,
                                   java.lang.Class<?> tokenType)
Creates converter with given tokenConverter for convert tokens and expected tokenType.

If tokenConverter is null then no conversation is done and String is used as token type in resulting model collection.

Parameters:
tokenConverter - converter for token
tokenType - expected token model type

StringToCollectionConverter

public StringToCollectionConverter(java.lang.String delimiter,
                                   Converter<java.lang.String,?> tokenConverter,
                                   java.lang.Class<?> tokenClass)
Creates converter with given tokenConverter for convert tokens and expected tokenType.

If tokenConverter is null then no conversation is done and String is used as token type in resulting model collection.

Parameters:
tokenConverter - converter for token
tokenType - expected token model type
delimiter - delimiter in presentation string

StringToCollectionConverter

public StringToCollectionConverter(java.lang.String delimiter,
                                   Converter<java.lang.String,?> tokenConverter,
                                   java.lang.Class<?> tokenClass,
                                   StringToCollectionConverter.CollectionFactory factory)
Creates converter with given tokenConverter for convert tokens and expected tokenType.

If tokenConverter is null then no conversation is done and String is used as token type in resulting model collection.

Parameters:
tokenConverter - converter for token
tokenType - expected token model type
delimiter - delimiter in presentation string
factory - factory to create resulting collection
Method Detail

getModelType

public java.lang.Class<java.util.Collection> getModelType()
Description copied from interface: Converter
The source type of the converter. Values of this type can be passed to Converter.convertToPresentation(Object, Class, Locale).

Specified by:
getModelType in interface Converter<java.lang.String,java.util.Collection>
Returns:
The source type

getPresentationType

public java.lang.Class<java.lang.String> getPresentationType()
Description copied from interface: Converter
The target type of the converter. Values of this type can be passed to Converter.convertToModel(Object, Class, Locale).

Specified by:
getPresentationType in interface Converter<java.lang.String,java.util.Collection>
Returns:
The target type

convertToModel

public java.util.Collection convertToModel(java.lang.String value,
                                           java.lang.Class<? extends java.util.Collection> targetType,
                                           java.util.Locale locale)
                                    throws Converter.ConversionException
Description copied from interface: Converter
Converts the given value from target type to source type.

A converter can optionally use locale to do the conversion.

A converter should in most cases be symmetric so chaining Converter.convertToPresentation(Object, Class, Locale) and Converter.convertToModel(Object, Class, Locale) should return the original value.

Specified by:
convertToModel in interface Converter<java.lang.String,java.util.Collection>
Parameters:
value - The value to convert, compatible with the target type. Can be null
targetType - The requested type of the return value
locale - The locale to use for conversion. Can be null.
Returns:
The converted value compatible with the source type
Throws:
Converter.ConversionException - If the value could not be converted

convertToPresentation

public java.lang.String convertToPresentation(java.util.Collection value,
                                              java.lang.Class<? extends java.lang.String> targetType,
                                              java.util.Locale locale)
                                       throws Converter.ConversionException
Description copied from interface: Converter
Converts the given value from source type to target type.

A converter can optionally use locale to do the conversion.

A converter should in most cases be symmetric so chaining Converter.convertToPresentation(Object, Class, Locale) and Converter.convertToModel(Object, Class, Locale) should return the original value.

Specified by:
convertToPresentation in interface Converter<java.lang.String,java.util.Collection>
Parameters:
value - The value to convert, compatible with the target type. Can be null
targetType - The requested type of the return value
locale - The locale to use for conversion. Can be null.
Returns:
The converted value compatible with the source type
Throws:
Converter.ConversionException - If the value could not be converted


Copyright © 2000-2014 Vaadin Ltd. All Rights Reserved.