/protocols/jain-mgcp/stack/src/main/java/org/mobicents/protocols/mgcp/utils/PacketRepresentationFactory.java

http://mobicents.googlecode.com/ · Java · 93 lines · 52 code · 20 blank · 21 comment · 7 complexity · 6da8927148367002493664888a9d13fb 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.mgcp.utils;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.apache.log4j.Logger;
  26. public class PacketRepresentationFactory {
  27. private static final Logger logger = Logger.getLogger(PacketRepresentationFactory.class);
  28. private List<PacketRepresentation> list = new ArrayList<PacketRepresentation>();
  29. private int size = 0;
  30. private int dataArrSize = 0;
  31. private int count = 0;
  32. public PacketRepresentationFactory(int size, int dataArrSize) {
  33. this.size = size;
  34. this.dataArrSize = dataArrSize;
  35. for (int i = 0; i < size; i++) {
  36. PacketRepresentation pr = new PacketRepresentation(dataArrSize, this);
  37. list.add(pr);
  38. }
  39. }
  40. public PacketRepresentation allocate() {
  41. PacketRepresentation pr = null;
  42. if (!list.isEmpty()) {
  43. pr = list.remove(0);
  44. }
  45. if(pr!=null){
  46. pr.setLength(0);
  47. return pr;
  48. }
  49. pr = new PacketRepresentation(this.dataArrSize, this);
  50. count++;
  51. if (logger.isDebugEnabled()) {
  52. logger.debug("UtilsFactory underflow. Count = " + count);
  53. }
  54. logger.error("UtilsFactory underflow. Count = " + count);
  55. return pr;
  56. }
  57. public void deallocate(PacketRepresentation pr) {
  58. if (list.size() < size && pr != null) {
  59. list.add(pr);
  60. } else{
  61. System.out.println("Discarding the PR "+pr);
  62. }
  63. }
  64. public int getSize() {
  65. return this.size;
  66. }
  67. public int getDataArrSize() {
  68. return this.dataArrSize;
  69. }
  70. public int getCount() {
  71. return this.count;
  72. }
  73. }