Skip to content

Commit 8fe9ae5

Browse files
author
TheSnoozer
committed
simplify code
1 parent 68ebc4e commit 8fe9ae5

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/main/java/pl/project13/core/util/GenericFileManager.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import pl.project13.core.PropertiesFileGenerator;
2525
import pl.project13.core.log.LogInterface;
2626

27+
import javax.annotation.Nonnull;
28+
import javax.annotation.Nullable;
2729
import java.io.File;
2830
import java.io.FileOutputStream;
2931
import java.io.IOException;
@@ -34,30 +36,30 @@
3436

3537
public class GenericFileManager {
3638
public static Properties readProperties(
37-
LogInterface log,
38-
CommitIdPropertiesOutputFormat propertiesOutputFormat,
39-
File gitPropsFile,
40-
Charset sourceCharset,
41-
String projectName
39+
@Nullable LogInterface log,
40+
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
41+
@Nonnull File gitPropsFile,
42+
@Nonnull Charset sourceCharset,
43+
@Nullable String projectName
4244
) throws GitCommitIdExecutionException {
4345
final Properties persistedProperties;
4446

4547
try {
48+
if (log != null) {
49+
log.info(String.format("Reading existing %s file [%s] (for project %s)...",
50+
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
51+
}
4652
switch (propertiesOutputFormat) {
4753
case JSON:
48-
log.info(String.format("Reading existing json file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
4954
persistedProperties = JsonManager.readJsonProperties(gitPropsFile, sourceCharset);
5055
break;
5156
case PROPERTIES:
52-
log.info(String.format("Reading existing properties file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
5357
persistedProperties = PropertyManager.readProperties(gitPropsFile);
5458
break;
5559
case XML:
56-
log.info(String.format("Reading existing xml file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
5760
persistedProperties = XmlManager.readXmlProperties(gitPropsFile, sourceCharset);
5861
break;
5962
case YML:
60-
log.info(String.format("Reading existing yml file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
6163
persistedProperties = YmlManager.readYmlProperties(gitPropsFile, sourceCharset);
6264
break;
6365
default:
@@ -66,42 +68,43 @@ public static Properties readProperties(
6668
} catch (final CannotReadFileException ex) {
6769
// Read has failed, regenerate file
6870
throw new GitCommitIdExecutionException(
69-
String.format("Cannot read properties file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
71+
String.format("Cannot read file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
7072
}
7173
return persistedProperties;
7274
}
7375

7476
public static void dumpProperties(
75-
LogInterface log,
76-
CommitIdPropertiesOutputFormat propertiesOutputFormat,
77-
File gitPropsFile,
78-
Charset sourceCharset,
77+
@Nullable LogInterface log,
78+
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
79+
@Nonnull File gitPropsFile,
80+
@Nonnull Charset sourceCharset,
7981
boolean escapeUnicode,
80-
String projectName,
81-
Properties propertiesToDump
82+
@Nullable String projectName,
83+
@Nonnull Properties propertiesToDump
8284
) throws GitCommitIdExecutionException {
8385
try {
86+
if (log != null) {
87+
log.info(String.format("Writing %s file [%s] (for project %s)...",
88+
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
89+
}
90+
8491
Files.createDirectories(gitPropsFile.getParentFile().toPath());
8592
try (final OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
8693
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
8794
propertiesToDump.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
8895
switch (propertiesOutputFormat) {
8996
case JSON:
90-
log.info(String.format("Writing json file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
9197
JsonManager.dumpJson(outputStream, sortedLocalProperties, sourceCharset);
9298
break;
9399
case PROPERTIES:
94-
log.info(String.format("Writing properties file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
95100
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
96101
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
97102
break;
98103
case XML:
99-
log.info(String.format("Writing xml file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
100104
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
101105
XmlManager.dumpXml(outputStream, sortedLocalProperties, sourceCharset);
102106
break;
103107
case YML:
104-
log.info(String.format("Writing yml file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
105108
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
106109
YmlManager.dumpYml(outputStream, sortedLocalProperties, sourceCharset);
107110
break;

0 commit comments

Comments
 (0)