e8eb19824bc6b8f488465a90da77294d19149314
[aaa.git] / aaa-authn / src / main / java / org / opendaylight / aaa / ClaimBuilder.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa;
10
11 import static org.opendaylight.aaa.EqualUtil.areEqual;
12 import static org.opendaylight.aaa.HashCodeUtil.hash;
13
14 import java.io.Serializable;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.opendaylight.aaa.api.Claim;
20
21 /**
22  * Builder for a {@link Claim}
23  *
24  * @author liemmn
25  *
26  */
27 public class ClaimBuilder {
28     private final MutableClaim mc = new MutableClaim();
29
30     public ClaimBuilder() {
31     }
32
33     public ClaimBuilder(Claim claim) {
34         setClaim(claim);
35     }
36
37     protected void setClaim(Claim claim) {
38         mc.clientId = claim.clientId();
39         mc.userId = claim.userId();
40         mc.user = claim.user();
41         mc.domain = claim.domain();
42         mc.roles.addAll(claim.roles());
43     }
44
45     public ClaimBuilder setClientId(String clientId) {
46         mc.clientId = clientId;
47         return this;
48     }
49
50     public ClaimBuilder setUserId(String userId) {
51         mc.userId = userId;
52         return this;
53     }
54
55     public ClaimBuilder setUser(String userName) {
56         mc.user = userName;
57         return this;
58     }
59
60     public ClaimBuilder setDomain(String domain) {
61         mc.domain = domain;
62         return this;
63     }
64
65     public ClaimBuilder addRoles(Set<String> roles) {
66         mc.roles.addAll(roles);
67         return this;
68     }
69
70     public ClaimBuilder addRole(String role) {
71         mc.roles.add(role);
72         return this;
73     }
74
75     public Claim build() {
76         return mc;
77     }
78
79     // Mutable claim
80     protected static class MutableClaim implements Claim, Serializable {
81         private static final long serialVersionUID = -8115027645190209129L;
82         int hashCode = 0;
83         String clientId;
84         String userId;
85         String user;
86         String domain;
87         final Set<String> roles = new HashSet<String>();
88
89         @Override
90         public String clientId() {
91             return clientId;
92         }
93
94         @Override
95         public String userId() {
96             return userId;
97         }
98
99         @Override
100         public String user() {
101             return user;
102         }
103
104         @Override
105         public String domain() {
106             return domain;
107         }
108
109         @Override
110         public Set<String> roles() {
111             return Collections.unmodifiableSet(roles);
112         }
113
114         @Override
115         public boolean equals(Object o) {
116             if (this == o)
117                 return true;
118             if (!(o instanceof Claim))
119                 return false;
120             Claim a = (Claim) o;
121             return areEqual(roles, a.roles()) && areEqual(domain, a.domain())
122                     && areEqual(userId, a.userId()) && areEqual(user, a.user())
123                     && areEqual(clientId, a.clientId());
124         }
125
126         @Override
127         public int hashCode() {
128             if (hashCode == 0) {
129                 int result = HashCodeUtil.SEED;
130                 result = hash(result, clientId);
131                 result = hash(result, userId);
132                 result = hash(result, user);
133                 result = hash(result, domain);
134                 result = hash(result, roles);
135                 hashCode = result;
136             }
137             return hashCode;
138         }
139
140         @Override
141         public String toString() {
142             StringBuffer sb = new StringBuffer();
143             if (clientId != null)
144                 sb.append("clientId:").append(clientId).append(",");
145             if (userId != null)
146                 sb.append("userId:").append(userId).append(",");
147             if (user != null)
148                 sb.append("userName:").append(user).append(",");
149             if (domain != null)
150                 sb.append("domain:").append(domain).append(",");
151             sb.append("roles:").append(roles);
152             return sb.toString();
153         }
154     }
155
156 }