net.entropysoft.transmorph.converters.collections.ArrayToCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of transmorph Show documentation
Show all versions of transmorph Show documentation
Java library to convert a Java object of one type into
an object of another type (with another signature, possibly
parameterized).
/*
* Copyright 2008-2009 the original author or authors.
*
* 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 net.entropysoft.transmorph.converters.collections;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.entropysoft.transmorph.ConversionContext;
import net.entropysoft.transmorph.ConverterException;
import net.entropysoft.transmorph.converters.AbstractContainerConverter;
import net.entropysoft.transmorph.type.TypeReference;
/**
* Converter used when source object is an array and destination type is
* Collection
*
* @author Cedric Chabanois (cchabanois at gmail.com)
*
*/
public class ArrayToCollection extends AbstractContainerConverter {
private Class extends Set> defaultSetClass = HashSet.class;
private Class extends List> defaultListClass = ArrayList.class;
public ArrayToCollection() {
this.useObjectPool = true;
}
public Class extends Set> getDefaultSetClass() {
return defaultSetClass;
}
public void setDefaultSetClass(Class extends Set> defaultSetClass) {
this.defaultSetClass = defaultSetClass;
}
public Class extends List> getDefaultListClass() {
return defaultListClass;
}
public void setDefaultListClass(Class extends List> defaultListClass) {
this.defaultListClass = defaultListClass;
}
public Object doConvert(ConversionContext context, Object sourceObject,
TypeReference> destinationType) throws ConverterException {
if (sourceObject == null) {
return null;
}
Collection