blob: 3249dab1ca84332912ae8d8fc1a1edb274d4eae5 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;
import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.function.Supplier;
import java.util.logging.Logger;
/**
* Class wrapping a float stored on disk
*
* @author freva
*/
public class StoredDouble implements Supplier<OptionalDouble> {
private static final Logger logger = Logger.getLogger(StoredDouble.class.getName());
private final UnixPath path;
public StoredDouble(Path path) {
this.path = new UnixPath(path);
}
@Override
public OptionalDouble get() {
return path.readUtf8FileIfExists().stream().mapToDouble(Double::parseDouble).findAny();
}
public void write(TaskContext taskContext, double value) {
path.writeUtf8File(Double.toString(value));
taskContext.log(logger, "Stored new double in %s: %f", path, value);
}
public void clear() {
path.deleteIfExists();
}
public Optional<Instant> getLastModifiedTime() {
return path.getAttributesIfExists().map(FileAttributes::lastModifiedTime);
}
}
|