PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/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
23package org.mobicents.protocols.mgcp.utils;
24
25import java.util.ArrayList;
26import java.util.List;
27
28import org.apache.log4j.Logger;
29
30public class PacketRepresentationFactory {
31
32	private static final Logger logger = Logger.getLogger(PacketRepresentationFactory.class);
33
34	private List<PacketRepresentation> list = new ArrayList<PacketRepresentation>();
35	private int size = 0;
36	private int dataArrSize = 0;
37	private int count = 0;
38
39	public PacketRepresentationFactory(int size, int dataArrSize) {
40		this.size = size;
41		this.dataArrSize = dataArrSize;
42		for (int i = 0; i < size; i++) {
43			PacketRepresentation pr = new PacketRepresentation(dataArrSize, this); 
44			list.add(pr);
45		}
46	}
47
48	public PacketRepresentation allocate() {
49		PacketRepresentation pr = null;
50
51		if (!list.isEmpty()) {
52			pr = list.remove(0);
53			
54		}
55		
56		if(pr!=null){
57			pr.setLength(0);
58			return pr;
59		}
60
61		pr = new PacketRepresentation(this.dataArrSize, this);
62		count++;
63
64		if (logger.isDebugEnabled()) {
65			logger.debug("UtilsFactory underflow. Count = " + count);			
66		}
67		
68		logger.error("UtilsFactory underflow. Count = " + count);
69		
70		return pr;
71	}
72
73	public void deallocate(PacketRepresentation pr) {
74		if (list.size() < size && pr != null) {
75			list.add(pr);
76		} else{
77			System.out.println("Discarding the PR "+pr);
78		}
79	}
80
81	public int getSize() {
82		return this.size;
83	}
84
85	public int getDataArrSize() {
86		return this.dataArrSize;
87	}
88
89	public int getCount() {
90		return this.count;
91	}
92
93}