1
2
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
14
15
16
17
18
19 public final class ScreenSize implements Comparable<ScreenSize>, IValueObject
20 {
21
22
23
24
25
26
27
28
29 private final int width;
30
31
32
33
34 private final int height;
35
36
37
38
39 private final String name;
40
41
42
43
44
45
46
47
48
49 private ScreenSize(final int width, final int height, final String name)
50 {
51 super();
52 if ((width < 1) || (width > 8192.0))
53 {
54 throw new IndexOutOfBoundsException("Width out of range (1-8192)");
55 }
56 if ((height < 1) || (height > 8192.0))
57 {
58 throw new IndexOutOfBoundsException("Height out of range (1-8192)");
59 }
60 Objects.requireNonNull(name);
61 this.width = width;
62 this.height = height;
63 this.name = name;
64 }
65
66
67
68
69
70
71
72
73
74
75 public static ScreenSize of(final int width, final int height, final String name)
76 {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 return new ScreenSize(width, height, name);
100 }
101
102
103
104
105
106
107
108
109 public static ScreenSize of(final String value)
110 {
111 final String[] values = value.split("x");
112 if (values.length != 2)
113 {
114 throw new IllegalArgumentException("value not of expected format");
115 }
116 return of(Integer.parseInt(values[0]), Integer.parseInt(values[1]), "");
117 }
118
119
120
121
122
123
124
125 public int getWidth()
126 {
127 return this.width;
128 }
129
130
131
132
133
134
135
136 public int getHeight()
137 {
138 return this.height;
139 }
140
141
142
143
144
145
146
147 @Override
148 public String stringValue()
149 {
150 return String.valueOf(this.width) + 'x' + this.height;
151 }
152
153
154
155
156
157
158
159 public String getName()
160 {
161 return this.name;
162 }
163
164
165
166
167
168
169
170
171 @Override
172 public int hashCode()
173 {
174 int result = Integer.hashCode(this.width);
175 result = (31 * result) + Integer.hashCode(this.height);
176 return (31 * result) + this.name.hashCode();
177 }
178
179
180
181
182
183
184
185
186
187 @Override
188 public boolean equals(final Object obj)
189 {
190 if (this == obj)
191 {
192 return true;
193 }
194 if (!(obj instanceof ScreenSize))
195 {
196 return false;
197 }
198 final ScreenSize other = (ScreenSize)obj;
199 return (this.width == other.width) && (this.height == other.height) && this.name.equals(other.name);
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213 @Override
214 public String toString()
215 {
216 final var builder = new StringBuilder(47);
217 builder.append("ScreenSize[width=").append(this.width).append(", height=").append(this.height).append(", name=").append(this.name).append(']');
218 return builder.toString();
219 }
220
221
222
223
224
225
226
227
228
229 @Override
230 public int compareTo(final ScreenSize obj)
231 {
232 Objects.requireNonNull(obj, "obj");
233 int result = Integer.compare(this.width, obj.width);
234 if (result == 0)
235 {
236 result = Integer.compare(this.height, obj.height);
237 if (result == 0)
238 {
239 result = this.name.compareTo(obj.name);
240 }
241 }
242 return result;
243 }
244
245 }