View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.internal.util;
17  
18  /**
19   * This class is from javapractices.com:
20   * 
21   * http://www.javapractices.com/Topic17.cjp
22   * 
23   * Collected methods which allow easy implementation of <code>equals</code>.
24   * 
25   * Example use case in a class called Car:
26   * 
27   * <pre>
28   * public boolean equals(Object that) {
29   *     if (this == that)
30   *         return true;
31   *     if (!(that instanceof Car))
32   *         return false;
33   *     Car thatCar = (Car) that;
34   *     return EqualsUtil.areEqual(this.fName, that.fName)
35   *             &amp;&amp; EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors)
36   *             &amp;&amp; EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage)
37   *             &amp;&amp; EqualsUtil.areEqual(this.fColor, that.fColor)
38   *             &amp;&amp; Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks); //array!
39   * }
40   * </pre>
41   * 
42   * <em>Arrays are not handled by this class</em>. This is because the
43   * <code>Arrays.equals</code> methods should be used for array fields.
44   */
45  public final class EqualsUtil {
46  
47      /**
48       * Are equal.
49       *
50       * @param aThis
51       *            the a this
52       * @param aThat
53       *            the a that
54       * @return true, if successful
55       */
56      static public boolean areEqual(boolean aThis, boolean aThat) {
57          return aThis == aThat;
58      }
59  
60      /**
61       * Are equal.
62       *
63       * @param aThis
64       *            the a this
65       * @param aThat
66       *            the a that
67       * @return true, if successful
68       */
69      static public boolean areEqual(char aThis, char aThat) {
70          return aThis == aThat;
71      }
72  
73      /**
74       * Are equal.
75       *
76       * @param aThis
77       *            the a this
78       * @param aThat
79       *            the a that
80       * @return true, if successful
81       */
82      static public boolean areEqual(long aThis, long aThat) {
83          /*
84           * Implementation Note Note that byte, short, and int are handled by
85           * this method, through implicit conversion.
86           */
87          return aThis == aThat;
88      }
89  
90      /**
91       * Are equal.
92       *
93       * @param aThis
94       *            the a this
95       * @param aThat
96       *            the a that
97       * @return true, if successful
98       */
99      static public boolean areEqual(float aThis, float aThat) {
100         return Float.floatToIntBits(aThis) == Float.floatToIntBits(aThat);
101     }
102 
103     /**
104      * Are equal.
105      *
106      * @param aThis
107      *            the a this
108      * @param aThat
109      *            the a that
110      * @return true, if successful
111      */
112     static public boolean areEqual(double aThis, double aThat) {
113         return Double.doubleToLongBits(aThis) == Double.doubleToLongBits(aThat);
114     }
115 
116     /**
117      * Possibly-null object field.
118      * 
119      * Includes type-safe enumerations and collections, but does not include arrays. See class comment.
120      *
121      * @param aThis
122      *            the a this
123      * @param aThat
124      *            the a that
125      * @return true, if successful
126      */
127     static public boolean areEqual(Object aThis, Object aThat) {
128         return aThis == null ? aThat == null : aThis.equals(aThat);
129     }
130 }