com.threewks.thundr.csv.CsvMappingFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thundr-contrib-csv Show documentation
Show all versions of thundr-contrib-csv Show documentation
A thundr module for working with csvs
The newest version!
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://3wks.github.io/thundr/
* Copyright (C) 2014 3wks,
*
* 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 com.threewks.thundr.csv;
import java.util.HashMap;
import java.util.Map;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
import com.opencsv.bean.MappingStrategy;
// TODO - v3 - NAO allow better bean mapping by allowing classes to be registered
// to use the HeaderColumnNameTranslateMappingFactory is so required.
public class CsvMappingFactory {
private Map, MappingStrategy> strategies = new HashMap<>();
@SuppressWarnings("unchecked")
public MappingStrategy getMappingStrategy(Class type) {
MappingStrategy registered = (MappingStrategy) strategies.get(type);
return registered != null ? registered : createDefaultMappingStrategy(type);
}
@SuppressWarnings("deprecation")
protected MappingStrategy createDefaultMappingStrategy(Class type) {
HeaderColumnNameMappingStrategy mapper = new HeaderColumnNameMappingStrategy();
mapper.setType(type);
return mapper;
}
public void register(Class type, MappingStrategy strategy) {
strategies.put(type, strategy);
}
public void deregister(Class type) {
this.strategies.remove(type);
}
/**
* Rather than register a {@link MappingStrategy} directly, you can just register a mapping of column names to bean property names,
* which will automatically be translated when beans of this type are imported or bound.
*
*
* @param type
* @param columnNames
* @see HeaderColumnNameTranslateMappingStrategy#setColumnMapping(Map)
*/
public void register(Class type, Map columnNames) {
HeaderColumnNameTranslateMappingStrategy