/***************************************************************************
 *   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 joliex.gwt.client;

import com.google.gwt.user.client.rpc.IsSerializable;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Value implements Serializable, IsSerializable
{
	public enum Type implements IsSerializable {
		UNDEFINED, STRING, INT, DOUBLE
	}

	private Map< String, ValueVector > children = new HashMap< String, ValueVector >();

	private String valueObject = null;
	private Type type = Type.UNDEFINED;
	
	public Value()
	{}
	
	public Value( String value )
	{
		setValue( value );
	}
	
	public Value( Integer value )
	{
		setValue( value );
	}
	
	public Value( Double value )
	{
		setValue( value );
	}
	
	public boolean isString()
	{
		return type == Type.STRING;
	}
	
	public boolean isInt()
	{
		return type == Type.INT;
	}
	
	public boolean isDouble()
	{
		return type == Type.DOUBLE;
	}
	
	public boolean isDefined()
	{
		return type != Type.UNDEFINED;
	}
	
	public ValueVector getChildren( String id )
	{
		ValueVector v = children.get( id );
		if ( v == null ) {
			v = new ValueVector();
			children.put( id, v );
		}
	
		return v;	
	}
	
	public boolean hasChildren()
	{
		return !children.isEmpty();
	}
	
	public boolean hasChildren( String id )
	{
		return children.get( id ) != null;
	}
	
	public int intValue()
	{
		if ( valueObject == null )
			return 0;
		return new Integer( valueObject );
	}
	
	public double doubleValue()
	{
		if ( valueObject == null )
			return 0;
		return new Double( valueObject );
	}
	
	public String strValue()
	{
		if ( valueObject == null )
			return new String();
		return valueObject.toString();
	}
	
	public Value getNewChild( String childId )
	{
		ValueVector vec = getChildren( childId );
		Value retVal = new Value();
		vec.add( retVal );
		
		return retVal;
	}
	
	public Map< String, ValueVector > children()
	{
		return children;
	}
	
	public Value getFirstChild( String id )
	{
		return getChildren( id ).first();
	}
	
	public void setValue( String obj )
	{
		valueObject = obj;
		type = Type.STRING;
	}

	public void setValue( Integer obj )
	{
		valueObject = obj.toString();
		type = Type.INT;
	}
	
	public void setValue( Double obj )
	{
		valueObject = obj.toString();
		type = Type.DOUBLE;
	}
}

@@ -1694,16 +1694,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0Apublic
@@ -2923,32 +2923,444 @@
 alueObject );%0A%09%7D
+%0A%0A%09public void deepCopy( Value otherValue )%0A%09%7B%0A%09%09valueObject = otherValue.valueObject;%0A%09%09ValueVector myVector;%0A%09%09Value myValue;%0A%09%09for( Entry%3C String, ValueVector %3E entry : otherValue.children.entrySet() ) %7B%0A%09%09%09myVector = new ValueVector();%0A%09%09%09for( Value v : entry.getValue() ) %7B%0A%09%09%09%09myValue = new Value();%0A%09%09%09%09myValue.deepCopy( v );%0A%09%09%09%09myVector.add( v );%0A%09%09%09%7D%0A%09%09%09children.put( entry.getKey(), myVector );%0A%09%09%7D%0A%09%7D
 %0A%09%0A%09public doubl

add Value.deepCopy