View Javadoc
1   /*
2    * Copyright (C) 2024 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values;
5   
6   import java.util.Objects;
7   
8   import de.powerstat.validation.interfaces.IValueObject;
9   
10  /**
11   * Percent 0-100.
12   *
13   * Not DSGVO relevant.
14   */
15  public final class Percent implements Comparable<Percent>, IValueObject
16   {
17    /**
18     * Percent.
19     */
20    private final int percent;
21  
22  
23    /**
24     * Constructor.
25     *
26     * @param percent Percent 0-100
27     * @throws IndexOutOfBoundsException When the percent is less than 0 or greater than 100
28     */
29    private Percent(final int percent)
30     {
31      super();
32      if ((percent < 0) || (percent > 100))
33       {
34        throw new IndexOutOfBoundsException("Percent number out of range (0-100)!"); //$NON-NLS-1$
35       }
36      this.percent = percent;
37     }
38  
39  
40    /**
41     * Percent factory.
42     *
43     * @param percent Percent 0-100
44     * @return Percent object
45     */
46    public static Percent of(final int percent)
47     {
48      return new Percent(percent);
49     }
50  
51  
52    /**
53     * Percent factory.
54     *
55     * @param percent Percent 0-100
56     * @return Percent object
57     */
58    public static Percent of(final String percent)
59     {
60      return of(Integer.parseInt(percent));
61     }
62  
63  
64    /**
65     * Returns the value of this Percent as an int.
66     *
67     * @return The numeric value represented by this object after conversion to type int.
68     */
69    public int intValue()
70     {
71      return this.percent;
72     }
73  
74  
75    /**
76     * Returns the value of this Percent as an String.
77     *
78     * @return The numeric value represented by this object after conversion to type String.
79     */
80    @Override
81    public String stringValue()
82     {
83      return String.valueOf(this.percent);
84     }
85  
86  
87    /**
88     * Calculate hash code.
89     *
90     * @return Hash
91     * @see java.lang.Object#hashCode()
92     */
93    @Override
94    public int hashCode()
95     {
96      return Integer.hashCode(this.percent);
97     }
98  
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 final Percent other))
115      {
116       return false;
117      }
118     return this.percent == other.percent;
119    }
120 
121 
122   /**
123    * Returns the string representation of this Percent.
124    *
125    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
126    *
127    * "Percent[percent=0]"
128    *
129    * @return String representation of this Percent
130    * @see java.lang.Object#toString()
131    */
132   @Override
133   public String toString()
134    {
135     final var builder = new StringBuilder();
136     builder.append("Percent[percent=").append(this.percent).append(']'); //$NON-NLS-1$
137     return builder.toString();
138    }
139 
140 
141   /**
142    * Compare with another object.
143    *
144    * @param obj Object to compare with
145    * @return 0: equal; 1: greater; -1: smaller
146    * @see java.lang.Comparable#compareTo(java.lang.Object)
147    */
148   @Override
149   public int compareTo(final Percent obj)
150    {
151     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
152     return Integer.compare(this.percent, obj.percent);
153    }
154 
155  }