-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
InMemoryResignations.java
158 lines (143 loc) · 4.83 KB
/
InMemoryResignations.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright (c) 2020, Self XDSD Contributors
* All rights reserved.
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to read the Software only. Permission is hereby NOT GRANTED to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.selfxdsd.core.mock;
import com.selfxdsd.api.Resignation;
import com.selfxdsd.api.Resignations;
import com.selfxdsd.api.Task;
import com.selfxdsd.api.storage.Storage;
import com.selfxdsd.core.tasks.TaskResignations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* In-memory Resignations.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.21
*/
public final class InMemoryResignations implements Resignations {
/**
* Resignations "table".
*/
private final Map<ResignationKey, Resignation> resignations;
/**
* Parent storage.
*/
private final Storage storage;
/**
* Constructor.
*
* @param storage Parent storage
*/
public InMemoryResignations(final Storage storage) {
this.storage = storage;
this.resignations = new HashMap<>();
}
@Override
public Resignations ofTask(final Task task) {
final Supplier<Stream<Resignation>> ofTask = () -> this.resignations
.values()
.stream()
.filter(r -> r.task().equals(task));
return new TaskResignations(
task,
ofTask,
this.storage
);
}
@Override
public Iterator<Resignation> iterator() {
throw new UnsupportedOperationException(
"You cannot iterate over all Resignations in Self."
);
}
/**
* Resignation Primary Key, formed by the Task and the Contributor PK's
* respectively.
*/
private static class ResignationKey {
/**
* Full name of the Repo represented by the Project.
*/
private final String repoFullName;
/**
* Contributor's username.
*/
private final String contributorUsername;
/**
* Contributor/Project's provider.
*/
private final String provider;
/**
* Issue's ID.
*/
private final String issueId;
/**
* Constructor.
*
* @param repoFullName Fullname of the Repo represented by the project.
* @param contributorUsername Contributor's username.
* @param provider Contributor/Project's provider.
* @param issueId Issue's is.
*/
ResignationKey(
final String repoFullName,
final String contributorUsername,
final String provider,
final String issueId
) {
this.repoFullName = repoFullName;
this.contributorUsername = contributorUsername;
this.provider = provider;
this.issueId = issueId;
}
@Override
public boolean equals(final Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
final InMemoryResignations.ResignationKey key =
(InMemoryResignations.ResignationKey) object;
//@checkstyle LineLength (5 lines)
return this.repoFullName.equals(key.repoFullName)
&& this.contributorUsername.equals(key.contributorUsername)
&& this.provider.equals(key.provider)
&& this.issueId.equals(key.issueId);
}
@Override
public int hashCode() {
return Objects.hash(
this.repoFullName,
this.contributorUsername,
this.provider,
this.issueId
);
}
}
}