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

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/DestinationTable.java

http://mobicents.googlecode.com/
Java | 162 lines | 104 code | 22 blank | 36 comment | 21 complexity | 39ea238ab4546ad55b27ddd7a3629c9a 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.message;
  23. import java.io.Serializable;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import org.slf4j.LoggerFactory;
  29. import org.mobicents.protocols.smpp.Address;
  30. import org.mobicents.protocols.smpp.util.PacketDecoder;
  31. import org.mobicents.protocols.smpp.util.PacketEncoder;
  32. /**
  33. * A table of destinations, primarily used in
  34. * {@link org.mobicents.protocols.smpp.message.SubmitMulti}.
  35. * @version $Id: DestinationTable.java 457 2009-01-15 17:37:42Z orank $
  36. */
  37. public class DestinationTable implements Serializable {
  38. private static final long serialVersionUID = 2L;
  39. private List<Address> addresses = new ArrayList<Address>();
  40. private List<String> distributionLists = new ArrayList<String>();
  41. /**
  42. * The length is the total number of bytes the table would encode as.
  43. */
  44. private int length;
  45. public DestinationTable() {
  46. }
  47. public void add(Address addr) {
  48. addresses.add(addr);
  49. // Plus 1 for the dest type flag.
  50. length += addr.getLength() + 1;
  51. }
  52. public void add(String distributionList) {
  53. distributionLists.add(distributionList);
  54. // nul byte plus dest type flag
  55. length += distributionList.length() + 2;
  56. }
  57. public void remove(Address addr) {
  58. int i = addresses.indexOf(addr);
  59. if (i > -1) {
  60. length -= addresses.remove(i).getLength() + 1;
  61. }
  62. }
  63. public void remove(String distributionList) {
  64. int i = distributionLists.indexOf(distributionList);
  65. if (i > -1) {
  66. length -= distributionLists.remove(i).length() + 2;
  67. }
  68. }
  69. public int getLength() {
  70. return length;
  71. }
  72. public int size() {
  73. return addresses.size() + distributionLists.size();
  74. }
  75. public Collection<Address> getAddresses() {
  76. return Collections.unmodifiableCollection(addresses);
  77. }
  78. public Collection<String> getDistributionLists() {
  79. return Collections.unmodifiableCollection(distributionLists);
  80. }
  81. public void writeTo(PacketEncoder encoder) throws java.io.IOException {
  82. for (Address address : addresses) {
  83. encoder.writeUInt1(1);
  84. encoder.writeAddress(address);
  85. }
  86. for (String list : distributionLists) {
  87. encoder.writeUInt1(2);
  88. encoder.writeCString(list);
  89. }
  90. }
  91. public void readFrom(PacketDecoder decoder, int count) {
  92. for (int i = 0; i < count; i++) {
  93. int type = decoder.readUInt1();
  94. if (type == 1) {
  95. // SME address..
  96. addresses.add(decoder.readAddress());
  97. } else if (type == 2) {
  98. // Distribution list name
  99. distributionLists.add(decoder.readCString());
  100. } else {
  101. LoggerFactory.getLogger(DestinationTable.class).warn(
  102. "Unidentified destination type on input.");
  103. }
  104. }
  105. calculateLength();
  106. }
  107. public boolean equals(Object obj) {
  108. if (this == obj) {
  109. return true;
  110. }
  111. if (obj == null || !(obj instanceof DestinationTable)) {
  112. return false;
  113. }
  114. DestinationTable other = (DestinationTable) obj;
  115. return length == other.length
  116. && addresses.equals(other.addresses)
  117. && distributionLists.equals(other.distributionLists);
  118. }
  119. public int hashCode() {
  120. return addresses.hashCode() + distributionLists.hashCode();
  121. }
  122. public String toString() {
  123. List<Object> list = new ArrayList<Object>();
  124. list.addAll(addresses);
  125. list.addAll(distributionLists);
  126. return list.toString();
  127. }
  128. private void calculateLength() {
  129. // One byte for all type flags, plus 1 (null) byte for each distribution
  130. // list string
  131. length = addresses.size() + (distributionLists.size() * 2);
  132. for (Address address : addresses) {
  133. // For the destination type flag
  134. length += address.getLength();
  135. }
  136. for (String list : distributionLists) {
  137. length += list.length();
  138. }
  139. }
  140. }