001 package org.biojava3.core.util;
002
003 /**
004 * A set of helper methods which return true if the two parameters are
005 * equal to each other.
006 *
007 * @author ayates
008 */
009 public class Equals {
010
011 public static boolean equal(int one, int two) {
012 return (one == two);
013 }
014
015 public static boolean equal(long one, long two) {
016 return (one == two);
017 }
018
019 public static boolean equal(boolean one, boolean two) {
020 return (one == two);
021 }
022
023 public static boolean equal(Object one, Object two) {
024 boolean equal = false;
025 //Both are null means they're equal
026 if (one == null && two == null) {
027 equal = true;
028 }
029 else if (one == null || two == null) {
030 equal = false;
031 }
032 //True only if they are the same object
033 else if (one == two) {
034 equal = true;
035 }
036 //Otherwise ask the Object on their equals method.
037 else {
038 equal = one.equals(two);
039 }
040 return equal;
041 }
042
043 /**
044 * This method should be called before beginning any equals methods. In order
045 * to return true the method:
046 *
047 * <ol>
048 * <li>The two given objects are the same instance using ==. This also means
049 * if both Objects are null then this method will return true (well
050 * technically they are equal)</li>
051 * <li>Tests that neither object is null</li>
052 * <li>The the two classes from the objects are equal using ==</li>
053 * </ol>
054 *
055 * The boilerplate using this method then becomes:
056 *
057 * <pre>
058 * boolean equals = false;
059 * if (EqualsHelper.classEqual(this, obj)) {
060 * TargetClass casted = (TargetClass) obj;
061 * equals = (EqualsHelper.equal(this.getId(), casted.getId()) && EqualsHelper
062 * .equal(this.getName(), casted.getName()));
063 * }
064 * return equals;
065 * </pre>
066 *
067 * @param one
068 * The first object to test
069 * @param two
070 * The second object to test
071 * @return A boolean indicating if the logic agrees that these two objects are
072 * equal at the class level
073 */
074 public static boolean classEqual(Object one, Object two) {
075
076 boolean equal = false;
077
078 if (one == two) {
079 equal = true;
080 }
081 else if (one == null || two == null) {
082 equal = false;
083 }
084 else {
085 equal = (one.getClass() == two.getClass());
086 }
087
088 return equal;
089 }
090 }