PoBoxNumber.java

  1. /*
  2.  * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
  3.  */
  4. package de.powerstat.validation.values;


  5. import java.util.Objects;

  6. import de.powerstat.validation.interfaces.IValueObject;


  7. /**
  8.  * Address Post office box number.
  9.  *
  10.  * DSGVO relevant.
  11.  */
  12. public final class PoBoxNumber implements Comparable<PoBoxNumber>, IValueObject
  13.  {
  14.   /* *
  15.    * Cache for singletons.
  16.    */
  17.   // private static final Map<Long, PoBoxNumber> CACHE = new WeakHashMap<>();

  18.   /**
  19.    * Post office box number.
  20.    */
  21.   private final long poBoxNumber;


  22.   /**
  23.    * Constructor.
  24.    *
  25.    * @param poBoxNumber PO box number 1-..
  26.    * @throws IndexOutOfBoundsException When the poBoxNumber is less than 1
  27.    */
  28.   private PoBoxNumber(final long poBoxNumber)
  29.    {
  30.     super();
  31.     if (poBoxNumber < 1)
  32.      {
  33.       throw new IndexOutOfBoundsException("POBoxNumber number out of range (1-..)!"); //$NON-NLS-1$
  34.      }
  35.     this.poBoxNumber = poBoxNumber;
  36.    }


  37.   /**
  38.    * PoBoxNumber factory.
  39.    *
  40.    * @param poBoxNumber PoBox number 1-..
  41.    * @return PoBoxNumber object
  42.    */
  43.   public static PoBoxNumber of(final long poBoxNumber)
  44.    {
  45.     /*
  46.     synchronized (PoBoxNumber.class)
  47.      {
  48.       PoBoxNumber obj = PoBoxNumber.CACHE.get(poBoxNumber);
  49.       if (obj != null)
  50.        {
  51.         return obj;
  52.        }
  53.       obj = new PoBoxNumber(poBoxNumber);
  54.       PoBoxNumber.CACHE.put(Long.valueOf(poBoxNumber), obj);
  55.       return obj;
  56.      }
  57.     */
  58.     return new PoBoxNumber(poBoxNumber);
  59.    }


  60.   /**
  61.    * PoBoxNumber factory.
  62.    *
  63.    * @param value PoBox number 1-.. string
  64.    * @return PoBoxNumber object
  65.    */
  66.   public static PoBoxNumber of(final String value)
  67.    {
  68.     return of(Long.parseLong(value));
  69.    }


  70.   /**
  71.    * Returns the value of this BFPONumber as a long.
  72.    *
  73.    * @return The numeric value represented by this object after conversion to type long.
  74.    */
  75.   public long longValue()
  76.    {
  77.     return this.poBoxNumber;
  78.    }


  79.   /**
  80.    * Returns the value of this PoBoNumber as a string.
  81.    *
  82.    * @return The text value represented by this object after conversion to type string.
  83.    */
  84.   @Override
  85.   public String stringValue()
  86.    {
  87.     return Long.toString(this.poBoxNumber);
  88.    }


  89.   /**
  90.    * Calculate hash code.
  91.    *
  92.    * @return Hash
  93.    * @see java.lang.Object#hashCode()
  94.    */
  95.   @Override
  96.   public int hashCode()
  97.    {
  98.     return Long.hashCode(this.poBoxNumber);
  99.    }


  100.   /**
  101.    * Is equal with another object.
  102.    *
  103.    * @param obj Object
  104.    * @return true when equal, false otherwise
  105.    * @see java.lang.Object#equals(java.lang.Object)
  106.    */
  107.   @Override
  108.   public boolean equals(final Object obj)
  109.    {
  110.     if (this == obj)
  111.      {
  112.       return true;
  113.      }
  114.     if (!(obj instanceof PoBoxNumber))
  115.      {
  116.       return false;
  117.      }
  118.     final PoBoxNumber other = (PoBoxNumber)obj;
  119.     return this.poBoxNumber == other.poBoxNumber;
  120.    }


  121.   /**
  122.    * Returns the string representation of this PoBoxNumber.
  123.    *
  124.    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
  125.    *
  126.    * "PoBoxNumber[poBoxNumber=4711]"
  127.    *
  128.    * @return String representation of this PoBoxNumber
  129.    * @see java.lang.Object#toString()
  130.    */
  131.   @Override
  132.   public String toString()
  133.    {
  134.     final var builder = new StringBuilder(25);
  135.     builder.append("PoBoxNumber[poBoxNumber=").append(this.poBoxNumber).append(']'); //$NON-NLS-1$
  136.     return builder.toString();
  137.    }


  138.   /**
  139.    * Compare with another object.
  140.    *
  141.    * @param obj Object to compare with
  142.    * @return 0: equal; 1: greater; -1: smaller
  143.    * @see java.lang.Comparable#compareTo(java.lang.Object)
  144.    */
  145.   @Override
  146.   public int compareTo(final PoBoxNumber obj)
  147.    {
  148.     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
  149.     return Long.compare(this.poBoxNumber, obj.poBoxNumber);
  150.    }

  151.  }