package com.jxml.quick.model.qdml;
import java.util.*;

/**
 * <p>
 * A publicly cloneable object.
 * </p><p>
 * The clone() method on java.lang.Object is protected, while
 * the clone() method on QDMCloneable is public.
 * </p><p>
 * Additional static support methods are provided.
 * </p><p>
 * One of the assumptions made here is that objects which extend
 * QDMCloneable have clones which also extend QDMCloneable.
 * </p>
 */
public class QDMCloneable implements Cloneable
{
	/**
	 * <p>
	 * Returns a copy of this object.
	 * This is a public method which overrides the protected method on java.lang.Object.
	 * </p><p>
	 * It is expected that this method will be overridden 
	 * by classes which extend QDMCloneable, the result being a deep clone.
	 * </p>
	 */
	public Object clone()
	{
		try
		{
			return super.clone(); // Performs a shallow copy of the contents of this object.
		}
		catch (CloneNotSupportedException e)
		{
			throw new Error(e.toString()); // This code should be unreachable ;-)
		}
	}

	/**
	 * <p>
	 * Deep clone a list of QDMCloneable objects.
	 * A convenience method that clones an ArrayList and its contents.
	 * </p><p>
	 * It is assumed that all the objects in the list extend QDMCloneable.
	 * </p>
	 */
	public static ArrayList clone(ArrayList l)
	{
		int i,s;
		s=l.size();
		ArrayList rv=new ArrayList(s);
		for (i=0;i<s;++i)
		{
			QDMCloneable o=(QDMCloneable)l.get(i);
			QDMCloneable c=clone(o); // The object may be null.
			rv.add(c);
		}
		return rv;
	}
	/**
	 * Clone a QDMCloneable object, but first check for null!
	 */
	public static QDMCloneable clone(QDMCloneable c)
	{
		if (c==null)
			return null;
		return (QDMCloneable)c.clone();
	}
}
