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

  20.   /**
  21.    * Hours.
  22.    */
  23.   private final long hours;


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


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


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


  72.   /**
  73.    * Returns the value of this BFPONumber as a 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.hours;
  80.    }


  81.   /**
  82.    * Returns the value of this BFPONumber 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.hours);
  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.hours);
  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 Hours))
  117.      {
  118.       return false;
  119.      }
  120.     final Hours other = (Hours)obj;
  121.     return this.hours == other.hours;
  122.    }


  123.   /**
  124.    * Returns the string representation of this Hours.
  125.    *
  126.    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
  127.    *
  128.    * "Hours[hours=1]"
  129.    *
  130.    * @return String representation of this Hours
  131.    * @see java.lang.Object#toString()
  132.    */
  133.   @Override
  134.   public String toString()
  135.    {
  136.     final var builder = new StringBuilder();
  137.     builder.append("Hours[hours=").append(this.hours).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 Hours obj)
  149.    {
  150.     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
  151.     return Long.compare(this.hours, obj.hours);
  152.    }


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


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


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


  189.   /**
  190.    * Divide hours 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 Hours divide(final long divisor)
  197.    {
  198.     return Hours.of(Math.floorDiv(this.hours, divisor));
  199.    }


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

  211.  }