/
ImmutableAccessControlListBuilder.java
executable file
·115 lines (86 loc) · 3.43 KB
/
ImmutableAccessControlListBuilder.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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.things;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
/**
* A mutable builder for a {@link ImmutableAccessControlList} with a fluent API.
*
* @deprecated AccessControlLists belong to deprecated API version 1. Use API version 2 with policies instead.
*/
@Deprecated
@NotThreadSafe
final class ImmutableAccessControlListBuilder implements AccessControlListBuilder {
private final Map<AuthorizationSubject, AclEntry> entries;
private ImmutableAccessControlListBuilder(final Map<AuthorizationSubject, AclEntry> theEntries) {
entries = theEntries;
}
/**
* Returns a new empty builder for a {@code AccessControlList}.
*
* @return the new builder.
*/
public static AccessControlListBuilder newInstance() {
return new ImmutableAccessControlListBuilder(new HashMap<>());
}
/**
* Returns a new builder for a {@code AccessControlList} which is initialised with the given entries.
*
* @param aclEntries the initials entries of the new builder.
* @return the new builder.
* @throws NullPointerException if {@code aclEntries} is null;
*/
public static AccessControlListBuilder of(final Iterable<AclEntry> aclEntries) {
checkNotNull(aclEntries, "initial ACL entries");
final Map<AuthorizationSubject, AclEntry> theEntries = new HashMap<>();
aclEntries.forEach(e -> theEntries.put(e.getAuthorizationSubject(), e));
return new ImmutableAccessControlListBuilder(theEntries);
}
@Override
public AccessControlListBuilder set(final AclEntry entry) {
checkNotNull(entry, "entry to be set");
entries.put(entry.getAuthorizationSubject(), entry);
return this;
}
@Override
public AccessControlListBuilder setAll(final Iterable<AclEntry> entries) {
checkNotNull(entries, "entries to be set");
entries.forEach(e -> this.entries.put(e.getAuthorizationSubject(), e));
return this;
}
@Override
public AccessControlListBuilder remove(final AclEntry entry) {
checkNotNull(entry, "entry to be removed");
entries.remove(entry.getAuthorizationSubject());
return this;
}
@Override
public AccessControlListBuilder remove(final AuthorizationSubject authorizationSubject) {
checkNotNull(authorizationSubject, "authorization subject of the entry to be removed");
entries.remove(authorizationSubject);
return this;
}
@Override
public AccessControlListBuilder removeAll(final Iterable<AclEntry> entries) {
checkNotNull(entries, "entries to be removed");
entries.forEach(e -> this.entries.remove(e.getAuthorizationSubject()));
return this;
}
@Override
public AccessControlList build() {
return AccessControlListModelFactory.newAcl(entries.values());
}
}