Last active
August 29, 2015 14:10
-
-
Save anatollupacescu/14a70115303991029054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serioja; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import com.fasterxml.jackson.databind.MappingIterator; | |
import com.fasterxml.jackson.dataformat.csv.CsvMapper; | |
import com.fasterxml.jackson.dataformat.csv.CsvParser; | |
import com.google.common.base.MoreObjects; | |
import com.google.common.base.Strings; | |
import com.google.common.collect.ImmutableMap; | |
import com.google.common.collect.Maps; | |
/* | |
depends on Guava and jackson: | |
<dependency> | |
<groupId>com.fasterxml.jackson.dataformat</groupId> | |
<artifactId>jackson-dataformat-csv</artifactId> | |
<version>2.4.0</version> | |
</dependency> | |
*/ | |
public class CsvFileToObjectListMapper { | |
static class Pojo { | |
private String firstName; | |
private String lastName; | |
private Integer age; | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public Integer getAge() { | |
return age; | |
} | |
public void setAge(Integer age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return MoreObjects.toStringHelper(this.getClass()) | |
.add("firstName", firstName) | |
.add("lastName", lastName) | |
.add("age", age).toString(); | |
} | |
} | |
interface Testable { | |
public Object test(String obj, String columnName) throws Exception; | |
} | |
private static class IntegerTest implements Testable { | |
@Override | |
public Integer test(String value, String columnName) throws Exception { | |
Integer result = null; | |
try { | |
result = Integer.valueOf(value); | |
} catch (Exception e) { | |
throw new IllegalArgumentException("Could not cast to integer: " + value); | |
} | |
return result; | |
} | |
} | |
private static class StringTest implements Testable { | |
private boolean testNotNull = false; | |
private Pattern pattern; | |
public StringTest() { | |
} | |
public StringTest(boolean doTestOnNull) { | |
this.testNotNull = doTestOnNull; | |
} | |
public StringTest(String regexp) { | |
this.testNotNull = true; | |
this.pattern = Pattern.compile(regexp); | |
} | |
@Override | |
public String test(String obj, String columnName) throws Exception { | |
if (testNotNull && Strings.isNullOrEmpty(obj)) { | |
throw new IllegalArgumentException("Null value received for: " + columnName); | |
} | |
if (pattern != null) { | |
Matcher m = pattern.matcher(obj); | |
if (!m.find()) { | |
throw new IllegalArgumentException("Pattern not matched for: " + columnName); | |
} | |
} | |
return obj; | |
} | |
} | |
public void test1() throws Exception { | |
Map<String, Testable> columnTypes = ImmutableMap.of("age", new IntegerTest(), "firstName", new StringTest(true), "lastName", new StringTest()); | |
String filePath = "src/main/java/input.csv"; | |
List<Pojo> pojos = CsvToObjectListMapper.mapToObjects(filePath, columnTypes, Pojo.class); | |
} | |
public static class CsvToObjectListMapper { | |
public static <T> List<T> mapToObjects(String filePath, Map<String, Testable> columnTypes, Class<Pojo> class1) throws Exception { | |
CsvMapper mapper = new CsvMapper(); | |
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); | |
List<T> resultList = new ArrayList<T>(); | |
File csvFile = new File(filePath); | |
MappingIterator<String[]> reader = mapper.reader(String[].class).readValues(csvFile); | |
final String[] headerLine = reader.next(); | |
String[] nextLine; | |
while (reader.hasNext()) { | |
nextLine = reader.next(); | |
Map<String, Object> map = Maps.newHashMap(); | |
for (int i = 0; i < headerLine.length; i++) { | |
String headerName = headerLine[i]; | |
String value = nextLine[i]; | |
Testable testable = columnTypes.get(headerName); | |
map.put(headerName, testable.test(value, headerName)); | |
} | |
T bean = (T) mapper.convertValue(map, class1); | |
resultList.add(bean); | |
} | |
reader.close(); | |
return resultList; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment