034cfe1f3c8329bec69675745e6c9a7cec656ba8
[aaa.git] /
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
9
10 import java.util.Objects;
11 import org.opendaylight.aaa.api.PasswordCredentials;
12
13 /**
14  * {@link PasswordCredentials} builder.
15  *
16  * @author liemmn
17  */
18 public class PasswordCredentialBuilder {
19     private final MutablePasswordCredentials pc = new MutablePasswordCredentials();
20
21     public PasswordCredentialBuilder setUserName(String username) {
22         pc.username = username;
23         return this;
24     }
25
26     public PasswordCredentialBuilder setPassword(String password) {
27         pc.password = password;
28         return this;
29     }
30
31     public PasswordCredentialBuilder setDomain(String domain) {
32         pc.domain = domain;
33         return this;
34     }
35
36     public PasswordCredentials build() {
37         return pc;
38     }
39
40     private static class MutablePasswordCredentials implements PasswordCredentials {
41         private int hashCode = 0;
42         private String username;
43         private String password;
44         private String domain;
45
46         @Override
47         public String username() {
48             return username;
49         }
50
51         @Override
52         public String password() {
53             return password;
54         }
55
56         @Override
57         public String domain() {
58             return domain;
59         }
60
61         @Override
62         public boolean equals(Object object) {
63             if (this == object) {
64                 return true;
65             }
66             if (!(object instanceof PasswordCredentials)) {
67                 return false;
68             }
69             PasswordCredentials passwordCredentials = (PasswordCredentials) object;
70             return Objects.equals(username, passwordCredentials.username())
71                     && Objects.equals(password, passwordCredentials.password());
72         }
73
74         @Override
75         public int hashCode() {
76             if (hashCode == 0) {
77                 hashCode = Objects.hash(username, password);
78             }
79             return hashCode;
80         }
81     }
82 }