1
|
package eu.dnetlib.dhp.common.counter;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.io.OutputStream;
|
7
|
import java.util.Properties;
|
8
|
|
9
|
/**
|
10
|
* Writer of {@link NamedCounters} object into a properties file.
|
11
|
*
|
12
|
* @author madryk
|
13
|
*/
|
14
|
public class NamedCountersFileWriter {
|
15
|
|
16
|
|
17
|
//------------------------ LOGIC --------------------------
|
18
|
|
19
|
/**
|
20
|
* Writes {@link NamedCounters} as a properties file located under
|
21
|
* provided filePath.
|
22
|
*
|
23
|
* @throws IOException if writing to properties file resulted in an error
|
24
|
*/
|
25
|
public void writeCounters(NamedCounters counters, String filePath) throws IOException {
|
26
|
|
27
|
Properties counterProperties = buildPropertiesFromCounters(counters);
|
28
|
|
29
|
File file = new File(filePath);
|
30
|
try (OutputStream os = new FileOutputStream(file)) {
|
31
|
|
32
|
counterProperties.store(os, null);
|
33
|
|
34
|
}
|
35
|
|
36
|
}
|
37
|
|
38
|
|
39
|
//------------------------ PRIVATE --------------------------
|
40
|
|
41
|
private Properties buildPropertiesFromCounters(NamedCounters counters) {
|
42
|
|
43
|
Properties properties = new Properties();
|
44
|
|
45
|
for (String counterName : counters.counterNames()) {
|
46
|
long count = counters.currentValue(counterName);
|
47
|
properties.put(counterName, String.valueOf(count));
|
48
|
}
|
49
|
|
50
|
return properties;
|
51
|
}
|
52
|
}
|