aboutsummaryrefslogtreecommitdiffstats
path: root/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/ContainerResources.java
blob: 4c538d6a194ef5861076a7fc92aa6f7c28e6a04d (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 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.dockerapi;

/**
 * @author valerijf
 */
public class ContainerResources {
    public static final ContainerResources UNLIMITED = ContainerResources.from(0, 0);

    public final int cpuShares;
    public final long memoryBytes;

    ContainerResources(int cpuShares, long memoryBytes) {
        this.cpuShares = cpuShares;
        this.memoryBytes = memoryBytes;
    }

    public static ContainerResources from(double cpuCores, double memoryGb) {
        return new ContainerResources(
                (int) Math.round(10 * cpuCores),
                (long) ((1L << 30) * memoryGb));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ContainerResources that = (ContainerResources) o;

        if (cpuShares != that.cpuShares) return false;
        return memoryBytes == that.memoryBytes;
    }

    @Override
    public int hashCode() {
        int result = cpuShares;
        result = 31 * result + (int) (memoryBytes ^ (memoryBytes >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return cpuShares + " CPU Shares, " + memoryBytes + "B memory";
    }
}