forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
93 lines (76 loc) · 2.5 KB
/
Stack.java
File metadata and controls
93 lines (76 loc) · 2.5 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
package io.sentry;
import io.sentry.util.Objects;
import java.util.Deque;
import java.util.concurrent.LinkedBlockingDeque;
import org.jetbrains.annotations.NotNull;
final class Stack {
static final class StackItem {
private final SentryOptions options;
private volatile @NotNull ISentryClient client;
private volatile @NotNull Scope scope;
StackItem(
final @NotNull SentryOptions options,
final @NotNull ISentryClient client,
final @NotNull Scope scope) {
this.client = Objects.requireNonNull(client, "ISentryClient is required.");
this.scope = Objects.requireNonNull(scope, "Scope is required.");
this.options = Objects.requireNonNull(options, "Options is required");
}
StackItem(final @NotNull StackItem item) {
options = item.options;
client = item.client;
try {
scope = item.scope.clone();
} catch (CloneNotSupportedException e) {
// TODO: Why do we need to deal with this? We must guarantee clone is possible here!
options.getLogger().log(SentryLevel.ERROR, "Clone not supported");
scope = new Scope(item.options);
}
}
public @NotNull ISentryClient getClient() {
return client;
}
public void setClient(final @NotNull ISentryClient client) {
this.client = client;
}
public @NotNull Scope getScope() {
return scope;
}
public @NotNull SentryOptions getOptions() {
return options;
}
}
private final @NotNull Deque<StackItem> items = new LinkedBlockingDeque<>();
private final @NotNull ILogger logger;
public Stack(final @NotNull ILogger logger, final @NotNull StackItem rootStackItem) {
this.logger = Objects.requireNonNull(logger, "logger is required");
this.items.push(Objects.requireNonNull(rootStackItem, "rootStackItem is required"));
}
public Stack(final @NotNull Stack stack) {
this(stack.logger, stack.items.getFirst());
for (final StackItem item : stack.items) {
push(new StackItem(item));
}
}
@NotNull
StackItem peek() {
// peek can never return null since Stack can be created only with an item and pop does not drop
// the last item.
return items.peek();
}
void pop() {
synchronized (items) {
if (items.size() != 1) {
items.pop();
} else {
logger.log(SentryLevel.WARNING, "Attempt to pop the root scope.");
}
}
}
void push(final @NotNull StackItem stackItem) {
items.push(stackItem);
}
int size() {
return items.size();
}
}