/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/ServiceIndicatorsImpl.java

http://mobicents.googlecode.com/ · Java · 119 lines · 68 code · 19 blank · 32 comment · 4 complexity · 55ad3b3c307451338622ad409e0b6d02 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.m3ua.impl.parameter;
  23. import java.util.Arrays;
  24. import javolution.xml.XMLFormat;
  25. import javolution.xml.XMLSerializable;
  26. import javolution.xml.stream.XMLStreamException;
  27. import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
  28. import org.mobicents.protocols.ss7.m3ua.parameter.ServiceIndicators;
  29. /**
  30. *
  31. * @author amit bhayani
  32. *
  33. */
  34. public class ServiceIndicatorsImpl extends ParameterImpl implements ServiceIndicators, XMLSerializable {
  35. private static final String INDICATOR = "indicator";
  36. private static final String ARRAY_SIZE = "size";
  37. private short[] indicators;
  38. private byte[] value = null;
  39. public ServiceIndicatorsImpl() {
  40. this.tag = Parameter.Service_Indicators;
  41. }
  42. protected ServiceIndicatorsImpl(short[] inds) {
  43. this.tag = Parameter.Service_Indicators;
  44. this.indicators = inds;
  45. this.encode();
  46. }
  47. protected ServiceIndicatorsImpl(byte[] value) {
  48. this.tag = Parameter.Service_Indicators;
  49. this.indicators = new short[value.length];
  50. for (int i = 0; i < value.length; i++) {
  51. this.indicators[i] = value[i];
  52. }
  53. this.value = value;
  54. }
  55. private void encode() {
  56. // create byte array taking into account data, point codes and
  57. // indicators;
  58. this.value = new byte[indicators.length];
  59. int count = 0;
  60. // encode routing context
  61. while (count < value.length) {
  62. value[count] = (byte) indicators[count++];
  63. }
  64. }
  65. @Override
  66. protected byte[] getValue() {
  67. return this.value;
  68. }
  69. public short[] getIndicators() {
  70. return this.indicators;
  71. }
  72. @Override
  73. public String toString() {
  74. return String.format("ServiceIndicators ids=%s", Arrays.toString(this.indicators));
  75. }
  76. /**
  77. * XML Serialization/Deserialization
  78. */
  79. protected static final XMLFormat<ServiceIndicatorsImpl> RC_XML = new XMLFormat<ServiceIndicatorsImpl>(
  80. ServiceIndicatorsImpl.class) {
  81. @Override
  82. public void read(javolution.xml.XMLFormat.InputElement xml, ServiceIndicatorsImpl si) throws XMLStreamException {
  83. int size = xml.getAttribute(ARRAY_SIZE).toInt();
  84. si.indicators = new short[size];
  85. size = 0;
  86. while (xml.hasNext()) {
  87. si.indicators[size++] = xml.get(INDICATOR);
  88. }
  89. si.encode();
  90. }
  91. @Override
  92. public void write(ServiceIndicatorsImpl si, javolution.xml.XMLFormat.OutputElement xml)
  93. throws XMLStreamException {
  94. xml.setAttribute(ARRAY_SIZE, si.indicators.length);
  95. for (Short s : si.indicators) {
  96. xml.add(s, INDICATOR);
  97. }
  98. }
  99. };
  100. }