001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * CloneUtils.java
029 * ---------------
030 * (C) Copyright 2014, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 08-Apr-2014 : Version 1 (DG);
038 *
039 */
040package org.jfree.chart.util;
041
042import java.util.ArrayList;
043import java.util.HashMap;
044import java.util.List;
045import java.util.Map;
046
047/**
048 * Utilities for cloning.
049 * 
050 * @since 1.0.18
051 */
052public class CloneUtils {
053    
054    /**
055     * Returns a list containing cloned copies of the items in the source
056     * list.
057     * 
058     * @param source  the source list ({@code null} not permitted).
059     * 
060     * @return A new list. 
061     */
062    public static List<?> cloneList(List<?> source) {
063        Args.nullNotPermitted(source, "source");
064        List result = new ArrayList();
065        for (Object obj: source) {
066            if (obj != null) {
067                try {
068                    result.add(ObjectUtils.clone(obj));
069                } catch (CloneNotSupportedException ex) {
070                    throw new RuntimeException(ex);
071                }
072            } else {
073                result.add(null);
074            }
075        }
076        return result;
077    }
078    
079    /**
080     * Returns a new map that contains the same keys and cloned copied of the
081     * values.
082     * 
083     * @param source  the source map ({@code null} not permitted).
084     * 
085     * @return A new map. 
086     * 
087     * @since 1.0.18
088     */
089    public static Map cloneMapValues(Map source) {
090        Args.nullNotPermitted(source, "source");
091        Map result = new HashMap();
092        for (Object key : source.keySet()) {
093            Object value = source.get(key);
094            if (value != null) {
095                try {
096                    result.put(key, ObjectUtils.clone(value));
097                } catch (CloneNotSupportedException ex) {
098                    throw new RuntimeException(ex);
099                }
100            } else {
101                result.put(key, null);
102            }
103        }
104        return result;
105    }
106   
107}