aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/PartialFileData.java
blob: b1d56b131bb0cd684ea12ec61399f96b85db3a72 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Vespa.ai. 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.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
 * Represents a subset of a file's content, owner, group, and permissions.
 *
 * @author hakonhall
 */
// @Immutable
public class PartialFileData {
    private final Optional<byte[]> content;
    private final Optional<Integer> ownerId;
    private final Optional<Integer> groupId;
    private final Optional<String> permissions;

    public static Builder builder() {
        return new Builder();
    }

    private PartialFileData(Optional<byte[]> content,
                            Optional<Integer> ownerId,
                            Optional<Integer> groupId,
                            Optional<String> permissions) {
        this.content = content;
        this.ownerId = ownerId;
        this.groupId = groupId;
        this.permissions = permissions;
    }

    public Optional<byte[]> getContent() {
        return content;
    }

    public Optional<Integer> getOwnerId() {
        return ownerId;
    }

    public Optional<Integer> getGroupId() {
        return groupId;
    }

    public Optional<String> getPermissions() {
        return permissions;
    }

    public static class Builder {
        private Optional<byte[]> content = Optional.empty();
        private Optional<Integer> ownerId = Optional.empty();
        private Optional<Integer> groupId = Optional.empty();
        private Optional<String> permissions = Optional.empty();

        public Builder withContent(byte[] content) { this.content = Optional.of(content); return this; }
        public Builder withContent(String content, Charset charset) { return withContent(content.getBytes(charset)); }
        public Builder withContent(String content) { return withContent(content, StandardCharsets.UTF_8); }
        public Builder withOwnerId(int ownerId) { this.ownerId = Optional.of(ownerId); return this; }
        public Builder withGroupId(int groupId) { this.groupId = Optional.of(groupId); return this; }
        public Builder withPermissions(String permissions) { this.permissions = Optional.of(permissions); return this; }

        public PartialFileData create() {
            return new PartialFileData(content, ownerId, groupId, permissions);
        }
    }
}