/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 18-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.api.programs.arguments.internal.argumentInfo;

import java.util.ArrayList;
import java.util.Collection;

import uk.ac.ic.doc.kevlar.api.programs.arguments.ISetValue;
import uk.ac.ic.doc.kevlar.publicUtil.collections.IImmutableSet;
import uk.ac.ic.doc.kevlar.publicUtil.collections.ImmutableHashSet;
import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;

/**
 * @author Daniel Burke
 *
 */
public final class SetValue implements ISetValue, Comparable<SetValue> {
    private String name;
    private ImmutableHashSet<String> children;
    
    public SetValue(String name, Collection<String> children)
    {
    	if(name == null)
    	{
    		throw new IllegalArgumentException("null params");
    	}
    	
    	if(children == null)
    	{
    		children = new ArrayList<String>();
    	}
    	
        this.name = name;
        this.children = new ImmutableHashSet<String>(children);
    }
    
	public int compareTo(SetValue o) {
		if(o == null)
		{
			throw new IllegalArgumentException("null param");
		}
		return name.compareTo(o.getName());
	}
	
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof SetValue))
		{
			return false;
		}
		
		return name.equals(((SetValue)obj).getName());
	}
	
	public int hashCode() {
		return name.hashCode();
	}
    
    /**
     * @return Returns the children.
     */
    public IImmutableSet<String> getChildren() {
        return children;
    }
    
    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }
    
	public String toString()
	{
		return toString(0);
	}
	
	public String toString(int depth)
	{
		StringBuilder text = new StringBuilder();
		
		depth = XMLUtil.putOpenElem(text, "set-value", depth);

		XMLUtil.putElement(text, "name", name, depth);
		if(children.size() > 0)
		{
			depth = XMLUtil.putOpenElem(text, "children", depth);
			
			for(String child : children)
			{
				XMLUtil.putElement(text, "child", child, depth);
			}
			
			depth = XMLUtil.putCloseElem(text, "children", depth);
		}
		
		depth = XMLUtil.putCloseElem(text, "set-value", depth);

		return text.toString();
	}
}
