aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/cgroup/Size.java
blob: d89db56e4d2b82188e69ffee5936ee51c577875a (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
68
// 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.cgroup;

import java.util.Objects;

/**
 * Represents a number of bytes or possibly "max".
 *
 * @author hakonhall
 */
public class Size {
    private static final String MAX = "max";
    private static final Size MAX_SIZE = new Size(true, 0);

    private final boolean max;
    private final long value;

    public static Size max() {
        return MAX_SIZE;
    }

    public static Size from(long value) {
        return new Size(false, value);
    }

    public static Size from(String value) {
        return value.equals(MAX) ? MAX_SIZE : new Size(false, Long.parseLong(value));
    }

    private Size(boolean max, long value) {
        this.max = max;
        this.value = value;
    }

    public boolean isMax() {
        return max;
    }

    /** Returns the value, i.e. the number of "bytes" if applicable. Throws if this is max. */
    public long value() {
        if (max) throw new IllegalStateException("Value is max");
        return value;
    }

    public String toFileContent() { return toString() + '\n'; }

    @Override
    public String toString() { return max ? MAX : Long.toString(value); }

    public boolean isGreaterThan(Size that) {
        if (that.max) return false;
        if (this.max) return true;
        return this.value > that.value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Size size = (Size) o;
        return max == size.max && value == size.value;
    }

    @Override
    public int hashCode() {
        return Objects.hash(max, value);
    }
}