KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > CyclicBuffer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16      
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.spi.LoggingEvent;
20
21 /**
22
23    CyclicBuffer is used by other appenders to hold {@link LoggingEvent
24    LoggingEvents} for immediate or differed display.
25    
26    <p>This buffer gives read access to any element in the buffer not
27    just the first or last element.
28
29    @author Ceki G&uuml;lc&uuml;
30    @since 0.9.0
31
32  */

33 public class CyclicBuffer {
34   
35   LoggingEvent[] ea;
36   int first;
37   int last;
38   int numElems;
39   int maxSize;
40
41   /**
42      Instantiate a new CyclicBuffer of at most <code>maxSize</code> events.
43
44      The <code>maxSize</code> argument must a positive integer.
45
46      @param maxSize The maximum number of elements in the buffer.
47   */

48   public CyclicBuffer(int maxSize) throws IllegalArgumentException JavaDoc {
49     if(maxSize < 1) {
50       throw new IllegalArgumentException JavaDoc("The maxSize argument ("+maxSize+
51                 ") is not a positive integer.");
52     }
53     this.maxSize = maxSize;
54     ea = new LoggingEvent[maxSize];
55     first = 0;
56     last = 0;
57     numElems = 0;
58   }
59     
60   /**
61      Add an <code>event</code> as the last event in the buffer.
62
63    */

64   public
65   void add(LoggingEvent event) {
66     ea[last] = event;
67     if(++last == maxSize)
68       last = 0;
69
70     if(numElems < maxSize)
71       numElems++;
72     else if(++first == maxSize)
73       first = 0;
74   }
75
76
77   /**
78      Get the <i>i</i>th oldest event currently in the buffer. If
79      <em>i</em> is outside the range 0 to the number of elements
80      currently in the buffer, then <code>null</code> is returned.
81
82
83   */

84   public
85   LoggingEvent get(int i) {
86     if(i < 0 || i >= numElems)
87       return null;
88
89     return ea[(first + i) % maxSize];
90   }
91
92   public
93   int getMaxSize() {
94     return maxSize;
95   }
96
97   /**
98      Get the oldest (first) element in the buffer. The oldest element
99      is removed from the buffer.
100   */

101   public
102   LoggingEvent get() {
103     LoggingEvent r = null;
104     if(numElems > 0) {
105       numElems--;
106       r = ea[first];
107       ea[first] = null;
108       if(++first == maxSize)
109     first = 0;
110     }
111     return r;
112   }
113   
114   /**
115      Get the number of elements in the buffer. This number is
116      guaranteed to be in the range 0 to <code>maxSize</code>
117      (inclusive).
118   */

119   public
120   int length() {
121     return numElems;
122   }
123
124   /**
125      Resize the cyclic buffer to <code>newSize</code>.
126
127      @throws IllegalArgumentException if <code>newSize</code> is negative.
128    */

129   public
130   void resize(int newSize) {
131     if(newSize < 0) {
132       throw new IllegalArgumentException JavaDoc("Negative array size ["+newSize+
133                      "] not allowed.");
134     }
135     if(newSize == numElems)
136       return; // nothing to do
137

138     LoggingEvent[] temp = new LoggingEvent[newSize];
139
140     int loopLen = newSize < numElems ? newSize : numElems;
141     
142     for(int i = 0; i < loopLen; i++) {
143       temp[i] = ea[first];
144       ea[first] = null;
145       if(++first == numElems)
146     first = 0;
147     }
148     ea = temp;
149     first = 0;
150     numElems = loopLen;
151     maxSize = newSize;
152     if (loopLen == newSize) {
153       last = 0;
154     } else {
155       last = loopLen;
156     }
157   }
158 }
159
Popular Tags