Fix checkstyle issues to enforce it
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / tokenauthrealm / auth / AuthenticationBuilder.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 java.io.Serializable;
11 import java.util.Set;
12 import org.opendaylight.aaa.api.Authentication;
13 import org.opendaylight.aaa.api.Claim;
14 import org.opendaylight.aaa.shiro.tokenauthrealm.util.EqualUtil;
15 import org.opendaylight.aaa.shiro.tokenauthrealm.util.HashCodeUtil;
16
17 /**
18  * A builder for the authentication context.
19  *
20  * <p>
21  * The expiration DEFAULTS to 0.
22  *
23  * @author liemmn
24  */
25 public class AuthenticationBuilder {
26
27     private long expiration = 0L;
28     private final Claim claim;
29
30     public AuthenticationBuilder(Claim claim) {
31         this.claim = claim;
32     }
33
34     public AuthenticationBuilder setExpiration(long expiration) {
35         this.expiration = expiration;
36         return this;
37     }
38
39     public Authentication build() {
40         return new ImmutableAuthentication(this);
41     }
42
43     private static final class ImmutableAuthentication implements Authentication, Serializable {
44         private static final long serialVersionUID = 4919078164955609987L;
45         private int hashCode = 0;
46         long expiration = 0L;
47         Claim claim;
48
49         private ImmutableAuthentication(AuthenticationBuilder base) {
50             if (base.claim == null) {
51                 throw new IllegalStateException("The Claim is null.");
52             }
53             claim = new ClaimBuilder(base.claim).build();
54             expiration = base.expiration;
55
56             if (base.expiration < 0) {
57                 throw new IllegalStateException("The expiration is less than 0.");
58             }
59         }
60
61         @Override
62         public long expiration() {
63             return expiration;
64         }
65
66         @Override
67         public String clientId() {
68             return claim.clientId();
69         }
70
71         @Override
72         public String userId() {
73             return claim.userId();
74         }
75
76         @Override
77         public String user() {
78             return claim.user();
79         }
80
81         @Override
82         public String domain() {
83             return claim.domain();
84         }
85
86         @Override
87         public Set<String> roles() {
88             return claim.roles();
89         }
90
91         @Override
92         public boolean equals(Object object) {
93             if (this == object) {
94                 return true;
95             }
96             if (!(object instanceof Authentication)) {
97                 return false;
98             }
99             Authentication authentication = (Authentication) object;
100             return EqualUtil.areEqual(expiration, authentication.expiration()) && EqualUtil
101                     .areEqual(claim.roles(), authentication.roles()) && EqualUtil
102                     .areEqual(claim.domain(), authentication.domain()) && EqualUtil
103                     .areEqual(claim.userId(), authentication.userId()) && EqualUtil
104                     .areEqual(claim.user(), authentication.user()) && EqualUtil
105                     .areEqual(claim.clientId(), authentication.clientId());
106         }
107
108         @Override
109         public int hashCode() {
110             if (hashCode == 0) {
111                 int result = HashCodeUtil.SEED;
112                 result = HashCodeUtil.hash(result, expiration);
113                 result = HashCodeUtil.hash(result, claim.hashCode());
114                 hashCode = result;
115             }
116             return hashCode;
117         }
118
119         @Override
120         public String toString() {
121             return "expiration:" + expiration + "," + claim.toString();
122         }
123     }
124 }