PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/IMEIImpl.java

http://mobicents.googlecode.com/
Java | 169 lines | 111 code | 32 blank | 26 comment | 19 complexity | 33c2381da0f641bcafb05fadd824c074 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.map.primitives;
 24
 25import java.io.IOException;
 26
 27import org.mobicents.protocols.asn.AsnException;
 28import org.mobicents.protocols.asn.AsnInputStream;
 29import org.mobicents.protocols.asn.AsnOutputStream;
 30import org.mobicents.protocols.asn.Tag;
 31import org.mobicents.protocols.ss7.map.api.MAPException;
 32import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 33import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 34import org.mobicents.protocols.ss7.map.api.primitives.IMEI;
 35
 36/**
 37 * 
 38 * @author sergey vetyutnev
 39 * 
 40 */
 41public class IMEIImpl extends TbcdString implements IMEI {
 42
 43	private String imei;
 44
 45	public IMEIImpl() {
 46	}
 47
 48	public IMEIImpl(String imei) {
 49		this.imei = imei;
 50	}
 51
 52	@Override
 53	public String getIMEI() {
 54		return this.imei;
 55	}
 56	
 57	public int getTag() throws MAPException {
 58		return Tag.STRING_OCTET;
 59	}
 60
 61	public int getTagClass() {
 62		return Tag.CLASS_UNIVERSAL;
 63	}
 64
 65	public boolean getIsPrimitive() {
 66		return true;
 67	}
 68
 69	
 70	@Override
 71	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 72		
 73		try {
 74			int length = ansIS.readLength();
 75			this._decode(ansIS, length);
 76		} catch (IOException e) {
 77			throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
 78					MAPParsingComponentExceptionReason.MistypedParameter);
 79		}
 80	}
 81
 82	@Override
 83	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
 84		
 85		try {
 86			this._decode(ansIS, length);
 87		} catch (IOException e) {
 88			throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
 89					MAPParsingComponentExceptionReason.MistypedParameter);
 90		}
 91	}
 92
 93	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
 94		
 95		if (length != 8)
 96			throw new MAPParsingComponentException("Error decoding IMEI: the IMEI field must contain from 8 octets. Contains: " + length,
 97					MAPParsingComponentExceptionReason.MistypedParameter);
 98
 99		try {
100			this.imei = this.decodeString(ansIS, length);
101		} catch (IOException e) {
102			throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
103					MAPParsingComponentExceptionReason.MistypedParameter);
104		}
105	}
106
107	@Override
108	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
109		
110		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
111	}
112
113	@Override
114	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
115		
116		try {
117			asnOs.writeTag(tagClass, true, tag);
118			int pos = asnOs.StartContentDefiniteLength();
119			this.encodeData(asnOs);
120			asnOs.FinalizeContent(pos);
121		} catch (AsnException e) {
122			throw new MAPException("AsnException when encoding IMEI: " + e.getMessage(), e);
123		}
124	}
125
126	@Override
127	public void encodeData(AsnOutputStream asnOs) throws MAPException {
128		
129		if (this.imei == null)
130			throw new MAPException("Error while encoding the IMEI: IMEI must not be null");
131
132		if (this.imei.length() < 15 || this.imei.length() > 16)
133			throw new MAPException("Error while encoding the IMEI: Bad IMEI length - must be 15 or 16");
134
135		this.encodeString(asnOs, this.imei);
136	}
137
138	@Override
139	public String toString() {
140		return "IMEI [IMEI=" + this.imei + "]";
141	}
142
143	@Override
144	public int hashCode() {
145		final int prime = 31;
146		int result = 1;
147		result = prime * result + ((imei == null) ? 0 : imei.hashCode());
148		return result;
149	}
150
151	@Override
152	public boolean equals(Object obj) {
153		if (this == obj)
154			return true;
155		if (obj == null)
156			return false;
157		if (getClass() != obj.getClass())
158			return false;
159		IMEIImpl other = (IMEIImpl) obj;
160		if (imei == null) {
161			if (other.imei != null)
162				return false;
163		} else if (!imei.equals(other.imei))
164			return false;
165		return true;
166	}
167	
168	
169}