aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileAttributesCache.java
blob: 12a9609f89cbff0d3e9202923ab1d86775bf15cf (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
// Copyright 2018 Yahoo Holdings. 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 java.util.Optional;

// @ThreadUnsafe
public class FileAttributesCache {
    private final UnixPath path;

    private Optional<FileAttributes> attributes = Optional.empty();

    public FileAttributesCache(UnixPath path) {
        this.path =  path;
    }

    public FileAttributes get() {
        if (!attributes.isPresent()) {
            attributes = Optional.of(path.getAttributes());
        }

        return attributes.get();
    }

    public FileAttributes forceGet() {
        attributes = Optional.empty();
        return get();
    }

    public boolean exists() {
        if (attributes.isPresent()) {
            return true;
        }

        Optional<FileAttributes> attributes = path.getAttributesIfExists();
        if (attributes.isPresent()) {
            // Might as well update this.attributes
            this.attributes = attributes;
            return true;
        } else {
            return false;
        }
    }
}