-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUsage.java
More file actions
109 lines (93 loc) · 3.38 KB
/
Copy pathUsage.java
File metadata and controls
109 lines (93 loc) · 3.38 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2022 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
package com.deepl.api;
import java.util.function.BiConsumer;
import org.jetbrains.annotations.Nullable;
/**
* Information about DeepL account usage for the current billing period, for example the number of
* characters translated.
*
* <p>Depending on the account type, some usage types will be omitted. See the <a
* href="https://www.deepl.com/docs-api/">API documentation</a> for more information.
*/
public class Usage {
private final @Nullable Detail character;
private final @Nullable Detail document;
private final @Nullable Detail teamDocument;
/** The character usage if included for the account type, or <code>null</code>. */
public @Nullable Detail getCharacter() {
return character;
}
/** The document usage if included for the account type, or <code>null</code>. */
public @Nullable Detail getDocument() {
return document;
}
/** The team document usage if included for the account type, or <code>null</code>. */
public @Nullable Detail getTeamDocument() {
return teamDocument;
}
/** Stores the amount used and maximum amount for one usage type. */
public static class Detail {
private final long count;
private final long limit;
public Detail(long count, long limit) {
this.count = count;
this.limit = limit;
}
/** @return The currently used number of items for this usage type. */
public long getCount() {
return count;
}
/** @return The maximum permitted number of items for this usage type. */
public long getLimit() {
return limit;
}
/**
* @return <code>true</code> if the amount used meets or exceeds the limit, otherwise <code>
* false</code>.
*/
public boolean limitReached() {
return getCount() >= getLimit();
}
@Override
public String toString() {
return getCount() + " of " + getLimit();
}
}
public Usage(
@Nullable Detail character, @Nullable Detail document, @Nullable Detail teamDocument) {
this.character = character;
this.document = document;
this.teamDocument = teamDocument;
}
/**
* @return <code>true</code> if any of the usage types included for the account type have been
* reached, otherwise <code>false</code>.
*/
public boolean anyLimitReached() {
return (getCharacter() != null && getCharacter().limitReached())
|| (getDocument() != null && getDocument().limitReached())
|| (getTeamDocument() != null && getTeamDocument().limitReached());
}
/**
* Returns a string representing the usage. This function is for diagnostic purposes only; the
* content of the returned string is exempt from backwards compatibility.
*
* @return A string containing the usage for this billing period.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Usage this billing period:");
BiConsumer<String, Detail> addLabelledDetail =
(label, detail) -> {
if (detail != null) {
sb.append("\n").append(label).append(": ").append(detail);
}
};
addLabelledDetail.accept("Characters", getCharacter());
addLabelledDetail.accept("Documents", getDocument());
addLabelledDetail.accept("Team documents", getTeamDocument());
return sb.toString();
}
}