    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ------------------
    028     * HashUtilities.java
    029     * ------------------
    030     * (C) Copyright 2006, 2007, by Object Refinery Limited;
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: HashUtilities.java,v 1.1.2.2 2007/03/06 16:00:44 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 03-Oct-2006 : Version 1 (DG);
    040     * 06-Mar-2007 : Fix for hashCodeForDoubleArray() method (DG);
    041     *
    042     */
    043
    044    package org.jfree.chart;
    045
    046    import java.awt.GradientPaint;
    047    import java.awt.Paint;
    048
    049    /**
    050     * Some utility methods for calculating hash codes.
    051     *
    052     * @since 1.0.3
    053     */
    054    public class HashUtilities {
    055
    056        /**
    057         * Returns a hash code for a <code>Paint</code> instance.  If
    058         * <code>p</code> is <code>null</code>, this method returns zero.
    059         *
    060         * @param p  the paint (<code>null</code> permitted).
    061         *
    062         * @return The hash code.
    063         */
    064        public static int hashCodeForPaint(Paint p) {
    065            if (p == null)
    066                return 0;
    067            int result = 0;
    068            // handle GradientPaint as a special case
    069            if (p instanceof GradientPaint) {
    070                GradientPaint gp = (GradientPaint) p;
    071                result = 193;
    072                result = 37 * result + gp.getColor1().hashCode();
    073                result = 37 * result + gp.getPoint1().hashCode();
    074                result = 37 * result + gp.getColor2().hashCode();
    075                result = 37 * result + gp.getPoint2().hashCode();
    076            }
    077            else {
    078                // we assume that all other Paint instances implement equals() and
    079                // hashCode()...of course that might not be true, but what can we
    080                // do about it?
    081                result = p.hashCode();
    082            }
    083            return result;
    084        }
    085
    086        /**
    087         * Returns a hash code for a <code>double[]</code> instance.  If the array
    088         * is <code>null</code>, this method returns zero.
    089         *
    090         * @param a  the array (<code>null</code> permitted).
    091         *
    092         * @return The hash code.
    093         */
    094        public static int hashCodeForDoubleArray(double[] a) {
    095            if (a == null) {
    096                return 0;
    097            }
    098            int result = 193;
    099            long temp;
    100            for (int i = 0; i < a.length; i++) {
    101                temp = Double.doubleToLongBits(a[i]);
    102                result = 29 * result + (int) (temp ^ (temp >>> 32));
    103            }
    104            return result;
    105        }
    106
    107    }