• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pgpainless / pgpainless / #1041

29 Aug 2023 02:59PM CUT coverage: 89.073%. Remained the same
#1041

push

github-actions

vanitasvitae
Update changelog

7084 of 7953 relevant lines covered (89.07%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java
1
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package org.pgpainless.algorithm;
6

7
import org.pgpainless.util.DateUtil;
8

9
import javax.annotation.Nonnull;
10
import java.util.Date;
11
import java.util.NoSuchElementException;
12

13
public final class RevocationState implements Comparable<RevocationState> {
14

15
    private final RevocationStateType type;
16
    private final Date date;
17

18
    private RevocationState(RevocationStateType type) {
19
        this(type, null);
1✔
20
    }
1✔
21

22
    private RevocationState(RevocationStateType type, Date date) {
1✔
23
        this.type = type;
1✔
24
        if (type == RevocationStateType.softRevoked && date == null) {
1✔
25
            throw new NullPointerException("If type is 'softRevoked' then date cannot be null.");
1✔
26
        }
27
        this.date = date;
1✔
28
    }
1✔
29

30
    public static RevocationState notRevoked() {
31
        return new RevocationState(RevocationStateType.notRevoked);
1✔
32
    }
33

34
    public static RevocationState softRevoked(@Nonnull Date date) {
35
        return new RevocationState(RevocationStateType.softRevoked, date);
1✔
36
    }
37

38
    public static RevocationState hardRevoked() {
39
        return new RevocationState(RevocationStateType.hardRevoked);
1✔
40
    }
41

42
    public RevocationStateType getType() {
43
        return type;
1✔
44
    }
45

46
    public @Nonnull Date getDate() {
47
        if (!isSoftRevocation()) {
1✔
48
            throw new NoSuchElementException("RevocationStateType is not equal to 'softRevoked'. Cannot extract date.");
1✔
49
        }
50
        return date;
1✔
51
    }
52

53
    public boolean isHardRevocation() {
54
        return getType() == RevocationStateType.hardRevoked;
1✔
55
    }
56

57
    public boolean isSoftRevocation() {
58
        return getType() == RevocationStateType.softRevoked;
1✔
59
    }
60

61
    public boolean isNotRevoked() {
62
        return getType() == RevocationStateType.notRevoked;
1✔
63
    }
64

65
    @Override
66
    public String toString() {
67
        String out = getType().toString();
1✔
68
        if (isSoftRevocation()) {
1✔
69
            out = out + " (" + DateUtil.formatUTCDate(date) + ")";
1✔
70
        }
71
        return out;
1✔
72
    }
73

74
    @Override
75
    public int compareTo(@Nonnull RevocationState o) {
76
        switch (getType()) {
1✔
77
            case notRevoked:
78
                if (o.isNotRevoked()) {
1✔
79
                    return 0;
1✔
80
                } else {
81
                    return -1;
1✔
82
                }
83

84
            case softRevoked:
85
                if (o.isNotRevoked()) {
1✔
86
                    return 1;
1✔
87
                } else if (o.isSoftRevocation()) {
1✔
88
                    // Compare soft dates in reverse
89
                    return o.getDate().compareTo(getDate());
1✔
90
                } else {
91
                    return -1;
×
92
                }
93

94
            case hardRevoked:
95
                if (o.isHardRevocation()) {
1✔
96
                    return 0;
1✔
97
                } else {
98
                    return 1;
1✔
99
                }
100

101
            default:
102
                throw new AssertionError("Unknown type: " + type);
×
103
        }
104
    }
105

106
    @Override
107
    public int hashCode() {
108
        return type.hashCode() * 31 + (isSoftRevocation() ? getDate().hashCode() : 0);
×
109
    }
110

111
    @Override
112
    public boolean equals(Object obj) {
113
        if (obj == null) {
1✔
114
            return false;
1✔
115
        }
116
        if (this == obj) {
1✔
117
            return true;
1✔
118
        }
119
        if (!(obj instanceof RevocationState)) {
1✔
120
            return false;
1✔
121
        }
122
        RevocationState other = (RevocationState) obj;
1✔
123
        if (getType() != other.getType()) {
1✔
124
            return false;
1✔
125
        }
126
        if (isSoftRevocation()) {
1✔
127
            return DateUtil.toSecondsPrecision(getDate()).getTime() == DateUtil.toSecondsPrecision(other.getDate()).getTime();
1✔
128
        }
129
        return true;
1✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc