Weeks.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.  * Weeks.
  9.  *
  10.  * Not DSGVO relevant.
  11.  *
  12.  * TODO min, max
  13.  */
  14. public final class Weeks implements Comparable<Weeks>, IValueObject
  15.  {
  16.   /* *
  17.    * Cache for singletons.
  18.    */
  19.   // private static final Map<Long, Weeks> CACHE = new WeakHashMap<>();

  20.   /**
  21.    * Weeks.
  22.    */
  23.   private final long weeks;


  24.   /**
  25.    * Constructor.
  26.    *
  27.    * @param weeks Weeks 0-..
  28.    * @throws IndexOutOfBoundsException When the week is less than 0
  29.    */
  30.   private Weeks(final long weeks)
  31.    {
  32.     super();
  33.     if (weeks < 0)
  34.      {
  35.       throw new IndexOutOfBoundsException("Negative weeks are not allowed"); //$NON-NLS-1$
  36.      }
  37.     this.weeks = weeks;
  38.    }


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


  62.   /**
  63.    * Weeks factory.
  64.    *
  65.    * @param value Weeks 0-.. string
  66.    * @return Weeks object
  67.    */
  68.   public static Weeks of(final String value)
  69.    {
  70.     return of(Long.parseLong(value));
  71.    }


  72.   /**
  73.    * Returns the value of this Weeks as an long.
  74.    *
  75.    * @return The numeric value represented by this object after conversion to type long.
  76.    */
  77.   public long longValue()
  78.    {
  79.     return this.weeks;
  80.    }


  81.   /**
  82.    * Returns the value of this Weeks as a String.
  83.    *
  84.    * @return The numeric value represented by this object after conversion to type String.
  85.    */
  86.   @Override
  87.   public String stringValue()
  88.    {
  89.     return String.valueOf(this.weeks);
  90.    }


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


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


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


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


  153.   /**
  154.    * Add other weeks to this years.
  155.    *
  156.    * @param other Other weeks to add to this weeks
  157.    * @return New weeks after adding other weeks to this weeks
  158.    * @throws ArithmeticException In case of an overflow
  159.    */
  160.   public Weeks add(final Weeks other)
  161.    {
  162.     return Weeks.of(Math.addExact(this.weeks, other.weeks));
  163.    }


  164.   /**
  165.    * Subtract other weeks from this weeks.
  166.    *
  167.    * @param other Other weeks to subtract from this one
  168.    * @return Absolute new weeks after subtracting other weeks from this weeks
  169.    */
  170.   public Weeks subtract(final Weeks other)
  171.    {
  172.     if (other.weeks > this.weeks)
  173.      {
  174.       return Weeks.of(other.weeks - this.weeks);
  175.      }
  176.     return Weeks.of(this.weeks - other.weeks);
  177.    }


  178.   /**
  179.    * Multiply weeks with a multiplier.
  180.    *
  181.    * @param multiplier Multiplier to multiply with
  182.    * @return New weeks that is a multiplication of this weeks with the multiplier
  183.    * @throws ArithmeticException In case of an overflow
  184.    */
  185.   public Weeks multiply(final long multiplier)
  186.    {
  187.     return Weeks.of(Math.multiplyExact(this.weeks, multiplier));
  188.    }


  189.   /**
  190.    * Divide weeks by a divisor.
  191.    *
  192.    * @param divisor Divisor to divide by
  193.    * @return The largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
  194.    * @throws ArithmeticException In case the divisor is 0.
  195.    */
  196.   public Weeks divide(final long divisor)
  197.    {
  198.     return Weeks.of(Math.floorDiv(this.weeks, divisor));
  199.    }


  200.   /**
  201.    * Floor modulo weeks by a divisor.
  202.    *
  203.    * @param divisor Divisor to divide by
  204.    * @return The floor modulus Weeks - (floorDiv(Weeks, divisor) * divisor)
  205.    * @throws ArithmeticException In case the divisor is 0.
  206.    */
  207.   public Weeks modulo(final long divisor)
  208.    {
  209.     return Weeks.of(Math.floorMod(this.weeks, divisor));
  210.    }

  211.  }