/*******************************************************************************
* 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 03-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.api.programs.returnValues;

import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;


/**
 * @author Daniel Burke
 *
 */
public final class ReturnMapping implements IReturnMapping {
	
	private int value;
	private boolean isGood;
	private String message;
	
	/**
	 * @param value
	 * @param isGood
	 * @param message
	 */
	
	public ReturnMapping(int value, boolean isGood, String message) {
	    
	    if(value == 256)
	    {
	        throw new IllegalArgumentException("This return value is reserved : " + value);
	    }
	    
	    if(value == 0 && !isGood)
	    {
	        throw new IllegalArgumentException("0 must always be a good return value");
	    }
	    
	    if(value == 1 && isGood)
	    {
	        throw new IllegalArgumentException("1 must always be a bad return value");
	    }
	    
		this.value = value;
		this.isGood = isGood;
		this.message = message;		
	}
	
	public boolean isGood() {
		return isGood;
	}
	
	public String getMessage() {
		return message;
	}
	
	public int getValue() {
		return value;
	}
	
	public String toString()
	{
	    return toString(0);
	}
	
	public String toString(int depth)
	{
	    StringBuilder text = new StringBuilder();
	    depth = XMLUtil.putOpenElem(text, "return-mapping", depth);
	    
	    XMLUtil.putElement(text, "value", Integer.toString(value), depth);
	    XMLUtil.putElement(text, "message", message, depth);
	    
	    if(isGood)
	    {
	        XMLUtil.putElement(text, "is-good", null, depth);
	    }
	    
	    depth = XMLUtil.putCloseElem(text, "return-mapping", depth);
	    return text.toString(); 
	}
}
