View Javadoc
1   /*
2    * Copyright (C) 2022-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.containers;
5   
6   
7   import java.util.Objects;
8   
9   
10  /**
11   * N-Tuple 2.
12   *
13   * @param <T1> Type 1
14   * @param <T2> Type 2
15   */
16  @SuppressWarnings({"checkstyle:ClassTypeParameterName", "checkstyle:MethodTypeParameterName", "PMD.GenericsNaming"})
17  public final class NTuple2<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<NTuple2<T1, T2>>
18   {
19    /* *
20     * Cache for singletons.
21     */
22    // private static final Map<?, NTuple2<T1 extends Comparable<T1>, T2 extends Comparable<T2>>> CACHE = new ConcurrentHashMap<>();
23  
24    /**
25     * Object1 of type T1.
26     */
27    private final T1 object1;
28  
29    /**
30     * Object2 of type T2.
31     */
32    private final T2 object2;
33  
34  
35    /**
36     * Private constructor.
37     *
38     * @param obj1 Object 1 of type T1
39     * @param obj2 Object 2 of Type T2
40     */
41    private NTuple2(final T1 obj1, final T2 obj2)
42     {
43      super();
44      Objects.requireNonNull(obj1, "obj1 is null"); //$NON-NLS-1$
45      Objects.requireNonNull(obj2, "obj2 is null"); //$NON-NLS-1$
46      this.object1 = obj1;
47      this.object2 = obj2;
48     }
49  
50  
51    /**
52     * NTuple2 factory.
53     *
54     * @param <T1> Type 1
55     * @param <T2> Type 2
56     * @param obj1 Object 1 of type T1
57     * @param obj2 Object 2 of Type T2
58     * @return NTuple2 object
59     */
60    public static <T1 extends Comparable<T1>, T2 extends Comparable<T2>> NTuple2<T1, T2> of(final T1 obj1, final T2 obj2)
61     {
62      return new NTuple2<>(obj1, obj2);
63     }
64  
65  
66  
67    /**
68     * Returns the first value of this NTuple2 as a T1.
69     *
70     * @return The T1 value represented by this object.
71     */
72    public T1 t1Value()
73     {
74      return this.object1;
75     }
76  
77  
78    /**
79     * Returns the second value of this NTuple2 as a T2.
80     *
81     * @return The T2 value represented by this object.
82     */
83    public T2 t2Value()
84     {
85      return this.object2;
86     }
87  
88  
89    /**
90     * Calculate hash code.
91     *
92     * @return Hash
93     * @see java.lang.Object#hashCode()
94     */
95    @Override
96    public int hashCode()
97     {
98      return Objects.hash(this.object1, this.object2);
99     }
100 
101 
102   /**
103    * Is equal with another object.
104    *
105    * @param obj Object
106    * @return true when equal, false otherwise
107    * @see java.lang.Object#equals(java.lang.Object)
108    */
109   @Override
110   public boolean equals(final Object obj)
111    {
112     if (this == obj)
113      {
114       return true;
115      }
116     if (!(obj instanceof NTuple2))
117      {
118       return false;
119      }
120     final NTuple2<T1, T2> other = (NTuple2<T1, T2>)obj;
121     boolean result = this.object1.equals(other.object1);
122     if (result)
123      {
124       result = this.object2.equals(other.object2);
125      }
126     return result;
127    }
128 
129 
130   /**
131    * Returns the string representation of this NTuple2.
132    *
133    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
134    *
135    * "NTuple2[object1=..., object2=...]"
136    *
137    * @return String representation of this NTuple2
138    * @see java.lang.Object#toString()
139    */
140   @Override
141   public String toString()
142    {
143     final var builder = new StringBuilder(27);
144     builder.append("NTuple2[object1=").append(this.object1).append(", object2=").append(this.object2).append(']'); //$NON-NLS-1$ //$NON-NLS-2$
145     return builder.toString();
146    }
147 
148 
149   /**
150    * Compare with another object.
151    *
152    * @param obj Object to compare with
153    * @return 0: equal; 1: greater; -1: smaller
154    * @see java.lang.Comparable#compareTo(java.lang.Object)
155    */
156   @Override
157   public int compareTo(final NTuple2<T1, T2> obj)
158    {
159     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
160     int result = this.object1.compareTo(obj.object1);
161     if (result == 0)
162      {
163       result = this.object2.compareTo(obj.object2);
164      }
165     return result;
166    }
167 
168  }