/*
Copyright 2002-2006 The Members of the OSS through Java(TM) Initiative.
All rights reserved. Use is subject to license terms.
 */
package ossj.common.cbe;

import java.lang.reflect.Method;
import javax.oss.cbe.CharacteristicValue;
import ossj.common.Utils;


/**
 * An implementation class for the <CODE>javax.oss.cbe.CharacteristicValue</CODE> interface.
 *
 * @see javax.oss.cbe.CharacteristicValue
 *
 * @author OSS through Java(tm) Initiative, Vincent Perrot Sun Microsystems Inc.
 * @version 1.3
 * @since January 2006
 */


public class CharacteristicValueImpl
        implements CharacteristicValue {
    
    /**
     * Constructs a new CharacteristicValueImpl with empty attributes.
     *
     */
    
    public CharacteristicValueImpl() {
    }
    /**
     * Constructs a new CharacteristicValueImpl using given attributes.
     * @param name characteristic name
     * @param value the content of the attribute
     *
     */
    
    public CharacteristicValueImpl(String name, Object value) {
        _characteristicvalueimpl_characteristic = name;
        _characteristicvalueimpl_value = value;
        
    }
    
    private java.lang.String _characteristicvalueimpl_characteristic = "";
    private java.lang.Object _characteristicvalueimpl_value = null;
    
    
    /**
     * Changes the characteristic to be equal to the given argument.
     *
     * This method support a null argument.
     *
     * @param value the new characteristic for this object.
     * @exception java.lang.IllegalArgumentException - Is thrown to report
     * that a bad argument was provided to the method.
     */
    
    public void setCharacteristic(java.lang.String value)
    throws java.lang.IllegalArgumentException    {
        if (value == null)
            throw new java.lang.IllegalArgumentException("null value is not supported.");
            _characteristicvalueimpl_characteristic = value;
    }
    
    
    /**
     * Returns this CharacteristicValueImpl's characteristic
     *
     * @return the characteristic
     *
     */
    
    public java.lang.String getCharacteristic() {
        return _characteristicvalueimpl_characteristic;
    }
    
    /**
     * Changes the value to be equal to the given argument.
     *
     * This method support a null argument.
     * null means that this characteristic is not populated.
     *
     * @param value the new value for this object.
     * @exception java.lang.IllegalArgumentException - Is thrown to report
     * that a bad argument was provided to the method.
     */
    
    public void setValue(java.lang.Object value)
    throws java.lang.IllegalArgumentException    {
        _characteristicvalueimpl_value = value;
    }
    
    
    /**
     * Returns this CharacteristicValueImpl's value
     *
     * @return the value
     *
     */
    
    public java.lang.Object getValue() {
        return _characteristicvalueimpl_value;
    }
    
    /**
     * Creates a new object of the same class and with the same contents as this object.
     * @return a clone of this instance.
     */
    public Object clone() {
        CharacteristicValue val = null;
        try {
            val = (CharacteristicValue)super.clone();
            
            if( this.getValue()!=null)
                val.setValue((java.lang.Object)clone(getValue()));
            else
                val.setValue( null );
            
            return val;
        } catch( CloneNotSupportedException ex) {
            throw new RuntimeException("CharacteristicValueImpl: Unable to clone this object: "+ ex.getMessage());
        }
    }
    
    private Object clone(Object obj) {
        Object clone = obj;
        if ((obj != null) && obj instanceof Cloneable) {
            try {
                Method method = obj.getClass().getMethod("clone",(Class[]) null);
                clone = method.invoke(obj, (Object[])null);
            } catch (Exception e) {
                return null;
            }
        }
        return clone;
    }
    
    /**
     * Checks whether two CharacteristicValue are equal.
     * The result is true if and only if the argument is not null
     * and is an CharacteristicValue object that has the arguments.
     *
     * @param value the Object to compare with this CharacteristicValue
     * @return if the objects are equal; false otherwise.
     */
    
    public boolean equals(Object value) {
        if ( this == value ){ return true;}
        if ( (value != null) && ( value instanceof CharacteristicValue)) {
            CharacteristicValue argVal = (CharacteristicValue) value;
            if( !Utils.compareAttributes( getCharacteristic(), argVal.getCharacteristic())) { return false; }
            if( !Utils.compareAttributes( getValue(), argVal.getValue())) { return false; }
            
            return true;
        } else {
            return super.equals(value);
        }
    }
    
}
