/**
 * @author	STF 424_ITS_Test_Platform
 * @version    $URL$
 *             $Id$
 */
package org.etsi.codec;

import org.etsi.ttcn.tri.TriMessage;

/**
 * This class is provides an implementation for the TriMessage
 */
@SuppressWarnings("serial")
public class TriMessageImpl implements TriMessage {

	/**
	 * The TriMessage value
	 */
	private byte[] _message;

	/** 
	 * Creates a new TriMessage object
	 *
	 * @param message The content of the message
	 */
	public TriMessageImpl(byte[] message) {
		_message = message;
	}

	/**
	 * Checks if two TriMessage are identical
	 * 
	 * @return true if messages are identical, false otherwise
	 */
	@Override
	public boolean equals(TriMessage message) {
		return java.util.Arrays.equals(_message, message.getEncodedMessage()) && getNumberOfBits() == message.getNumberOfBits();
	}

	/**
	 * This method provides the TriMessage value
	 * 
	 * @return The TriMessage value
	 */
	@Override
	public byte[] getEncodedMessage() {
		return _message;
	}

	/**
	 * This method provides the size in bits of the riMessage value
	 * 
	 * @return The size in bits of the riMessage value
	 */
	@Override
	public int getNumberOfBits() {
		return _message.length * Byte.SIZE;
	}

	/**
	 * This method sets a new TriMessage value
	 * 
	 * @param message The new TriMessage value
	 */
	@Override
	public void setEncodedMessage(byte[] message) {
		_message = message;
	}

	/**
	 * This method sets (increase or decrease) the size of the TriMessage buffer
	 * 
	 * @param amount The new size in bits of the TriMessage
	 */
	@Override
	public void setNumberOfBits(int amount) {
		int length = amount / 8;
		if (_message == null) {
			if ((amount % 8) != 0) {
				length += 1;
			}
			_message = new byte[length];
		} else if (_message.length > length) {
			throw new UnsupportedOperationException();
		} else if (_message.length < length) {
			throw new UnsupportedOperationException();
		}
	}
} //End of class TriMessageImpl
