/*
* TeleStax, Open Source Cloud Communications Copyright 2012.
* and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.mobicents.protocols.ss7.sccp.impl.parameter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.mobicents.protocols.ss7.sccp.SccpProtocolVersion;
import org.mobicents.protocols.ss7.sccp.message.ParseException;
import org.mobicents.protocols.ss7.sccp.parameter.ParameterFactory;
import org.mobicents.protocols.ss7.sccp.parameter.ReturnCause;
import org.mobicents.protocols.ss7.sccp.parameter.ReturnCauseValue;
/**
*
* @author baranowb
* @author sergey vetyutnev
*/
public class ReturnCauseImpl extends AbstractParameter implements ReturnCause {
private ReturnCauseValue value;
private int digValue;
public ReturnCauseImpl() {
value = ReturnCauseValue.UNQALIFIED;
}
public ReturnCauseImpl(ReturnCauseValue value) {
this.value = value;
if (value != null)
this.digValue = value.getValue();
}
public ReturnCauseImpl(int digValue) {
this.digValue = digValue;
value = ReturnCauseValue.getInstance(digValue);
}
public ReturnCauseValue getValue() {
return value;
}
public int getDigitalValue() {
return digValue;
}
@Override
public void decode(final InputStream in, final ParameterFactory factory, final SccpProtocolVersion sccpProtocolVersion) throws ParseException {
try {
if (in.read() != 1) {
throw new ParseException();
}
this.digValue = in.read() & 0xFF;
this.value = ReturnCauseValue.getInstance(this.digValue);
} catch (IOException e) {
throw new ParseException(e);
}
}
@Override
public void encode(OutputStream out, final boolean removeSpc, final SccpProtocolVersion sccpProtocolVersion) throws ParseException {
try {
out.write(1);
out.write(this.digValue);
} catch (IOException e) {
throw new ParseException(e);
}
}
@Override
public void decode(byte[] bb, final ParameterFactory factory, final SccpProtocolVersion sccpProtocolVersion) throws ParseException {
this.digValue = bb[0] & 0xff;
this.value = ReturnCauseValue.getInstance(this.digValue);
}
@Override
public byte[] encode(final boolean removeSpc, final SccpProtocolVersion sccpProtocolVersion) throws ParseException {
return new byte[] { (byte) this.digValue };
}
public String toString() {
if (this.value != null)
return this.value.toString();
else {
return ((Integer) this.digValue).toString();
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + digValue;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReturnCauseImpl other = (ReturnCauseImpl) obj;
if (digValue != other.digValue)
return false;
return true;
}
}