/*******************************************************************************
* 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 19-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.api.programs.arguments.internal.iomapping;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IBasicArgumentCondition;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBasicArgumentValue;
import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;

/**
 * @author Daniel Burke
 *
 */
public abstract class AbstractArgumentCondition implements IBasicArgumentCondition, Comparable<AbstractArgumentCondition>  {
     
	
	private String targetArgument;

    public AbstractArgumentCondition(String targetArgument)
    {
    	if(targetArgument == null)
    	{
    		throw new IllegalArgumentException("target may not be null");
    	}
    	
        this.targetArgument = targetArgument;
    }
    
	public int compareTo(AbstractArgumentCondition o) {
		if(o == null)
		{
			throw new IllegalArgumentException("null param");
		}
		return targetArgument.compareTo(o.getTargetArgument());
	}

	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof AbstractArgumentCondition))
		{
			return false;
		}
		
		return targetArgument.equals(
				((AbstractArgumentCondition)obj).getTargetArgument());
	}
	
    public String getTargetArgument() {
        return targetArgument;
    }
    
    public abstract boolean isMatch(IBasicArgumentValue value);
    
    public abstract String toString();
    
    public abstract String toString(int depth);
    
    public String toString(int depth, String specialisedBody)
    {
		StringBuilder text = new StringBuilder();
		
		depth = XMLUtil.putOpenElem(text, "condition", depth);

		XMLUtil.putElement(text, "target", targetArgument, depth);

		text.append(specialisedBody);
		
		depth = XMLUtil.putCloseElem(text, "condition", depth);
		
		return text.toString();
    }
}
