001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
006 * 
007 * http://www.object-refinery.com/orsoncharts/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 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package com.orsoncharts.util;
034
035import java.awt.GradientPaint;
036import java.awt.LinearGradientPaint;
037import java.awt.Paint;
038import java.awt.RadialGradientPaint;
039import java.util.Arrays;
040
041/**
042 * Some general utility methods for working with objects.
043 */
044public class ObjectUtils {
045    
046    /**
047     * Returns {@code true} if the objects are equal or both {@code null}. 
048     * 
049     * @param obj1  object 1 ({@code null} permitted).
050     * @param obj2  object 2 ({@code null} permitted).
051     * 
052     * @return A boolean. 
053     */
054    public static boolean equals(Object obj1, Object obj2) {
055        if (obj1 != null) {
056            return obj1.equals(obj2);
057        } else {
058            return obj2 == null;
059        }
060    }
061    
062    /**
063     * Returns the hash code of the object, or 0 if the object is 
064     * {@code null}.  This method is provided in the Objects class in 
065     * Java 1.7, but you can use this one to run on Java 1.6.
066     * 
067     * @param obj ({@code null} permitted).
068     * 
069     * @return A hash code.
070     * 
071     * @since 1.1
072     */
073    public static int hashCode(Object obj) {
074        return obj != null ? obj.hashCode() : 0;
075    }
076    
077    /**
078     * Returns {@code true} if the two {@code Paint} objects are equal 
079     * OR both {@code null}.  This method handles
080     * {@code GradientPaint}, {@code LinearGradientPaint} and 
081     * {@code RadialGradientPaint} as a special cases, since those classes do
082     * not override the {@code equals()} method.
083     *
084     * @param p1  paint 1 ({@code null} permitted).
085     * @param p2  paint 2 ({@code null} permitted).
086     *
087     * @return A boolean.
088     */
089    public static boolean equalsPaint(Paint p1, Paint p2) {
090        if (p1 == p2) {
091            return true;
092        }
093            
094        // handle cases where either or both arguments are null
095        if (p1 == null) {
096            return (p2 == null);   
097        }
098        if (p2 == null) {
099            return false;   
100        }
101
102        // handle GradientPaint as a special case...
103        if (p1 instanceof GradientPaint && p2 instanceof GradientPaint) {
104            GradientPaint gp1 = (GradientPaint) p1;
105            GradientPaint gp2 = (GradientPaint) p2;
106            return gp1.getColor1().equals(gp2.getColor1()) 
107                    && gp1.getColor2().equals(gp2.getColor2())
108                    && gp1.getPoint1().equals(gp2.getPoint1())    
109                    && gp1.getPoint2().equals(gp2.getPoint2())
110                    && gp1.isCyclic() == gp2.isCyclic()
111                    && gp1.getTransparency() == gp1.getTransparency(); 
112        } else if (p1 instanceof LinearGradientPaint 
113                && p2 instanceof LinearGradientPaint) {
114            LinearGradientPaint lgp1 = (LinearGradientPaint) p1;
115            LinearGradientPaint lgp2 = (LinearGradientPaint) p2;
116            return lgp1.getStartPoint().equals(lgp2.getStartPoint())
117                    && lgp1.getEndPoint().equals(lgp2.getEndPoint()) 
118                    && Arrays.equals(lgp1.getFractions(), lgp2.getFractions())
119                    && Arrays.equals(lgp1.getColors(), lgp2.getColors())
120                    && lgp1.getCycleMethod() == lgp2.getCycleMethod()
121                    && lgp1.getColorSpace() == lgp2.getColorSpace()
122                    && lgp1.getTransform().equals(lgp2.getTransform());
123        } else if (p1 instanceof RadialGradientPaint 
124                && p2 instanceof RadialGradientPaint) {
125            RadialGradientPaint rgp1 = (RadialGradientPaint) p1;
126            RadialGradientPaint rgp2 = (RadialGradientPaint) p2;
127            return rgp1.getCenterPoint().equals(rgp2.getCenterPoint())
128                    && rgp1.getRadius() == rgp2.getRadius() 
129                    && rgp1.getFocusPoint().equals(rgp2.getFocusPoint())
130                    && Arrays.equals(rgp1.getFractions(), rgp2.getFractions())
131                    && Arrays.equals(rgp1.getColors(), rgp2.getColors())
132                    && rgp1.getCycleMethod() == rgp2.getCycleMethod()
133                    && rgp1.getColorSpace() == rgp2.getColorSpace()
134                    && rgp1.getTransform().equals(rgp2.getTransform());
135        } else {
136            return p1.equals(p2);
137        }
138    }
139
140}