Equality.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.internal.util;
021 
022 import java.util.Arrays;
023 import java.util.Objects;
024 import java.util.function.Predicate;
025 
026 import org.jenetics.util.Seq;
027 
028 /**
029  * Helper object for calculating object equality.
030  *
031  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
032  * @since 3.0
033  * @version 3.0
034  */
035 public final class Equality {
036     private Equality() {require.noInstance();}
037 
038     public static final Predicate<Object> TRUE = a -> true;
039 
040     /**
041      * Create a new {@code Equality} object for testing object equality.
042      *
043      * @param self the {@code this} object to test; must not be {@code null}
044      * @param other the {@code other} object to test; maybe {@code null}
045      * @param <T> the object type
046      * @return the {@code Predicate} object for equality testing
047      * @throws java.lang.NullPointerException if the {@code self} parameter is
048      *         {@code null}
049      */
050     @SuppressWarnings("unchecked")
051     public static <T> Predicate<Predicate<T>>
052     of(final T self, final Object other) {
053         Objects.requireNonNull(self);
054         return self == other ?
055             p -> true :
056             other == null || self.getClass() != other.getClass() ?
057                 p -> false : p -> p.test((T)other);
058     }
059 
060     /**
061      * Check if the given arguments are from the same type.
062      *
063      * @param self the {@code this} object to test; must not be {@code null}
064      * @param other the {@code other} object to test; maybe {@code null}
065      * @return {@code true} if the two objects are from the same type
066      * @throws java.lang.NullPointerException if the {@code self} parameter is
067      *         {@code null}
068      */
069     public static boolean ofType(final Object self, final Object other) {
070         Objects.requireNonNull(self);
071         return self == other ||
072             other != null && self.getClass() == other.getClass();
073     }
074 
075     /**
076      * Compares the two given {@code boolean} values.
077      *
078      * @param a first value to compare.
079      * @param b second value to compare.
080      * @return {@code true} if the given values are equal, {@code false}
081      *          otherwise.
082      */
083     public static boolean eq(final boolean a, final boolean b) {
084         return a == b;
085     }
086 
087     /**
088      * Compares the two given {@code boolean} arrays.
089      *
090      * @param a first value to compare.
091      * @param b second value to compare.
092      * @return {@code true} if the given values are equal, {@code false}
093      *          otherwise.
094      */
095     public static boolean eq(final boolean[] a, final boolean[] b) {
096         return Arrays.equals(a, b);
097     }
098 
099     /**
100      * Compares the two given {@code byte} values.
101      *
102      * @param a first value to compare.
103      * @param b second value to compare.
104      * @return {@code true} if the given values are equal, {@code false}
105      *          otherwise.
106      */
107     public static boolean eq(final byte a, final byte b) {
108         return a == b;
109     }
110 
111     /**
112      * Compares the two given {@code byte} arrays.
113      *
114      * @param a first value to compare.
115      * @param b second value to compare.
116      * @return {@code true} if the given values are equal, {@code false}
117      *          otherwise.
118      */
119     public static boolean eq(final byte[] a, final byte[] b) {
120         return Arrays.equals(a, b);
121     }
122 
123     /**
124      * Compares the two given {@code char} values.
125      *
126      * @param a first value to compare.
127      * @param b second value to compare.
128      * @return {@code true} if the given values are equal, {@code false}
129      *          otherwise.
130      */
131     public static boolean eq(final char a, final char b) {
132         return a == b;
133     }
134 
135     /**
136      * Compares the two given {@code char} arrays.
137      *
138      * @param a first value to compare.
139      * @param b second value to compare.
140      * @return {@code true} if the given values are equal, {@code false}
141      *          otherwise.
142      */
143     public static boolean eq(final char[] a, final char[] b) {
144         return Arrays.equals(a, b);
145     }
146 
147     /**
148      * Compares the two given {@code short} values.
149      *
150      * @param a first value to compare.
151      * @param b second value to compare.
152      * @return {@code true} if the given values are equal, {@code false}
153      *          otherwise.
154      */
155     public static boolean eq(final short a, final short b) {
156         return a == b;
157     }
158 
159     /**
160      * Compares the two given {@code short} arrays.
161      *
162      * @param a first value to compare.
163      * @param b second value to compare.
164      * @return {@code true} if the given values are equal, {@code false}
165      *          otherwise.
166      */
167     public static boolean eq(final short[] a, final short[] b) {
168         return Arrays.equals(a, b);
169     }
170 
171     /**
172      * Compares the two given {@code int} values.
173      *
174      * @param a first value to compare.
175      * @param b second value to compare.
176      * @return {@code true} if the given values are equal, {@code false}
177      *          otherwise.
178      */
179     public static boolean eq(final int a, final int b) {
180         return a == b;
181     }
182 
183     /**
184      * Compares the two given {@code int} arrays.
185      *
186      * @param a first value to compare.
187      * @param b second value to compare.
188      * @return {@code true} if the given values are equal, {@code false}
189      *          otherwise.
190      */
191     public static boolean eq(final int[] a, final int[] b) {
192         return Arrays.equals(a, b);
193     }
194 
195     /**
196      * Compares the two given {@code long} values.
197      *
198      * @param a first value to compare.
199      * @param b second value to compare.
200      * @return {@code true} if the given values are equal, {@code false}
201      *          otherwise.
202      */
203     public static boolean eq(final long a, final long b) {
204         return a == b;
205     }
206 
207     /**
208      * Compares the two given {@code long} arrays.
209      *
210      * @param a first value to compare.
211      * @param b second value to compare.
212      * @return {@code true} if the given values are equal, {@code false}
213      *          otherwise.
214      */
215     public static boolean eq(final long[] a, final long[] b) {
216         return Arrays.equals(a, b);
217     }
218 
219     /**
220      * Compares the two given {@code float} values.
221      *
222      * @param a first value to compare.
223      * @param b second value to compare.
224      * @return {@code true} if the given values are equal, {@code false}
225      *          otherwise.
226      */
227     public static boolean eq(final float a, final float b) {
228         return Float.floatToIntBits(a) == Float.floatToIntBits(b);
229     }
230 
231     /**
232      * Compares the two given {@code float} arrays.
233      *
234      * @param a first value to compare.
235      * @param b second value to compare.
236      * @return {@code true} if the given values are equal, {@code false}
237      *          otherwise.
238      */
239     public static boolean eq(final float[] a, final float[] b) {
240         return Arrays.equals(a, b);
241     }
242 
243     /**
244      * Compares the two given {@code double} values.
245      *
246      * @param a first value to compare.
247      * @param b second value to compare.
248      * @return {@code true} if the given values are equal, {@code false}
249      *          otherwise.
250      */
251     public static boolean eq(final double a, final double b) {
252         return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
253     }
254 
255     /**
256      * Compares the two given {@code double} arrays.
257      *
258      * @param a first value to compare.
259      * @param b second value to compare.
260      * @return {@code true} if the given values are equal, {@code false}
261      *          otherwise.
262      */
263     public static boolean eq(final double[] a, final double[] b) {
264         return Arrays.equals(a, b);
265     }
266 
267     /**
268      * Compares the two given {@code Enum} values.
269      *
270      * @param a first value to compare.
271      * @param b second value to compare.
272      * @return {@code true} if the given values are equal, {@code false}
273      *          otherwise.
274      */
275     public static boolean eq(final Enum<?> a, final Enum<?> b) {
276         return a == b;
277     }
278 
279     /**
280      * Compares the two given {@code Object} values.
281      *
282      * @param a first value to compare.
283      * @param b second value to compare.
284      * @return {@code true} if the given values are equal, {@code false}
285      *          otherwise.
286      */
287     public static boolean eq(final Object a, final Object b) {
288         return a != null ? a.equals(b) : b == null;
289     }
290 
291     /**
292      * Compares the two given {@code Object} arrays.
293      *
294      * @param a first value to compare.
295      * @param b second value to compare.
296      * @return {@code true} if the given values are equal, {@code false}
297      *          otherwise.
298      */
299     public static boolean eq(final Object[] a, final Object[] b) {
300         return Arrays.equals(a, b);
301     }
302 
303     /**
304      * Compares the two given {@code Seq} values.
305      *
306      * @param a first value to compare.
307      * @param b second value to compare.
308      * @return {@code true} if the given values are equal, {@code false}
309      *          otherwise.
310      */
311     public static boolean eq(final Seq<?> a, final Seq<?> b) {
312         return Seq.equals(a, b);
313     }
314 }