PageRenderTime 51ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/Address.java

http://mobicents.googlecode.com/
Java | 170 lines | 73 code | 20 blank | 77 comment | 11 complexity | 8c0f476e6b7a882e7079988c02e085c8 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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.smpp;
  23. import org.mobicents.protocols.smpp.util.PacketDecoder;
  24. import org.mobicents.protocols.smpp.util.PacketEncoder;
  25. /**
  26. * Object representing a Short Message Entity's address. An address consists of
  27. * a Type Of Number, a Numbering Plan Indicator and an address.
  28. *
  29. * @see com.adenki.smpp.util.GSMConstants
  30. */
  31. public class Address implements java.io.Serializable {
  32. private static final long serialVersionUID = 2L;
  33. /** Type of number. */
  34. private int ton = Ton.UNKNOWN;
  35. /** Numbering plan indicator. */
  36. private int npi = Npi.UNKNOWN;
  37. /** The address. */
  38. private String address = "";
  39. /**
  40. * Create a new Address with all nul values. TON will be 0, NPI will be 0
  41. * and the address field will be blank.
  42. */
  43. public Address() {
  44. }
  45. /**
  46. * Create a new Address.
  47. *
  48. * @param ton
  49. * The Type Of Number.
  50. * @param npi
  51. * The Numbering Plan Indicator.
  52. * @param address
  53. * The address.
  54. */
  55. public Address(int ton, int npi, String address) {
  56. this.ton = ton;
  57. this.npi = npi;
  58. this.address = address;
  59. }
  60. /**
  61. * Get the Type Of Number.
  62. */
  63. public int getTON() {
  64. return ton;
  65. }
  66. /**
  67. * Set the Type of Number.
  68. */
  69. public void setTON(int ton) {
  70. this.ton = ton;
  71. }
  72. /**
  73. * Get the Numbering Plan Indicator.
  74. */
  75. public int getNPI() {
  76. return npi;
  77. }
  78. /**
  79. * Set the Numbering Plan Indicator.
  80. */
  81. public void setNPI(int npi) {
  82. this.npi = npi;
  83. }
  84. /**
  85. * Get the address.
  86. */
  87. public String getAddress() {
  88. return address;
  89. }
  90. /**
  91. * Set the address.
  92. */
  93. public void setAddress(String address) {
  94. this.address = (address != null) ? address : "";
  95. }
  96. /**
  97. * Get a unique hash code for this address.
  98. */
  99. public int hashCode() {
  100. StringBuilder buf = new StringBuilder();
  101. buf.append(Integer.toString(ton)).append(':');
  102. buf.append(Integer.toString(npi)).append(':');
  103. if (address != null) {
  104. buf.append(address);
  105. }
  106. return buf.hashCode();
  107. }
  108. /**
  109. * Test for equality. Two address objects are equal if their TON, NPI and
  110. * address fields are equal.
  111. */
  112. public boolean equals(Object obj) {
  113. if (obj == null) {
  114. return false;
  115. }
  116. if (obj instanceof Address) {
  117. Address a = (Address) obj;
  118. return (a.ton == ton) && (a.npi == npi) && (a.address.equals(address));
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Get the number of bytes this object would encode to.
  125. */
  126. public int getLength() {
  127. return 3 + address.length();
  128. }
  129. /**
  130. */
  131. public void writeTo(PacketEncoder encoder) throws java.io.IOException {
  132. encoder.writeUInt1(ton);
  133. encoder.writeUInt1(npi);
  134. encoder.writeCString(address);
  135. }
  136. /**
  137. * TODO: doc
  138. */
  139. public void readFrom(PacketDecoder decoder) {
  140. ton = decoder.readUInt1();
  141. npi = decoder.readUInt1();
  142. address = decoder.readCString();
  143. }
  144. public String toString() {
  145. return new StringBuffer(25)
  146. .append(Integer.toString(ton)).append(':')
  147. .append(Integer.toString(npi)).append(':')
  148. .append(address).toString();
  149. }
  150. }