001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2017, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.util;
030
031import java.lang.reflect.InvocationTargetException;
032import java.lang.reflect.Method;
033import java.lang.reflect.Modifier;
034import java.util.Collection;
035import java.util.Iterator;
036
037/**
038 * A collection of useful static utility methods for handling classes and object
039 * instantiation.
040 */
041public final class ObjectUtils {
042
043    /**
044     * Default constructor - private.
045     */
046    private ObjectUtils() {
047    }
048
049    /**
050     * Returns {@code true} if the two objects are equal OR both
051     * {@code null}.
052     *
053     * @param o1 object 1 ({@code null} permitted).
054     * @param o2 object 2 ({@code null} permitted).
055     * @return {@code true} or {@code false}.
056     */
057    public static boolean equal(Object o1, Object o2) {
058        if (o1 == o2) {
059            return true;
060        }
061        if (o1 != null) {
062            return o1.equals(o2);
063        }
064        else {
065            return false;
066        }
067    }
068
069    /**
070     * Returns a hash code for an object, or zero if the object is
071     * {@code null}.
072     *
073     * @param object the object ({@code null} permitted).
074     * @return The object's hash code (or zero if the object is
075     *         {@code null}).
076     */
077    public static int hashCode(Object object) {
078        int result = 0;
079        if (object != null) {
080            result = object.hashCode();
081        }
082        return result;
083    }
084
085    /**
086     * Returns a clone of the specified object, if it can be cloned, otherwise
087     * throws a CloneNotSupportedException.
088     *
089     * @param object the object to clone ({@code null} not permitted).
090     * @return A clone of the specified object.
091     * @throws CloneNotSupportedException if the object cannot be cloned.
092     */
093    public static Object clone(Object object)
094        throws CloneNotSupportedException {
095        if (object == null) {
096            throw new IllegalArgumentException("Null 'object' argument.");
097        }
098        if (object instanceof PublicCloneable) {
099            PublicCloneable pc = (PublicCloneable) object;
100            return pc.clone();
101        }
102        else {
103            try {
104                Method method = object.getClass().getMethod("clone",
105                        (Class[]) null);
106                if (Modifier.isPublic(method.getModifiers())) {
107                    return method.invoke(object, (Object[]) null);
108                }
109            }
110            catch (NoSuchMethodException e) {
111                throw new CloneNotSupportedException("Object without clone() method is impossible.");
112            }
113            catch (IllegalAccessException e) {
114                throw new CloneNotSupportedException("Object.clone(): unable to call method.");
115            }
116            catch (InvocationTargetException e) {
117                throw new CloneNotSupportedException("Object without clone() method is impossible.");
118            }
119        }
120        throw new CloneNotSupportedException("Failed to clone.");
121    }
122
123    /**
124     * Returns a new collection containing clones of all the items in the
125     * specified collection.
126     *
127     * @param collection the collection ({@code null} not permitted).
128     * @return A new collection containing clones of all the items in the
129     *         specified collection.
130     * @throws CloneNotSupportedException if any of the items in the collection
131     *                                    cannot be cloned.
132     */
133    public static Collection deepClone(Collection collection)
134            throws CloneNotSupportedException {
135
136        if (collection == null) {
137            throw new IllegalArgumentException("Null 'collection' argument.");
138        }
139        // all JDK-Collections are cloneable ...
140        // and if the collection is not clonable, then we should throw
141        // a CloneNotSupportedException anyway ...
142        Collection result = (Collection) ObjectUtils.clone(collection);
143        result.clear();
144        Iterator iterator = collection.iterator();
145        while (iterator.hasNext()) {
146            Object item = iterator.next();
147            if (item != null) {
148                result.add(clone(item));
149            }
150            else {
151                result.add(null);
152            }
153        }
154        return result;
155    }
156
157}