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

com.greenpepper.converter.PrimitiveDoubleArrayConverter Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version
package com.greenpepper.converter;

import java.util.ArrayList;
import java.util.List;

import com.greenpepper.TypeConversion;
import com.greenpepper.util.CollectionUtil;
import com.greenpepper.util.StringUtil;

/**
 * 

PrimitiveDoubleArrayConverter class.

* * @author oaouattara * @version $Id: $Id */ public class PrimitiveDoubleArrayConverter extends AbstractPrimitiveArrayConverter { /** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public Object parse( String value, Class type ) { String text = removeSquareBrackets( value ); List values = new ArrayList(); if (StringUtil.isBlank( text )) return CollectionUtil.toArray( values, type.getComponentType() ); String[] parts = text.split( separators ); for (String part : parts) { values.add( TypeConversion.parse( part.trim(), type.getComponentType() ) ); } return CollectionUtil.toPrimitiveDoubleArray( values ); } /** {@inheritDoc} */ @Override public String toString(Object value) { double[] array = (double[]) value; if (array.length == 0) return ""; StringBuilder builder = new StringBuilder(); builder.append( TypeConversion.toString(array[0]) ); if (array.length == 1) return builder.toString(); for (int i = 1; i < array.length; i++) { builder.append( ", " ).append( TypeConversion.toString( array[i] ) ); } return builder.toString(); } }