001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.core.util;
022
023/**
024 * A set of helper methods which return true if the two parameters are
025 * equal to each other.
026 *
027 * @see #classEqual(Object, Object) For how to use this class
028 *
029 * @author ayates
030 */
031public class Equals {
032
033        public static boolean equal(int one, int two) {
034                return one == two;
035        }
036
037        public static boolean equal(long one, long two) {
038                return (one == two);
039        }
040
041        public static boolean equal(boolean one, boolean two) {
042                return one == two;
043        }
044
045        /**
046         * Does not compare class types.
047         * @see #classEqual(Object, Object)
048         */
049        public static boolean equal(Object one, Object two) {
050                return one == null && two == null || !(one == null || two == null) && (one == two || one.equals(two));
051        }
052
053        /**
054         * This method should be called before beginning any equals methods. In order
055         * to return true the method:
056         *
057         * <ol>
058         * <li>The two given objects are the same instance using ==. This also means
059         * if both Objects are null then this method will return true (well
060         * technically they are equal)</li>
061         * <li>Tests that neither object is null</li>
062         * <li>The the two classes from the objects are equal using ==</li>
063         * </ol>
064         *
065         * The boilerplate using this method then becomes:
066         *
067         * <pre>
068         * boolean equals = false;
069         * if (EqualsHelper.classEqual(this, obj)) {
070         *   TargetClass casted = (TargetClass) obj;
071         *   equals = (EqualsHelper.equal(this.getId(), casted.getId()) &amp;&amp; EqualsHelper
072         *       .equal(this.getName(), casted.getName()));
073         * }
074         * return equals;
075         * </pre>
076         *
077         * @param one
078         *          The first object to test
079         * @param two
080         *          The second object to test
081         * @return A boolean indicating if the logic agrees that these two objects are
082         *         equal at the class level
083         */
084        public static boolean classEqual(Object one, Object two) {
085                return one == two || !(one == null || two == null) && one.getClass() == two.getClass();
086        }
087}