1
2
3
4 package de.powerstat.validation.values;
5
6 import java.util.Objects;
7
8 import de.powerstat.validation.interfaces.IValueObject;
9
10
11
12
13
14
15 public final class Percent implements Comparable<Percent>, IValueObject
16 {
17
18
19
20 private final int percent;
21
22
23
24
25
26
27
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)!");
35 }
36 this.percent = percent;
37 }
38
39
40
41
42
43
44
45
46 public static Percent of(final int percent)
47 {
48 return new Percent(percent);
49 }
50
51
52
53
54
55
56
57
58 public static Percent of(final String percent)
59 {
60 return of(Integer.parseInt(percent));
61 }
62
63
64
65
66
67
68
69 public int intValue()
70 {
71 return this.percent;
72 }
73
74
75
76
77
78
79
80 @Override
81 public String stringValue()
82 {
83 return String.valueOf(this.percent);
84 }
85
86
87
88
89
90
91
92
93 @Override
94 public int hashCode()
95 {
96 return Integer.hashCode(this.percent);
97 }
98
99
100
101
102
103
104
105
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
124
125
126
127
128
129
130
131
132 @Override
133 public String toString()
134 {
135 final var builder = new StringBuilder();
136 builder.append("Percent[percent=").append(this.percent).append(']');
137 return builder.toString();
138 }
139
140
141
142
143
144
145
146
147
148 @Override
149 public int compareTo(final Percent obj)
150 {
151 Objects.requireNonNull(obj, "obj");
152 return Integer.compare(this.percent, obj.percent);
153 }
154
155 }