-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvFileDefinition.java
123 lines (108 loc) · 3.92 KB
/
CsvFileDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.csv;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public record CsvFileDefinition(
String fileName, Path directoryPath, String[] headLineElements, String csvSep) {
private static final Logger logger = LoggerFactory.getLogger(CsvFileDefinition.class);
private static final Pattern FILE_NAME_PATTERN =
Pattern.compile(
"^(?<fileName>[^\\\\/\\s.]{0,255})(?:\\.(?<extension>[a-zA-Z0-9]{0,10}(?:\\.[a-zA-Z0-9]{0,10})?))?$");
private static final String FILE_EXTENSION = "csv";
public CsvFileDefinition(
String fileName, Path directoryPath, String[] headLineElements, String csvSep) {
/* Remove all file separators at the beginning and end of a directory path and ensure harmonized file separator */
this.directoryPath = Objects.nonNull(directoryPath) ? directoryPath : Path.of("");
/* Check the given information of the file name */
Matcher matcher = FILE_NAME_PATTERN.matcher(fileName);
if (matcher.matches()) {
String extension = matcher.group("extension");
if (Objects.nonNull(extension) && !extension.equalsIgnoreCase(FILE_EXTENSION))
logger.warn(
"You provided a file name with extension '{}'. It will be overridden to '{}'.",
extension,
FILE_EXTENSION);
this.fileName = matcher.group("fileName") + "." + FILE_EXTENSION;
} else {
throw new IllegalArgumentException(
"The file name '"
+ fileName
+ "' is no valid file name. It may contain everything, except '/', '\\', '.' and any white space character.");
}
this.headLineElements = headLineElements;
this.csvSep = csvSep;
}
/** @deprecated since 3.0. Use {@link #directoryPath()} instead */
@Deprecated(since = "3.0")
public Path getDirectoryPath() {
return directoryPath;
}
/**
* @return The file name including extension
* @deprecated since 3.0. Use {@link #fileName()} instead
*/
@Deprecated(since = "3.0")
public String getFileName() {
return fileName;
}
/**
* @return The path to the file relative to a not explicitly defined base directory, including the
* file extension
*/
public Path getFilePath() {
return Files.exists(directoryPath) ? directoryPath.resolve(fileName) : Path.of(fileName);
}
/** @deprecated since 3.0. Use {@link #headLineElements()} instead */
@Deprecated(since = "3.0")
public String[] getHeadLineElements() {
return headLineElements;
}
/** @deprecated since 3.0. Use {@link #csvSep()} instead */
@Deprecated(since = "3.0")
public String getCsvSep() {
return csvSep;
}
@Override
public boolean equals(Object o) {
// equals implementation is required here because
// records' equals method and array fields don't play together nicely
if (this == o) return true;
if (!(o instanceof CsvFileDefinition that)) return false;
return directoryPath.equals(that.directoryPath)
&& fileName.equals(that.fileName)
&& Arrays.equals(headLineElements, that.headLineElements)
&& csvSep.equals(that.csvSep);
}
@Override
public int hashCode() {
int result = Objects.hash(directoryPath, fileName, csvSep);
result = 31 * result + Arrays.hashCode(headLineElements);
return result;
}
@Override
public String toString() {
return "CsvFileDefinition{"
+ "directoryPath='"
+ directoryPath
+ '\''
+ ", fileName='"
+ fileName
+ '\''
+ ", headLineElements="
+ Arrays.toString(headLineElements)
+ ", csvSep='"
+ csvSep
+ '\''
+ '}';
}
}