All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.convert.ToArrayConvertor Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
package org.onetwo.common.convert;

import java.lang.reflect.Array;

import org.onetwo.common.utils.LangUtils;
import org.onetwo.common.utils.StringUtils;

public class ToArrayConvertor extends AbstractWithConvertorTypeConvert{

	public ToArrayConvertor(Convertor convertor) {
		super(LangUtils.EMPTY_ARRAY, convertor);
	}

	@Override
	public Object doConvert(Object source, Class componentType) {
//		if(source==null)
//			return LangUtils.EMPTY_STRING_ARRAY;
		
		Object result = LangUtils.EMPTY_ARRAY;
		if(source.getClass().isArray()){
			int length = Array.getLength(source);
			result = Array.newInstance(componentType, length);
			for(int i=0; i c = source.getClass();
		String[] strs = LangUtils.EMPTY_STRING_ARRAY;
		if(String.class==c){
			String str = source.toString();
			if(str.indexOf(',')!=-1){
				strs = StringUtils.split(str, ',');
			}else{
				strs = new String[]{str};
			}
		}
		
		return strs;
	}
	
	private  T[] asArray(Object source, Class numberClass){
		Class c = source.getClass();
		T[] longs;
		if(String.class==c){
			String[] strs = convertor.convert(source, Object[].class, String.class);
			longs = (T[])Array.newInstance(numberClass, strs.length);
			for(int i=0; i