/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 *
 * -------------------
 * PaintUtils.java
 * -------------------
 * (C) Copyright 2003-2016, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: PaintUtils.java,v 1.10 2007/11/02 17:50:37 taqua Exp $
 *
 * Changes
 * -------
 * 13-Nov-2003 : Version 1 (DG);
 * 04-Oct-2004 : Renamed PaintUtils --> PaintUtils (DG);
 * 23-Feb-2005 : Rewrote equal() method with less indenting required (DG);
 * 16-Jun-2012 : Moved from JCommon to JFreeChart (DG);
 * 12-Jan-2016 : Add handling for LinearGradientPaint and 
 *               RadialGradientPaint (DG);
 *
 */

package org.jfree.chart.util;

import java.awt.GradientPaint;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.util.Arrays;
import java.util.Map;

/**
 * Utility code that relates to {@code Paint} objects.
 */
public class PaintUtils {

    /**
     * Private constructor prevents object creation.
     */
    private PaintUtils() {
    }

    /**
     * Returns {@code true} if the two {@code Paint} objects are equal 
     * OR both {@code null}.  This method handles
     * {@code GradientPaint} as a special case.
     *
     * @param p1  paint 1 ({@code null} permitted).
     * @param p2  paint 2 ({@code null} permitted).
     *
     * @return A boolean.
     */
    public static boolean equal(Paint p1, Paint p2) {
        // handle cases where either or both arguments are null
        if (p1 == null) {
            return (p2 == null);   
        }
        if (p2 == null) {
            return false;   
        }

        // handle GradientPaint as a special case...
        if (p1 instanceof GradientPaint && p2 instanceof GradientPaint) {
            GradientPaint gp1 = (GradientPaint) p1;
            GradientPaint gp2 = (GradientPaint) p2;
            return gp1.getColor1().equals(gp2.getColor1()) 
                    && gp1.getColor2().equals(gp2.getColor2())
                    && gp1.getPoint1().equals(gp2.getPoint1())    
                    && gp1.getPoint2().equals(gp2.getPoint2())
                    && gp1.isCyclic() == gp2.isCyclic()
                    && gp1.getTransparency() == gp1.getTransparency(); 
        } else if (p1 instanceof LinearGradientPaint 
                && p2 instanceof LinearGradientPaint) {
            LinearGradientPaint lgp1 = (LinearGradientPaint) p1;
            LinearGradientPaint lgp2 = (LinearGradientPaint) p2;
            return lgp1.getStartPoint().equals(lgp2.getStartPoint())
                    && lgp1.getEndPoint().equals(lgp2.getEndPoint()) 
                    && Arrays.equals(lgp1.getFractions(), lgp2.getFractions())
                    && Arrays.equals(lgp1.getColors(), lgp2.getColors())
                    && lgp1.getCycleMethod() == lgp2.getCycleMethod();
        } else if (p1 instanceof RadialGradientPaint 
                && p2 instanceof RadialGradientPaint) {
            RadialGradientPaint rgp1 = (RadialGradientPaint) p1;
            RadialGradientPaint rgp2 = (RadialGradientPaint) p2;
            return rgp1.getCenterPoint().equals(rgp2.getCenterPoint())
                    && rgp1.getRadius() == rgp2.getRadius() 
                    && rgp1.getFocusPoint().equals(rgp2.getFocusPoint())
                    && Arrays.equals(rgp1.getFractions(), rgp2.getFractions())
                    && Arrays.equals(rgp1.getColors(), rgp2.getColors())
                    && rgp1.getCycleMethod() == rgp2.getCycleMethod();
        } else {
            return p1.equals(p2);
        }
    }

    /**
     * Returns {@code true} if the maps contain the same keys and 
     * {@link Paint} values, and {@code false} otherwise.
     * 
     * @param m1  map 1 ({@code null} not permitted).
     * @param m2  map 2 ({@code null} not permitted).
     * 
     * @return A boolean. 
     */
    public static boolean equalMaps(Map<?, Paint> m1, Map<?, Paint> m2) {
        if (m1.size() != m2.size()) {
            return false;
        }
        for (Map.Entry<?, Paint> entry : m1.entrySet()) {
            if (!m2.containsKey(entry.getKey())) {
                return false;
            }
            if (!PaintUtils.equal(entry.getValue(), 
                    m2.get(entry.getKey()))) {
                return false;
            }
        }
        return true;
    }

}

@@ -2481,34 +2481,178 @@
 ntPaint%7D
- as a special case
+, %7B@code LinearGradientPaint%7D and %0D%0A     * %7B@code RadialGradientPaint%7D as a special cases, since those classes do%0D%0A     * not override the %7B@code equals()%7D method
 .%0D%0A     
@@ -2796,32 +2796,32 @@
 lean.%0D%0A     */%0D%0A
-
     public stati
@@ -2855,24 +2855,100 @@
 aint p2) %7B%0D%0A
+        if (p1 == p2) %7B%0D%0A            return true;%0D%0A        %7D%0D%0A            %0D%0A
         // h
@@ -4367,32 +4367,173 @@
 getCycleMethod()
+%0D%0A                    && lgp1.getColorSpace() == lgp2.getColorSpace()%0D%0A                    && lgp1.getTransform().equals(lgp2.getTransform())
 ;%0D%0A        %7D els
@@ -5194,16 +5194,157 @@
 Method()
+%0D%0A                    && rgp1.getColorSpace() == rgp2.getColorSpace()%0D%0A                    && rgp1.getTransform().equals(rgp2.getTransform())
 ;%0D%0A     

Fix failing tests.