Formatting applied to aaa-authn bundle
[aaa.git] / aaa-authn / src / main / java / org / opendaylight / aaa / PasswordCredentialBuilder.java
1 /*
2  * Copyright (c) 2014 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;
9
10 import static org.opendaylight.aaa.EqualUtil.areEqual;
11 import static org.opendaylight.aaa.HashCodeUtil.hash;
12
13 import org.opendaylight.aaa.api.PasswordCredentials;
14
15 /**
16  * {@link PasswordCredentials} builder.
17  *
18  * @author liemmn
19  *
20  */
21 public class PasswordCredentialBuilder {
22     private final MutablePasswordCredentials pc = new MutablePasswordCredentials();
23
24     public PasswordCredentialBuilder setUserName(String username) {
25         pc.username = username;
26         return this;
27     }
28
29     public PasswordCredentialBuilder setPassword(String password) {
30         pc.password = password;
31         return this;
32     }
33
34     public PasswordCredentialBuilder setDomain(String domain) {
35         pc.domain = domain;
36         return this;
37     }
38
39     public PasswordCredentials build() {
40         return pc;
41     }
42
43     private static class MutablePasswordCredentials implements PasswordCredentials {
44         private int hashCode = 0;
45         private String username;
46         private String password;
47         private String domain;
48
49         @Override
50         public String username() {
51             return username;
52         }
53
54         @Override
55         public String password() {
56             return password;
57         }
58
59         @Override
60         public String domain() {
61             return domain;
62         }
63
64         @Override
65         public boolean equals(Object o) {
66             if (this == o) {
67                 return true;
68             }
69             if (!(o instanceof PasswordCredentials)) {
70                 return false;
71             }
72             PasswordCredentials p = (PasswordCredentials) o;
73             return areEqual(username, p.username()) && areEqual(password, p.password());
74         }
75
76         @Override
77         public int hashCode() {
78             if (hashCode == 0) {
79                 int result = HashCodeUtil.SEED;
80                 result = hash(result, username);
81                 result = hash(result, password);
82                 hashCode = result;
83             }
84             return hashCode;
85         }
86     }
87 }