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

org.sfm.csv.impl.CellValueReaderFactoryImpl Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.csv.impl;

import org.sfm.csv.CellValueReader;
import org.sfm.csv.CellValueReaderFactory;
import org.sfm.csv.CsvColumnDefinition;
import org.sfm.csv.impl.cellreader.*;
import org.sfm.csv.impl.cellreader.joda.JodaTimeCellValueReaderHelper;
import org.sfm.csv.ParsingContextFactoryBuilder;
import org.sfm.reflect.TypeHelper;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public final class CellValueReaderFactoryImpl implements CellValueReaderFactory {

	private static final Map, CellValueReader> READERS = new HashMap, CellValueReader>();

	static {
		READERS.put(boolean.class, new BooleanCellValueReaderImpl());
		READERS.put(byte.class, new ByteCellValueReaderImpl());
		READERS.put(char.class, new CharCellValueReaderImpl());
		READERS.put(short.class, new ShortCellValueReaderImpl());
		READERS.put(int.class, new IntegerCellValueReaderImpl());
		READERS.put(long.class, new LongCellValueReaderImpl());
		READERS.put(float.class, new FloatCellValueReaderImpl());
		READERS.put(double.class, new DoubleCellValueReaderImpl());
		READERS.put(Boolean.class, new BooleanCellValueReaderImpl());
		READERS.put(Byte.class, new ByteCellValueReaderImpl());
		READERS.put(Character.class, new CharCellValueReaderImpl());
		READERS.put(Short.class, new ShortCellValueReaderImpl());
		READERS.put(Integer.class, new IntegerCellValueReaderImpl());
		READERS.put(Long.class, new LongCellValueReaderImpl());
		READERS.put(Float.class, new FloatCellValueReaderImpl());
		READERS.put(Double.class, new DoubleCellValueReaderImpl());
		READERS.put(String.class,    new StringCellValueReader());
		READERS.put(Object.class,    new StringCellValueReader());
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public 

CellValueReader

getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder parsingContextFactoryBuilder) { Class propertyClass = TypeHelper.toClass(propertyType); CellValueReader

reader; if (propertyClass.equals(Date.class)) { DateCellValueReader dateCellValueReader = new DateCellValueReader(index, columnDefinition.dateFormat(), columnDefinition.getTimeZone()); reader = (CellValueReader

) dateCellValueReader; parsingContextFactoryBuilder.addParsingContextProvider(index, dateCellValueReader); } else if (Calendar.class.equals(propertyClass)) { CalendarCellValueReader calendarCellValueReader = new CalendarCellValueReader(index, columnDefinition.dateFormat(), columnDefinition.getTimeZone()); reader = (CellValueReader

) calendarCellValueReader; parsingContextFactoryBuilder.addParsingContextProvider(index, calendarCellValueReader); } else if (Enum.class.isAssignableFrom(propertyClass)) { reader = new EnumCellValueReader(propertyClass); } else { reader = (CellValueReader

) JodaTimeCellValueReaderHelper.getReader(propertyClass, columnDefinition); } if (reader == null) { reader = getCellValueTransformer(propertyClass); } return reader; } private SimpleDateFormat getSimpleDateFormat(CsvColumnDefinition columnDefinition) { SimpleDateFormat sdf = new SimpleDateFormat(columnDefinition.dateFormat()); sdf.setTimeZone(columnDefinition.getTimeZone()); return sdf; } @SuppressWarnings("unchecked") private

CellValueReader

getCellValueTransformer(Class propertyType) { return (CellValueReader

) READERS.get(propertyType); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy