Skip to content
Open

done #34

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>

</project>
57 changes: 55 additions & 2 deletions src/main/java/org/writer/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
package org.writer;


import com.github.javafaker.Faker;
import org.writer.model.*;
import org.writer.utils.writers.Writable;
import org.writer.utils.writers.csvwriter.CSVWriter;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
public static void main(String[] args) throws IOException {
Faker faker = new Faker(Locale.ENGLISH);

UUID id = UUID.randomUUID();
String fullname = faker.name().fullName();
LocalDate date = faker.date().birthday(0, 10).toInstant()
.atZone(java.time.ZoneId.systemDefault())
.toLocalDate();

Employee employee = new Employee(id, fullname, date);
List<Employee> employees = List.of(employee);

Writable<Employee> writerE = new CSVWriter<>();

writerE.writeToFile(employees, "src\\main\\resources" +
"\\reportEmployee.csv");

Product product = new Product(UUID.randomUUID(), faker.company().name(),
Double.parseDouble(faker.commerce().price().replace(',', '.')));
List<Product> products = List.of(product);

Writable<Product> writerP = new CSVWriter<>();
writerP.writeToFile(products, "src\\main\\resources" +
"\\reportProduct");

List<Person> persons = List.of(Person.builder()
.firstName("Pasha")
.lastName("Barabaska")
.dayOfBirth(2)
.monthOfBirth(Months.JANUARY)
.yearOfBirth(1992)
.build());

Writable<Person> writerPerson = new CSVWriter<>();
writerPerson.writeToFile(persons, "src\\main\\resources" +
"\\reportPersons");

List<Student> students = List.of(Student.builder()
.name("bacugan")
.score(List.of("qwerty", "wasd", "12345")).build());
Writable<Student> writerStudent = new CSVWriter<>();
writerStudent.writeToFile(students, "src\\main\\resources" +
"\\reportStudents");
}
}
9 changes: 0 additions & 9 deletions src/main/java/org/writer/Writable.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/org/writer/annotations/CSVColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.writer.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation for marking the fields that will form the CSV report headers.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CSVColumn {

/**
* column name in CSV report.
*
* @return column name
*/
String columnName();
}
14 changes: 14 additions & 0 deletions src/main/java/org/writer/annotations/CSVReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.writer.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation for marking classes that can be written to the CSV report.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CSVReport {
}
13 changes: 13 additions & 0 deletions src/main/java/org/writer/model/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.writer.model;

import org.writer.annotations.CSVColumn;
import org.writer.annotations.CSVReport;

import java.time.LocalDate;
import java.util.UUID;

@CSVReport
public record Employee(@CSVColumn(columnName = "id") UUID id,
@CSVColumn(columnName = "full name of the employee") String fullname,
@CSVColumn(columnName = "date of employment") LocalDate dateOfEmployment ) {
}
8 changes: 8 additions & 0 deletions src/main/java/org/writer/model/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.writer.annotations.CSVColumn;
import org.writer.annotations.CSVReport;

@Data
@Builder
@AllArgsConstructor
@CSVReport
public class Person {

@CSVColumn(columnName = "first name")
private String firstName;

@CSVColumn(columnName = "last name")
private String lastName;

@CSVColumn(columnName = "day of birth")
private int dayOfBirth;

@CSVColumn(columnName = "month of birth")
private Months monthOfBirth;

@CSVColumn(columnName = "year of birth")
private int yearOfBirth;

}
12 changes: 12 additions & 0 deletions src/main/java/org/writer/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.writer.model;

import org.writer.annotations.CSVColumn;
import org.writer.annotations.CSVReport;

import java.util.UUID;

@CSVReport
public record Product(@CSVColumn(columnName = "id") UUID id,
@CSVColumn(columnName = "product name") String name,
@CSVColumn(columnName = "price in dollars") double price) {
}
5 changes: 5 additions & 0 deletions src/main/java/org/writer/model/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.writer.annotations.CSVColumn;
import org.writer.annotations.CSVReport;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@CSVReport
public class Student {

@CSVColumn(columnName = "name")
private String name;

@CSVColumn(columnName = "score")
private List<String> score;
}
35 changes: 35 additions & 0 deletions src/main/java/org/writer/utils/FileType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.writer.utils;

import lombok.Getter;

/**
* Enumeration of supported file formats for data export.
* Each format contains file extension information.
*/
@Getter
public enum FileType {

/**
* Comma-Separated Values format.
* Text format for representing tabular data.
*/
CSV(".csv");

/**
* -- GETTER --
* Returns the file extension for this format.
*
* @return file extension string including the dot.
*/
private final String fileFormat;

/**
* Creates a file type with the specified extension.
*
* @param fileFormat file extension.
*/
FileType(String fileFormat) {
this.fileFormat = fileFormat;
}

}
31 changes: 31 additions & 0 deletions src/main/java/org/writer/utils/FileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.writer.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
* Utility class for guaranteed writing of a list of data to a file.
*/
public class FileUtil {

/**
* Static method for writing a list of data to a file at a specified absolute address.
* If the address or file does not exist, it will be created.
* Truncates the file if it already exists.
*
* @param filePath Absolute address of the file to write to.
* @param lines The data lines that will be written to the file.
* @throws IOException If an I/O error occurs while writing to the file.
*/
public static void writeLinesToFile(String filePath, Iterable<String> lines) throws IOException {
Path path = Paths.get(filePath);
if (Files.notExists(path)){
Files.createDirectories(path.getParent());
Files.createFile(path);
}
Files.write(path, lines, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
85 changes: 85 additions & 0 deletions src/main/java/org/writer/utils/writers/AbstractWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.writer.utils.writers;


import org.writer.utils.FileType;
import org.writer.utils.FileUtil;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
* Abstract class for writing data to files of various formats.
* Provides a general implementation for working with annotated fields.
*
* @param <T> type of data being written
*/
public abstract class AbstractWriter<T> implements Writable<T>{

/**
* Returns the file type for the current implementation.
*
* @return file type
*/
protected abstract FileType getFileType();

/**
* Gets a list of fields suitable for export to the current format.
*
* @param clazz class to parse
* @return list of fields to export
*/
protected abstract List<Field> getExportFields(Class<?> clazz);

/**
* Converts an object to a string for writing to a file.
*
* @param obj the object to convert
* @param fields the list of fields to process
* @return the string representation of the object
*/
protected abstract String formatRow(T obj, List<Field> fields);

/**
* Generates file headers.
*
* @param fields list of fields
* @return string with headers
*/
protected abstract String formatHeaders(List<Field> fields);

@Override
public void writeToFile(List<T> data, String path) throws IOException {
if (data.isEmpty()) {
throw new IllegalArgumentException("Data is empty!");
}

Class<?> clazz = data.get(0).getClass();
List<Field> fields = getExportFields(clazz);

List<String> report = new ArrayList<>();
report.add(formatHeaders(fields));

for (T item : data) {
report.add(formatRow(item, fields));
}

String fullPath = ensureFileExtension(path, getFileType());
FileUtil.writeLinesToFile(fullPath, report);
}

/**
* Adds file extension if it is missing.
*
* @param path file path
* @param fileType file type
* @return path with extension
*/
protected String ensureFileExtension(String path, FileType fileType) {
if (!path.endsWith(fileType.getFileFormat())) {
return path + fileType.getFileFormat();
}
return path;
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/writer/utils/writers/Writable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.writer.utils.writers;

import java.io.IOException;
import java.util.List;

/**
* Interface for writing file data.
*
* @param <T> Record data type.
*/
public interface Writable<T> {

/**
* Method for writing report file.
*
* @param data List of data to record.
* @param path Absolute address of the file to write to.
* @throws IOException If an I/O error occurs while writing to the file.
*/
void writeToFile(List<T> data, String path) throws IOException;
}
Loading