]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/security/SimplePrincipal.java
29b8626e16bcc581d132f9155b83b0ff443584c3
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / security / SimplePrincipal.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.util.security;
17
18 import java.security.Principal;
19
20 import org.argeo.util.internal.UtilsException;
21
22 /** Canonical implementation of a {@link Principal} */
23 public class SimplePrincipal implements Principal {
24 private final String name;
25
26 public SimplePrincipal(String name) {
27 if (name == null)
28 throw new UtilsException("Principal name cannot be null");
29 this.name = name;
30 }
31
32 public String getName() {
33 return name;
34 }
35
36 @Override
37 public int hashCode() {
38 return name.hashCode();
39 }
40
41 @Override
42 public boolean equals(Object obj) {
43 if (obj == null)
44 return false;
45 if (obj instanceof Principal)
46 return name.equals((((Principal) obj).getName()));
47 return name.equals(obj.toString());
48 }
49
50 @Override
51 protected Object clone() throws CloneNotSupportedException {
52 return new SimplePrincipal(name);
53 }
54
55 @Override
56 public String toString() {
57 return name;
58 }
59
60 }