KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > ObjectBuffer


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.util;
19
20 import freecs.Server;
21
22 /**
23  * FIFO-Buffer for objects whicht automatically removes Objects retrieved via
24  * get(). Programmers have to verify if value has bin put to this ObjectBuffer.
25  * If buffer is full, put-method returns false, on success it returns true.
26  */

27 public class ObjectBuffer {
28    private Object JavaDoc elements[];
29    private volatile int nextAdd;
30    private volatile int nextRemove;
31    private volatile int capacity;
32    private volatile int counter=0;
33
34    public ObjectBuffer (int cap) throws IllegalArgumentException JavaDoc {
35       if (cap < 2)
36          throw new IllegalArgumentException JavaDoc("Capacity must be higher than 1 to make sense");
37       this.capacity = cap;
38       this.nextAdd=0;
39       this.nextRemove=0;
40       elements = new Object JavaDoc[capacity];
41       if (Server.TRACE_CREATE_AND_FINALIZE)
42           Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
43    }
44
45    public int size () {
46       return counter;
47    }
48
49    public int capacity () {
50       return capacity;
51    }
52
53    public boolean isEmpty () {
54       return counter==0;
55    }
56
57    public boolean isFull () {
58       return counter==capacity;
59    }
60
61    public boolean put (Object JavaDoc o) {
62        if (o==null)
63            throw new NullPointerException JavaDoc ("ObjectBuffer unable to store null");
64       if (this.isFull ())
65           return false;
66       elements[nextAdd] = o;
67       nextAdd++;
68       if (nextAdd >= capacity)
69          nextAdd = 0;
70       counter++;
71       return true;
72    }
73     
74     public boolean contains (Object JavaDoc o) {
75         for (int i = 0, j=nextRemove; i < counter; i++, j++) {
76             if (j >= capacity) j = 0;
77             if (elements[j].equals (o)) return true;
78         }
79         return false;
80     }
81
82    public Object JavaDoc get () {
83       if (this.isEmpty ()) return null;
84       return elements[nextRemove];
85    }
86
87    public Object JavaDoc pop () {
88       if (this.isEmpty ()) return null;
89       Object JavaDoc retObj = elements[nextRemove];
90       elements[nextRemove]=null;
91       nextRemove++;
92       if (nextRemove >= capacity)
93          nextRemove = 0;
94       counter--;
95       return (retObj);
96    }
97     
98     public void inhale (ObjectBuffer ob) {
99         elements = new Object JavaDoc[ob.capacity()];
100         while (!ob.isEmpty())
101             this.put(ob.pop());
102     }
103     
104     public void resizeTo (int size) throws IllegalArgumentException JavaDoc {
105         if (size < this.size())
106             throw new IllegalArgumentException JavaDoc("new capacity may not be lower than current size");
107         if (size == elements.length)
108             return;
109         Object JavaDoc newelements[] = new Object JavaDoc[size];
110         synchronized (this) {
111             int csize = this.size();
112             if (isEmpty()) {
113                 elements = newelements;
114                 return;
115             }
116             if (nextAdd <= nextRemove) {
117                 System.arraycopy(elements,nextRemove,newelements,0,this.capacity()-nextRemove);
118                 System.arraycopy(elements, 0, newelements, this.capacity()-nextRemove, nextAdd);
119             } else {
120                 System.arraycopy(elements,nextRemove,newelements,0,nextAdd+1);
121             }
122             nextAdd = this.size();
123             nextRemove=0;
124             elements = newelements;
125         }
126     }
127
128     public void finalize() {
129         if (Server.TRACE_CREATE_AND_FINALIZE)
130             Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
131     }
132 }
Popular Tags