// (c) MIT 2003.  All rights reserved.

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	CloneableArray.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	April 99
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Wrapper for cloneable arrays
//
// HISTORY:     April 99  archon  created
//
////////////////////////////////////////////////////////////////////////////////

package rec.meche;

import java.lang.reflect.*;
import rec.util.*;

public final class CloneableArray implements Cloneable
{
  public CloneableArray()
  {
    m_Cloneables = null;
  }

  public CloneableArray( Cloneable[] cloneables )
  {
    m_Cloneables = cloneables;
  }

  //////////////////////////////////////////////////////////////////////////////

  public CloneableArray( Cloneable c1, Cloneable c2 )
  {
    m_Cloneables = new Cloneable[2];

    m_Cloneables[0] = c1;
    m_Cloneables[1] = c2;
  }

  //////////////////////////////////////////////////////////////////////////////

  public Object clone()
  {
    if( m_Cloneables == null )
      return( new CloneableArray() );

    Cloneable[] newCloneables = new Cloneable[m_Cloneables.length];

    for( int i = 0; i < m_Cloneables.length; ++i ) {

      Cloneable cl = m_Cloneables[i];

      if( cl == null ) {
	newCloneables[i] = null;
      }
      else {

	Class c = cl.getClass();

	Method m = null;

	try {
	  m = c.getMethod( "clone", null );
	  newCloneables[i] = (Cloneable)m.invoke( m_Cloneables[i], null );
	}
	catch( Exception e ) {
	  Assert.fatal( "data was not cloneable" );
	}
      }
    }

    return( new CloneableArray( newCloneables ) );
  }

  //////////////////////////////////////////////////////////////////////////////

  public Cloneable[] getArray()
  {
    return( m_Cloneables );
  }

  //////////////////////////////////////////////////////////////////////////////

  private Cloneable[] m_Cloneables;
}

////////////////////////////////////////////////////////////////////////////////
