/***************************************************************************
 *   Copyright (C) by Fabrizio Montesi                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.runtime;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class ValueVectorLink extends ValueVector implements Cloneable
{
	final private VariablePath linkPath;

	@Override
	public ValueVectorLink clone()
	{
		return new ValueVectorLink( linkPath );
	}

	public ValueVectorLink( VariablePath path )
	{
		linkPath = path;
	}

	public boolean isLink()
	{
		return true;
	}
	
	private ValueVector getLinkedValueVector()
	{
		return linkPath.getValueVector();
	}
	
	protected List< Value > values()
	{
		return getLinkedValueVector().values();
	}
}

class ValueVectorImpl extends ValueVector
{
	final private List< Value > values;
	
	protected List< Value > values()
	{
		return values;
	}
	
	public boolean isLink()
	{
		return false;
	}
	
	public ValueVectorImpl()
	{
		values = new LinkedList< Value >();
	}
}

abstract public class ValueVector implements Iterable< Value >
{
	public static ValueVector create()
	{
		return new ValueVectorImpl();
	}
	
	public synchronized Value remove( int i )
	{
		return values().remove( i );
	}
	
	public static ValueVector createLink( VariablePath path )
	{
		return new ValueVectorLink( path );
	}
	
	public static ValueVector createClone( ValueVector vec )
	{
		ValueVector retVec = null;
		
		if ( vec.isLink() ) {
			retVec = ((ValueVectorLink)vec).clone();
		} else {
			retVec = create();
			for( Value v : vec )
				retVec.add( Value.createClone( v ) );
		}
		
		return retVec;
	}
	
	public synchronized Value first()
	{
		return get( 0 );
	}
	
	public synchronized boolean isEmpty()
	{
		return values().isEmpty();
	}
	
	public Iterator< Value > iterator()
	{
		return values().iterator();
	}
	
	public synchronized Value get( int i )
	{
		final List< Value > values = values();

		if ( i >= values.size() ) {
			for( int k = values.size(); k <= i; k++ ) {
				values.add( Value.create() );
			}
		}
		return values.get( i );
	}
	
	public synchronized int size()
	{
		return values().size();
	}
	
	public synchronized void add( Value value )
	{
		values().add( value );
	}
	
	public synchronized void set( int i, Value value )
	{
		final List< Value > values = values();

		if ( i >= values.size() ) {
			for( int k = values.size(); k < i; k++ ) {
				values.add( Value.create() );
			}
			values.add( value );
		} else {
			values.set( i, value );
		}
	}
	
	// TODO: improve performance
	public synchronized void deepCopy( ValueVector vec )
	{
		for( int i = 0; i < vec.size(); i++ )
			get( i ).deepCopy( vec.get( i ) );
	}

	abstract protected List< Value > values();
	abstract public boolean isLink();
}

@@ -1559,16 +1559,45 @@
 ntime;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -2197,16 +2197,40 @@
 ueVector
+ implements Serializable
 %0A%7B%0A%09fina

Make ValueVector serializable.