    001/* ===================================================
    002 * JFreeSVG : an SVG library for the Java(tm) platform
    003 * ===================================================
    004 *
    005 * (C)opyright 2013, 2014, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * Project Info:  http://www.jfree.org/jfreesvg/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * JFreeSVG home page:
    028 *
    029 * http://www.jfree.org/jfreesvg
    030 *
    031 */
    032
    033package org.jfree.graphics2d;
    034
    035import java.awt.GradientPaint;
    036
    037/**
    038 * A wrapper for a <code>GradientPaint</code> that can be used as the key for
    039 * a <code>Map</code> (including a <code>HashMap</code>).  This class is used
    040 * internally by <code>SVGGraphics2D</code> to track and re-use gradient
    041 * definitions.  <code>GradientPaint</code> itself does not implement the
    042 * equals() and hashCode() methods, so it doesn't make a good key for a
    043 * <code>Map</code>.
    044 */
    045public final class GradientPaintKey {
    046
    047    private final GradientPaint paint;
    048
    049    /**
    050     * Creates a new instance based on the specified <code>paint</code>.
    051     *
    052     * @param paint  the paint (<code>null</code> not permitted).
    053     */
    054    public GradientPaintKey(GradientPaint paint) {
    055        Args.nullNotPermitted(paint, "paint");
    056        this.paint = paint;
    057    }
    058
    059    /**
    060     * Returns the <code>GradientPaint</code> that was supplied to the
    061     * constructor.
    062     *
    063     * @return The <code>GradientPaint</code> (never <code>null</code>).
    064     */
    065    public GradientPaint getPaint() {
    066        return this.paint;
    067    }
    068
    069    /**
    070     * Checks this instance for equality with an arbitrary object.
    071     *
    072     * @param obj  the object to test against (<code>null</code> permitted).
    073     *
    074     * @return A boolean.
    075     */
    076    @Override
    077    public boolean equals(Object obj) {
    078        if (obj == this) {
    079            return true;
    080        }
    081        if (!(obj instanceof GradientPaintKey)) {
    082            return false;
    083        }
    084        GradientPaintKey that = (GradientPaintKey) obj;
    085        GradientPaint thisGP = this.paint;
    086        GradientPaint thatGP = that.getPaint();
    087        if (!thisGP.getColor1().equals(thatGP.getColor1())) {
    088            return false;
    089        }
    090        if (!thisGP.getColor2().equals(thatGP.getColor2())) {
    091            return false;
    092        }
    093        if (!thisGP.getPoint1().equals(thatGP.getPoint1())) {
    094            return false;
    095        }
    096        if (!thisGP.getPoint2().equals(thatGP.getPoint2())) {
    097            return false;
    098        }
    099        if (thisGP.getTransparency() != thatGP.getTransparency()) {
    100            return false;
    101        }
    102        if (thisGP.isCyclic() != thatGP.isCyclic()) {
    103            return false;
    104        }
    105        return true;
    106    }
    107
    108    /**
    109     * Returns a hash code for this instance.
    110     *
    111     * @return A hash code.
    112     */
    113    @Override
    114    public int hashCode() {
    115        int hash = 5;
    116        hash = 47 * hash + this.paint.getPoint1().hashCode();
    117        hash = 47 * hash + this.paint.getPoint2().hashCode();
    118        hash = 47 * hash + this.paint.getColor1().hashCode();
    119        hash = 47 * hash + this.paint.getColor2().hashCode();
    120        return hash;
    121    }
    122
    123}