/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. package org.mobicents.protocols.ss7.map.primitives;
  23. import java.io.IOException;
  24. import org.mobicents.protocols.asn.AsnException;
  25. import org.mobicents.protocols.asn.AsnInputStream;
  26. import org.mobicents.protocols.asn.AsnOutputStream;
  27. import org.mobicents.protocols.asn.Tag;
  28. import org.mobicents.protocols.ss7.map.api.MAPException;
  29. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  30. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  31. import org.mobicents.protocols.ss7.map.api.primitives.IMEI;
  32. /**
  33. *
  34. * @author sergey vetyutnev
  35. *
  36. */
  37. public class IMEIImpl extends TbcdString implements IMEI {
  38. private String imei;
  39. public IMEIImpl() {
  40. }
  41. public IMEIImpl(String imei) {
  42. this.imei = imei;
  43. }
  44. @Override
  45. public String getIMEI() {
  46. return this.imei;
  47. }
  48. public int getTag() throws MAPException {
  49. return Tag.STRING_OCTET;
  50. }
  51. public int getTagClass() {
  52. return Tag.CLASS_UNIVERSAL;
  53. }
  54. public boolean getIsPrimitive() {
  55. return true;
  56. }
  57. @Override
  58. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  59. try {
  60. int length = ansIS.readLength();
  61. this._decode(ansIS, length);
  62. } catch (IOException e) {
  63. throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
  64. MAPParsingComponentExceptionReason.MistypedParameter);
  65. }
  66. }
  67. @Override
  68. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  69. try {
  70. this._decode(ansIS, length);
  71. } catch (IOException e) {
  72. throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
  73. MAPParsingComponentExceptionReason.MistypedParameter);
  74. }
  75. }
  76. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
  77. if (length != 8)
  78. throw new MAPParsingComponentException("Error decoding IMEI: the IMEI field must contain from 8 octets. Contains: " + length,
  79. MAPParsingComponentExceptionReason.MistypedParameter);
  80. try {
  81. this.imei = this.decodeString(ansIS, length);
  82. } catch (IOException e) {
  83. throw new MAPParsingComponentException("IOException when decoding IMEI: " + e.getMessage(), e,
  84. MAPParsingComponentExceptionReason.MistypedParameter);
  85. }
  86. }
  87. @Override
  88. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  89. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  90. }
  91. @Override
  92. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  93. try {
  94. asnOs.writeTag(tagClass, true, tag);
  95. int pos = asnOs.StartContentDefiniteLength();
  96. this.encodeData(asnOs);
  97. asnOs.FinalizeContent(pos);
  98. } catch (AsnException e) {
  99. throw new MAPException("AsnException when encoding IMEI: " + e.getMessage(), e);
  100. }
  101. }
  102. @Override
  103. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  104. if (this.imei == null)
  105. throw new MAPException("Error while encoding the IMEI: IMEI must not be null");
  106. if (this.imei.length() < 15 || this.imei.length() > 16)
  107. throw new MAPException("Error while encoding the IMEI: Bad IMEI length - must be 15 or 16");
  108. this.encodeString(asnOs, this.imei);
  109. }
  110. @Override
  111. public String toString() {
  112. return "IMEI [IMEI=" + this.imei + "]";
  113. }
  114. @Override
  115. public int hashCode() {
  116. final int prime = 31;
  117. int result = 1;
  118. result = prime * result + ((imei == null) ? 0 : imei.hashCode());
  119. return result;
  120. }
  121. @Override
  122. public boolean equals(Object obj) {
  123. if (this == obj)
  124. return true;
  125. if (obj == null)
  126. return false;
  127. if (getClass() != obj.getClass())
  128. return false;
  129. IMEIImpl other = (IMEIImpl) obj;
  130. if (imei == null) {
  131. if (other.imei != null)
  132. return false;
  133. } else if (!imei.equals(other.imei))
  134. return false;
  135. return true;
  136. }
  137. }