001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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 * CloneUtils.java
029 * ---------------
030 * (C) Copyright 2014-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036package org.jfree.chart.util;
037
038import java.lang.reflect.InvocationTargetException;
039import java.lang.reflect.Method;
040import java.lang.reflect.Modifier;
041import java.util.ArrayList;
042import java.util.HashMap;
043import java.util.List;
044import java.util.Map;
045
046/**
047 * Utilities for cloning.
048 */
049public class CloneUtils {
050
051    private CloneUtils() {
052        // no requirement to instantiate
053    }
054
055    /**
056     * Returns a clone of the specified object, if it can be cloned, otherwise
057     * throws a {@code CloneNotSupportedException}.
058     *
059     * @param object the object to clone ({@code null} not permitted).
060     * @return A clone of the specified object.
061     * @throws CloneNotSupportedException if the object cannot be cloned.
062     */
063    public static Object clone(Object object)
064        throws CloneNotSupportedException {
065        if (object == null) {
066            throw new IllegalArgumentException("Null 'object' argument.");
067        }
068        if (object instanceof PublicCloneable) {
069            PublicCloneable pc = (PublicCloneable) object;
070            return pc.clone();
071        }
072        else {
073            try {
074                Method method = object.getClass().getMethod("clone",
075                        (Class[]) null);
076                if (Modifier.isPublic(method.getModifiers())) {
077                    return method.invoke(object, (Object[]) null);
078                }
079            }
080            catch (NoSuchMethodException e) {
081                throw new CloneNotSupportedException("Object without clone() method is impossible.");
082            }
083            catch (IllegalAccessException e) {
084                throw new CloneNotSupportedException("Object.clone(): unable to call method.");
085            }
086            catch (InvocationTargetException e) {
087                throw new CloneNotSupportedException("Object without clone() method is impossible.");
088            }
089        }
090        throw new CloneNotSupportedException("Failed to clone.");
091    }
092
093    /**
094     * Returns a list containing cloned copies of the items in the source
095     * list.
096     * 
097     * @param source  the source list ({@code null} not permitted).
098     * 
099     * @return A new list. 
100     */
101    public static List<?> cloneList(List<?> source) {
102        Args.nullNotPermitted(source, "source");
103        List result = new ArrayList();
104        for (Object obj: source) {
105            if (obj == null) {
106                result.add(null);
107            } else if (obj.getClass() == String.class) {
108                result.add(obj);
109            } else {
110                try {
111                    result.add(ObjectUtils.clone(obj));
112                } catch (CloneNotSupportedException ex) {
113                    throw new RuntimeException(ex);
114                }
115            }
116        }
117        return result;
118    }
119    
120    /**
121     * Returns a new map that contains the same keys and cloned copied of the
122     * values.
123     * 
124     * @param source  the source map ({@code null} not permitted).
125     * 
126     * @return A new map. 
127     */
128    public static Map cloneMapValues(Map source) {
129        Args.nullNotPermitted(source, "source");
130        Map result = new HashMap();
131        for (Object key : source.keySet()) {
132            Object value = source.get(key);
133            if (value != null) {
134                try {
135                    result.put(key, ObjectUtils.clone(value));
136                } catch (CloneNotSupportedException ex) {
137                    throw new RuntimeException(ex);
138                }
139            } else {
140                result.put(key, null);
141            }
142        }
143        return result;
144    }
145   
146}