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

/**
 * <p>
 * A publicly cloneable object.
 * </p><p>
 * The clone() method on java.lang.Object is protected, while
 * the clone() method on QMCloneable is public.
 * </p><p>
 * Additional static support methods are provided.
 * </p><p>
 * One of the assumptions made here is that objects which extend
 * QMCloneable have clones which also extend QMCloneable.
 * </p>
 */
public class QMCloneable 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 QMCloneable, 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 QMCloneable objects.
	 * A convenience method that clones an ArrayList and its contents.
	 * </p><p>
	 * It is assumed that all the objects in the list extend QMCloneable.
	 * </p>
	 */
	public static ArrayList clone(ArrayList l)
	{
		int i,s;
		s=l.size();
		ArrayList rv=new ArrayList(s);
		for (i=0;i<s;++i)
		{
			QMCloneable o=(QMCloneable)l.get(i);
			QMCloneable c=clone(o); // The object may be null.
			rv.add(c);
		}
		return rv;
	}
	/**
	 * Clone a QMCloneable object, but first check for null!
	 */
	public static QMCloneable clone(QMCloneable c)
	{
		if (c==null)
			return null;
		return (QMCloneable)c.clone();
	}
}
