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 * Display aspect ratio.
14 *
15 * 1:1 2.1:1 3:1 3:2 4:3 5:3 5:4 8:5 9:5 10:6 15:9 16:9 16:10 17:10 25:12 25:16 60:29 64:35 72:35
16 */
17 public final class DisplayAspectRatio implements Comparable<DisplayAspectRatio>, IValueObject
18 {
19 /* *
20 * Cache for singletons.
21 */
22 // private static final Map<NTuple2<Integer, Integer>, DisplayAspectRatio> CACHE = new WeakHashMap<>();
23
24 /**
25 * Display x size (1-72).
26 */
27 private final int x;
28
29 /**
30 * Display y size (1-35).
31 */
32 private final int y;
33
34
35 /**
36 * Constructor.
37 *
38 * @param x Display x site (1-72)
39 * @param y Display y size (1-35)
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)"); //$NON-NLS-1$
47 }
48 if ((y <= 0) || (y > 35))
49 {
50 throw new IndexOutOfBoundsException("y out of range (1-35)"); //$NON-NLS-1$
51 }
52 // verify existing aspect ratios
53 this.x = x;
54 this.y = y;
55 }
56
57
58 /**
59 * Display aspect ration factory.
60 *
61 * @param x Display x site (1-72)
62 * @param y Display y size (1-35)
63 * @return DisplayAspectRatio object
64 */
65 public static DisplayAspectRatio of(final int x, final int y)
66 {
67 /*
68 final NTuple2<Integer, Integer> tuple = NTuple2.of(x, y);
69 synchronized (DisplayAspectRatio.class)
70 {
71 DisplayAspectRatio obj = DisplayAspectRatio.CACHE.get(tuple);
72 if (obj != null)
73 {
74 return obj;
75 }
76 obj = new DisplayAspectRatio(x, y);
77 DisplayAspectRatio.CACHE.put(tuple, obj);
78 return obj;
79 }
80 */
81 return new DisplayAspectRatio(x, y);
82 }
83
84
85 /**
86 * Display aspect ration factory.
87 *
88 * @param value String value of type x:y
89 * @return DisplayAspectRatio object
90 * @throws IllegalArgumentException If not of correct format
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 * Get display x size.
105 *
106 * @return X size
107 */
108 public int getX()
109 {
110 return this.x;
111 }
112
113
114 /**
115 * Get display y size.
116 *
117 * @return Y size
118 */
119 public int getY()
120 {
121 return this.y;
122 }
123
124
125 /**
126 * Returns the value of this DisplayAspectRatio as a string.
127 *
128 * @return The text value represented by this object after conversion to type string.
129 */
130 @Override
131 public String stringValue()
132 {
133 return String.valueOf(this.x) + ':' + this.y;
134 }
135
136
137 /**
138 * Calculate hash code.
139 *
140 * @return Hash
141 * @see java.lang.Object#hashCode()
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 * Is equal with another object.
153 *
154 * @param obj Object
155 * @return true when equal, false otherwise
156 * @see java.lang.Object#equals(java.lang.Object)
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 * Returns the string representation of this DisplayAspectRatio.
176 *
177 * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
178 *
179 * "DisplayAspectRatio[x=1, y=1]"
180 *
181 * @return String representation of this DisplayAspectRatio
182 * @see java.lang.Object#toString()
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(']'); //$NON-NLS-1$ //$NON-NLS-2$
189 return builder.toString();
190 }
191
192
193 /**
194 * Compare with another object.
195 *
196 * @param obj Object to compare with
197 * @return 0: equal; 1: greater; -1: smaller
198 * @see java.lang.Comparable#compareTo(java.lang.Object)
199 */
200 @Override
201 public int compareTo(final DisplayAspectRatio obj)
202 {
203 Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
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 }