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