aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/tenant/LastLoginInfo.java
blob: ed335a67df64cd8ba2f0d9a0ca4d51836b4cc11f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.tenant;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author freva
 */
public class LastLoginInfo {

    public static final LastLoginInfo EMPTY = new LastLoginInfo(Map.of());

    private final Map<UserLevel, Instant> lastLoginByUserLevel;

    public LastLoginInfo(Map<UserLevel, Instant> lastLoginByUserLevel) {
        this.lastLoginByUserLevel = Map.copyOf(lastLoginByUserLevel);
    }

    public Optional<Instant> get(UserLevel userLevel) {
        return Optional.ofNullable(lastLoginByUserLevel.get(userLevel));
    }

    /**
     * Returns new instance with updated last login time if the given {@code loginAt} timestamp is after the current
     * for the given {@code userLevel}, otherwise returns this
     */
    public LastLoginInfo withLastLoginIfLater(UserLevel userLevel, Instant loginAt) {
        Instant lastLogin = lastLoginByUserLevel.getOrDefault(userLevel, Instant.EPOCH);
        if (loginAt.isAfter(lastLogin)) {
            Map<UserLevel, Instant> lastLoginByUserLevel = new HashMap<>(this.lastLoginByUserLevel);
            lastLoginByUserLevel.put(userLevel, loginAt);
            return new LastLoginInfo(lastLoginByUserLevel);
        }
        return this;
    }

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

        LastLoginInfo lastLoginInfo = (LastLoginInfo) o;
        return lastLoginByUserLevel.equals(lastLoginInfo.lastLoginByUserLevel);
    }

    @Override
    public int hashCode() {
        return lastLoginByUserLevel.hashCode();
    }

    public enum UserLevel { user, developer, administrator };
}