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         * However, if the two arguments are non-null references to distinct objects,
048         *  the object's equals() method is called - which may well compare class types.
049         * @see #classEqual(Object, Object)
050         */
051        public static boolean equal(Object one, Object two) {
052                return one == null && two == null || !(one == null || two == null) && (one == two || one.equals(two));
053        }
054
055        /**
056         * This method should be called before beginning any equals methods. In order
057         * to return true the method:
058         *
059         * <ol>
060         * <li>The two given objects are the same instance using ==. This also means
061         * if both Objects are null then this method will return true (well
062         * technically they are equal)</li>
063         * <li>Tests that neither object is null</li>
064         * <li>The the two classes from the objects are equal using ==</li>
065         * </ol>
066         *
067         * The boilerplate using this method then becomes:
068         *
069         * <pre>
070         * boolean equals = false;
071         * if (EqualsHelper.classEqual(this, obj)) {
072         *   TargetClass casted = (TargetClass) obj;
073         *   equals = (EqualsHelper.equal(this.getId(), casted.getId()) &amp;&amp; EqualsHelper
074         *       .equal(this.getName(), casted.getName()));
075         * }
076         * return equals;
077         * </pre>
078         *
079         * @param one
080         *          The first object to test
081         * @param two
082         *          The second object to test
083         * @return A boolean indicating if  these two objects are
084         *         equal at the class level
085         */
086        public static boolean classEqual(Object one, Object two) {
087                return one == two || !(one == null || two == null) && one.getClass() == two.getClass();
088        }
089}