1 package cn.home1.oss.lib.security.api;
2
3 import org.springframework.security.core.GrantedAuthority;
4
5 import java.util.Comparator;
6
7
8
9
10 public abstract class AbstractGrantedAuthority implements GrantedAuthority, Comparable<GrantedAuthority> {
11
12 public static final Comparator<GrantedAuthority> COMPARATOR = (lhs, rhs) -> {
13 final int result;
14 if (lhs != null && rhs != null) {
15 result = lhs.getAuthority().compareTo(rhs.getAuthority());
16 } else if (lhs != null) {
17 result = 1;
18 } else {
19 result = -1;
20 }
21 return result;
22 };
23
24 @Override
25 public int compareTo(final GrantedAuthority rhs) {
26 return AbstractGrantedAuthority.COMPARATOR.compare(this, rhs);
27 }
28
29 @Override
30 public boolean equals(final Object obj) {
31 final boolean result;
32 if (obj != null) {
33 if (this.getClass() == obj.getClass() || GrantedAuthority.class.isAssignableFrom(obj.getClass())) {
34 final GrantedAuthority rhs = (GrantedAuthority) obj;
35 result = this.getAuthority().equals(rhs.getAuthority());
36 } else {
37 result = false;
38 }
39 } else {
40 result = false;
41 }
42 return result;
43 }
44
45 @Override
46 public int hashCode() {
47 return this.getAuthority().hashCode();
48 }
49
50 @Override
51 public String toString() {
52 return this.getAuthority();
53 }
54 }