1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.linecorp.centraldogma.server.metadata;
17
18 import static java.util.Objects.requireNonNull;
19
20 import java.util.Map;
21 import java.util.Map.Entry;
22
23 import com.fasterxml.jackson.annotation.JsonCreator;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.google.common.base.MoreObjects;
26 import com.google.common.base.Objects;
27 import com.google.common.collect.ImmutableMap;
28
29 import com.linecorp.centraldogma.common.RepositoryRole;
30 import com.linecorp.centraldogma.server.storage.repository.HasWeight;
31
32
33
34
35 public final class Roles implements HasWeight {
36
37 static final Roles EMPTY = new Roles(ProjectRoles.of(null, null),
38 ImmutableMap.of(), ImmutableMap.of());
39
40 private final ProjectRoles projectRoles;
41
42 private final Map<String, RepositoryRole> users;
43
44 private final Map<String, RepositoryRole> tokens;
45
46
47
48
49 @JsonCreator
50 public Roles(@JsonProperty("projects") ProjectRoles projectRoles,
51 @JsonProperty("users") Map<String, RepositoryRole> users,
52 @JsonProperty("tokens") Map<String, RepositoryRole> tokens) {
53 this.projectRoles = requireNonNull(projectRoles, "projectRoles");
54 this.users = requireNonNull(users, "users");
55 this.tokens = requireNonNull(tokens, "tokens");
56 }
57
58
59
60
61 @JsonProperty("projects")
62 public ProjectRoles projectRoles() {
63 return projectRoles;
64 }
65
66
67
68
69 @JsonProperty("users")
70 public Map<String, RepositoryRole> users() {
71 return users;
72 }
73
74
75
76
77 @JsonProperty("tokens")
78 public Map<String, RepositoryRole> tokens() {
79 return tokens;
80 }
81
82 @Override
83 public int weight() {
84 int weight = 0;
85 final RepositoryRole member = projectRoles.member();
86 if (member != null) {
87 weight += member.name().length();
88 }
89 for (Entry<String, RepositoryRole> entry : users.entrySet()) {
90 weight += entry.getKey().length();
91 weight += entry.getValue().name().length();
92 }
93 for (Entry<String, RepositoryRole> entry : tokens.entrySet()) {
94 weight += entry.getKey().length();
95 weight += entry.getValue().name().length();
96 }
97 return weight;
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) {
103 return true;
104 }
105 if (!(o instanceof Roles)) {
106 return false;
107 }
108 final Roles other = (Roles) o;
109 return projectRoles.equals(other.projectRoles) &&
110 users.equals(other.users) &&
111 tokens.equals(other.tokens);
112 }
113
114 @Override
115 public int hashCode() {
116 return Objects.hashCode(projectRoles, users, tokens);
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(this)
122 .add("projectRoles", projectRoles)
123 .add("users", users)
124 .add("tokens", tokens)
125 .toString();
126 }
127 }