Skip to content

gdbranco/YACSVWriter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

contributors-url forks-url stars-url issues-url license-url linkedin-url


YACSVWriter

Yet Another CSV Writer is an easy to use java library that maps POJOs to CSV string

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

Getting Started

Download the released version .jar or add dependency to maven (Maven Central)

<dependency>
  <groupId>io.github.gdbranco</groupId>
  <artifactId>yacsvwriter</artifactId>
  <version>0.0.2</version>
</dependency>

Usage

Please refer to the tests included for now, I'll be adding examples in the readme soon

  • Currently works for simple POJOs, composite POJOs, complex POJOs.
  • Accepts custom field name as well as field ignoring via java annotation.
public class SimplePojo {
	public String firstName;
    public String lastName;
    private LocalDate startDate;

    public SimplePojo(String firstName, String lastName, LocalDate startDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.startDate = startDate;
    }

    public String name() {
        return this.firstName + " " + this.lastName;
    }

    public LocalDate getStart() {
        return this.startDate;
    }
}
public class IgnoredFieldCompositePojo {
	public SimplePojo simple;
	public String field;
	@CsvIgnore
	public String ignoredField;
	
	public IgnoredFieldCompositePojo(SimplePojo simple, String field, String ignoredField) {
		super();
		this.simple = simple;
		this.field = field;
		this.ignoredField = ignoredField;
	}
}
 CsvWriter<IgnoredFieldCompositePojo> csvWriter = new CsvWriter<IgnoredFieldCompositePojo>();
    	LocalDate now = LocalDate.now();
    	LocalDate plusDays = now.plusDays(2);
		String csvString = csvWriter.writeCsv(Arrays.asList(new IgnoredFieldCompositePojo[] {
    			new IgnoredFieldCompositePojo(new SimplePojo("Alice", "Bennett", now), "firstRowField", "firstRowIgnoredField"),
    			new IgnoredFieldCompositePojo(new SimplePojo("Bob", "Russell", plusDays), "secondRowField", "secondRowIgnoredField")}));
  • Able to utilize overloading of toString in classes to shorten the amount of fields within a specific class, the list of classes can be passed via varargs within the constructor.
public class SimplePojo {
	public String firstName;
    public String lastName;
    private LocalDate startDate;

    public SimplePojo(String firstName, String lastName, LocalDate startDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.startDate = startDate;
    }

    public String name() {
        return this.firstName + " " + this.lastName;
    }

    public LocalDate getStart() {
        return this.startDate;
    }
}
public class RenameFieldCompositePojo {
	public SimplePojo simple;
	public String field;
	@CsvField(name = "Renamed Field")
	public String ignoredField;
	
	public RenameFieldCompositePojo(SimplePojo simple, String field, String ignoredField) {
		super();
		this.simple = simple;
		this.field = field;
		this.ignoredField = ignoredField;
	}
}
CsvWriter<RenameFieldCompositePojo> csvWriter = new CsvWriter<RenameFieldCompositePojo>();
    	LocalDate now = LocalDate.now();
    	LocalDate plusDays = now.plusDays(2);
		String csvString = csvWriter.writeCsv(Arrays.asList(new RenameFieldCompositePojo[] {
    			new RenameFieldCompositePojo(new SimplePojo("Alice", "Bennett", now), "firstRowField", "firstRowRenamedField"),
    			new RenameFieldCompositePojo(new SimplePojo("Bob", "Russell", plusDays), "secondRowField", "secondRowRenamedField")}));

Roadmap

No know issues at the moment

Contributing

Contributions make the open source community the best place to learn and create. Your efforts are greatly appreciated.

To contribute please follow the steps bellow:

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/YourFeatureName)
  3. Commit your Changes (git commit -m 'Add some new feature')
  4. Push to the Branch (git push origin feature/YourFeatureName)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Guilherme David Branco - gdbranco@gmail.com

Project Link: https://github.com/gdbranco/YACSVWriter

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages