View Javadoc
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   * Screen size.
14   *
15   * Not DSGVO relevant.
16   *
17   * TODO min, max
18   */
19  public final class ScreenSize implements Comparable<ScreenSize>, IValueObject
20   {
21    /* *
22     * Cache for singletons.
23     */
24    // private static final Map<NTuple3<Integer, Integer, String>, ScreenSize> CACHE = new WeakHashMap<>();
25  
26    /**
27     * Screen width (1-8192).
28     */
29    private final int width;
30  
31    /**
32     * Screen height (1-8192).
33     */
34    private final int height;
35  
36    /**
37     * Screen size name.
38     */
39    private final String name;
40  
41  
42    /**
43     * Constructor.
44     *
45     * @param width Screen width in pixel (1-8192)
46     * @param height Screen height in pixel (1-8192)
47     * @param name Screen size name
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)"); //$NON-NLS-1$
55       }
56      if ((height < 1) || (height > 8192.0))
57       {
58        throw new IndexOutOfBoundsException("Height out of range (1-8192)"); //$NON-NLS-1$
59       }
60      Objects.requireNonNull(name);
61      this.width = width;
62      this.height = height;
63      this.name = name;
64     }
65  
66  
67    /**
68     * ScreenSize factory.
69     *
70     * @param width Screen width in pixel (1-8192)
71     * @param height Screen height in pixel (1-8192)
72     * @param name Screen size name
73     * @return ScreenSize
74     */
75    public static ScreenSize of(final int width, final int height, final String name)
76     {
77      /*
78      if ((width < 1) || (width > 8192.0))
79       {
80        throw new IndexOutOfBoundsException("Width out of range (1-8192)"); //$NON-NLS-1$
81       }
82      if ((height < 1) || (height > 8192.0))
83       {
84        throw new IndexOutOfBoundsException("Height out of range (1-8192)"); //$NON-NLS-1$
85       }
86      final NTuple3<Integer, Integer, String> tuple = NTuple3.of(width, height, name);
87      synchronized (ScreenSize.class)
88       {
89        ScreenSize obj = ScreenSize.CACHE.get(tuple);
90        if (obj != null)
91         {
92          return obj;
93         }
94        obj = new ScreenSize(width, height, name);
95        ScreenSize.CACHE.put(tuple, obj);
96        return obj;
97       }
98      */
99      return new ScreenSize(width, height, name);
100    }
101 
102 
103   /**
104    * ScreenSize factory.
105    *
106    * @param value width (1-8192) x height (1-8192)
107    * @return ScreenSize
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    * Get screen width.
122    *
123    * @return Screen width in pixel
124    */
125   public int getWidth()
126    {
127     return this.width;
128    }
129 
130 
131   /**
132    * Get screen height.
133    *
134    * @return Screen height in pixel
135    */
136   public int getHeight()
137    {
138     return this.height;
139    }
140 
141 
142   /**
143    * Returns the value of this ScreenSize as a string.
144    *
145    * @return The text value represented by this object after conversion to type string format 320x200.
146    */
147   @Override
148   public String stringValue()
149    {
150     return String.valueOf(this.width) + 'x' + this.height;
151    }
152 
153 
154   /**
155    * Get screen size name.
156    *
157    * @return Screen size name
158    */
159   public String getName()
160    {
161     return this.name;
162    }
163 
164 
165   /**
166    * Calculate hash code.
167    *
168    * @return Hash
169    * @see java.lang.Object#hashCode()
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    * Is equal with another object.
182    *
183    * @param obj Object
184    * @return true when equal, false otherwise
185    * @see java.lang.Object#equals(java.lang.Object)
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    * Returns the string representation of this ScreenSize.
205    *
206    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
207    *
208    * "ScreenSize[width=320, height=200, name=QVGA]"
209    *
210    * @return String representation of this ScreenSize
211    * @see java.lang.Object#toString()
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(']'); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
218     return builder.toString();
219    }
220 
221 
222   /**
223    * Compare with another object.
224    *
225    * @param obj Object to compare with
226    * @return 0: equal; 1: greater; -1: smaller
227    * @see java.lang.Comparable#compareTo(java.lang.Object)
228    */
229   @Override
230   public int compareTo(final ScreenSize obj)
231    {
232     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
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  }