Inmate.java

  1. package com.renomad.minum.security;


  2. import com.renomad.minum.database.DbData;

  3. import java.util.Objects;

  4. import static com.renomad.minum.utils.SerializationUtils.deserializeHelper;
  5. import static com.renomad.minum.utils.SerializationUtils.serializeHelper;

  6. /**
  7.  * Represents an inmate in our "jail".  If someone does something we don't like, they do their time here.
  8.  */
  9. public final class Inmate extends DbData<Inmate> {

  10.     /**
  11.      * Builds an empty version of this class, except
  12.      * that it has a current Context object
  13.      */
  14.     public static final Inmate EMPTY = new Inmate(0L, "", 0L);
  15.     private Long index;
  16.     private final String clientId;
  17.     private final Long releaseTime;

  18.     /**
  19.      * Represents an inmate in our "jail".  If someone does something we don't like, they do their time here.
  20.      * @param clientId a string representation of the client address plus a string representing the offense,
  21.      *                 for example, "1.2.3.4_vuln_seeking" - 1.2.3.4 was seeking out vulnerabilities.
  22.      * @param releaseTime the time, in milliseconds from the epoch, at which this inmate will be released
  23.      *                    from the brig.
  24.      */
  25.     public Inmate(Long index, String clientId, Long releaseTime) {
  26.         this.index = index;
  27.         this.clientId = clientId;
  28.         this.releaseTime = releaseTime;
  29.     }

  30.     @Override
  31.     public long getIndex() {
  32.         return index;
  33.     }

  34.     @Override
  35.     public void setIndex(long index) {
  36.         this.index = index;
  37.     }

  38.     @Override
  39.     public String serialize() {
  40.         return serializeHelper(index, clientId, releaseTime);
  41.     }

  42.     @Override
  43.     public Inmate deserialize(String serializedText) {
  44.         final var tokens = deserializeHelper(serializedText);

  45.         return new Inmate(
  46.                 Long.parseLong(tokens.get(0)),
  47.                 tokens.get(1),
  48.                 Long.parseLong(tokens.get(2)));
  49.     }

  50.     public String getClientId() {
  51.         return clientId;
  52.     }

  53.     public Long getReleaseTime() {
  54.         return releaseTime;
  55.     }

  56.     @Override
  57.     public boolean equals(Object o) {
  58.         if (this == o) return true;
  59.         if (o == null || getClass() != o.getClass()) return false;
  60.         Inmate inmate = (Inmate) o;
  61.         return Objects.equals(index, inmate.index) && Objects.equals(clientId, inmate.clientId) && Objects.equals(releaseTime, inmate.releaseTime);
  62.     }

  63.     @Override
  64.     public int hashCode() {
  65.         return Objects.hash(index, clientId, releaseTime);
  66.     }

  67.     @Override
  68.     public String toString() {
  69.         return "Inmate{" +
  70.                 "index=" + index +
  71.                 ", clientId='" + clientId + '\'' +
  72.                 ", releaseTime=" + releaseTime +
  73.                 '}';
  74.     }
  75. }