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