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