View Javadoc
1   /*
2    * Copyright (C) 2021-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values;
5   
6   
7   import java.util.Objects;
8   
9   import de.powerstat.validation.interfaces.IValueObject;
10  
11  
12  /**
13   * Milliseconds.
14   *
15   * Not DSGVO relevant.
16   *
17   * TODO min, max
18   */
19  public final class Milliseconds implements Comparable<Milliseconds>, IValueObject
20   {
21    /* *
22     * Cache for singletons.
23     */
24    // private static final Map<Long, Milliseconds> CACHE = new WeakHashMap<>();
25  
26    /**
27     * Milliseonds.
28     */
29    private final long milliseconds;
30  
31  
32    /**
33     * Constructor.
34     *
35     * @param milliseconds Milliseconds &gt;= 0
36     * @throws IndexOutOfBoundsException When the milliseonds is less than 0
37     */
38    private Milliseconds(final long milliseconds)
39     {
40      super();
41      if (milliseconds < 0)
42       {
43        throw new IndexOutOfBoundsException("Milliseconds out of range (0-Long.MAX_VALUE)!"); //$NON-NLS-1$
44       }
45      this.milliseconds = milliseconds;
46     }
47  
48  
49    /**
50     * Milliseconds factory.
51     *
52     * @param milliseconds Milliseconds 0-[Long.MAX_VALUE]
53     * @return Milliseconds object
54     */
55    public static Milliseconds of(final long milliseconds)
56     {
57      /*
58      synchronized (Milliseconds.class)
59       {
60        Milliseconds obj = Milliseconds.CACHE.get(milliseconds);
61        if (obj != null)
62         {
63          return obj;
64         }
65        obj = new Milliseconds(milliseconds);
66        Milliseconds.CACHE.put(Long.valueOf(milliseconds), obj);
67        return obj;
68       }
69      */
70      return new Milliseconds(milliseconds);
71     }
72  
73  
74    /**
75     * Milliseconds factory.
76     *
77     * @param value Milliseconds 0-[Long.MAX_VALUE] string
78     * @return Milliseconds object
79     */
80    public static Milliseconds of(final String value)
81     {
82      return of(Long.parseLong(value));
83     }
84  
85  
86    /**
87     * Returns the value of this Milliseconds as a long.
88     *
89     * @return The numeric value represented by this object after conversion to type long (0-Long.MAX_VALUE).
90     */
91    public long longValue()
92     {
93      return this.milliseconds;
94     }
95  
96  
97    /**
98     * Returns the value of this Milliseconds as a String.
99     *
100    * @return The numeric value represented by this object after conversion to type String (0-Long.MAX_VALUE).
101    */
102   @Override
103   public String stringValue()
104    {
105     return String.valueOf(this.milliseconds);
106    }
107 
108 
109   /**
110    * Calculate hash code.
111    *
112    * @return Hash
113    * @see java.lang.Object#hashCode()
114    */
115   @Override
116   public int hashCode()
117    {
118     return Long.hashCode(this.milliseconds);
119    }
120 
121 
122   /**
123    * Is equal with another object.
124    *
125    * @param obj Object
126    * @return true when equal, false otherwise
127    * @see java.lang.Object#equals(java.lang.Object)
128    */
129   @Override
130   public boolean equals(final Object obj)
131    {
132     if (this == obj)
133      {
134       return true;
135      }
136     if (!(obj instanceof Milliseconds))
137      {
138       return false;
139      }
140     final Milliseconds other = (Milliseconds)obj;
141     return this.milliseconds == other.milliseconds;
142    }
143 
144 
145   /**
146    * Returns the string representation of this Milliseconds.
147    *
148    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
149    *
150    * "Milliseconds[milliseconds=0]"
151    *
152    * @return String representation of this Milliseconds
153    * @see java.lang.Object#toString()
154    */
155   @Override
156   public String toString()
157    {
158     final var builder = new StringBuilder(27);
159     builder.append("Milliseconds[milliseconds=").append(this.milliseconds).append(']'); //$NON-NLS-1$
160     return builder.toString();
161    }
162 
163 
164   /**
165    * Compare with another object.
166    *
167    * @param obj Object to compare with
168    * @return 0: equal; 1: greater; -1: smaller
169    * @see java.lang.Comparable#compareTo(java.lang.Object)
170    */
171   @Override
172   public int compareTo(final Milliseconds obj)
173    {
174     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
175     return Long.compare(this.milliseconds, obj.milliseconds);
176    }
177 
178 
179   /**
180    * Add other milliseconds to this milliseconds.
181    *
182    * @param other Other milliseconds to add to this milliseconds
183    * @return New milliseconds after adding other milliseconds to this milliseconds
184    * @throws ArithmeticException In case of an overflow
185    */
186   public Milliseconds add(final Milliseconds other)
187    {
188     return Milliseconds.of(Math.addExact(this.milliseconds, other.milliseconds));
189    }
190 
191 
192   /**
193    * Subtract other milliseconds from this milliseconds.
194    *
195    * @param other Other milliseconds to subtract from this one
196    * @return Absolute new milliseconds after subtracting other milliseconds from this Milliseconds
197    */
198   public Milliseconds subtract(final Milliseconds other)
199    {
200     if (other.milliseconds > this.milliseconds)
201      {
202       return Milliseconds.of(other.milliseconds - this.milliseconds);
203      }
204     return Milliseconds.of(this.milliseconds - other.milliseconds);
205    }
206 
207 
208   /**
209    * Multiply milliseconds with a multiplier.
210    *
211    * @param multiplier Multiplier to multiply with
212    * @return New milliseconds that is a multiplication of this milliseconds with the multiplier
213    * @throws ArithmeticException In case of an overflow
214    */
215   public Milliseconds multiply(final long multiplier)
216    {
217     return Milliseconds.of(Math.multiplyExact(this.milliseconds, multiplier));
218    }
219 
220 
221   /**
222    * Divide milliseconds by a divisor.
223    *
224    * @param divisor Divisor to divide by
225    * @return The largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
226    * @throws ArithmeticException In case the divisor is 0.
227    */
228   public Milliseconds divide(final long divisor)
229    {
230     return Milliseconds.of(Math.floorDiv(this.milliseconds, divisor));
231    }
232 
233 
234   /**
235    * Floor modulo milliseconds by a divisor.
236    *
237    * @param divisor Divisor to divide by
238    * @return The floor modulus Milliseconds - (floorDiv(Milliseconds, divisor) * divisor)
239    * @throws ArithmeticException In case the divisor is 0.
240    */
241   public Milliseconds modulo(final long divisor)
242    {
243     return Milliseconds.of(Math.floorMod(this.milliseconds, divisor));
244    }
245 
246  }