AbstractGrantedAuthority.java

  1. package cn.home1.oss.lib.security.api;

  2. import org.springframework.security.core.GrantedAuthority;

  3. import java.util.Comparator;

  4. /**
  5.  * Created by haolun on 17/1/4.
  6.  */
  7. public abstract class AbstractGrantedAuthority implements GrantedAuthority, Comparable<GrantedAuthority> {

  8.   public static final Comparator<GrantedAuthority> COMPARATOR = (lhs, rhs) -> {
  9.     final int result;
  10.     if (lhs != null && rhs != null) {
  11.       result = lhs.getAuthority().compareTo(rhs.getAuthority());
  12.     } else if (lhs != null) { // rhs == null
  13.       result = 1;
  14.     } else { // lhs == null && rhs != null
  15.       result = -1;
  16.     }
  17.     return result;
  18.   };

  19.   @Override
  20.   public int compareTo(final GrantedAuthority rhs) {
  21.     return AbstractGrantedAuthority.COMPARATOR.compare(this, rhs);
  22.   }

  23.   @Override
  24.   public boolean equals(final Object obj) {
  25.     final boolean result;
  26.     if (obj != null) {
  27.       if (this.getClass() == obj.getClass() || GrantedAuthority.class.isAssignableFrom(obj.getClass())) {
  28.         final GrantedAuthority rhs = (GrantedAuthority) obj;
  29.         result = this.getAuthority().equals(rhs.getAuthority());
  30.       } else {
  31.         result = false;
  32.       }
  33.     } else {
  34.       result = false;
  35.     }
  36.     return result;
  37.   }

  38.   @Override
  39.   public int hashCode() {
  40.     return this.getAuthority().hashCode();
  41.   }

  42.   @Override
  43.   public String toString() {
  44.     return this.getAuthority();
  45.   }
  46. }