1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.helpers;
19
20 import org.apache.log4j.spi.LoggingEvent;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class CyclicBuffer {
35
36 LoggingEvent[] ea;
37 int first;
38 int last;
39 int numElems;
40 int maxSize;
41
42
43
44
45
46
47
48
49 public CyclicBuffer(int maxSize) throws IllegalArgumentException {
50 if(maxSize < 1) {
51 throw new IllegalArgumentException("The maxSize argument ("+maxSize+
52 ") is not a positive integer.");
53 }
54 this.maxSize = maxSize;
55 ea = new LoggingEvent[maxSize];
56 first = 0;
57 last = 0;
58 numElems = 0;
59 }
60
61
62
63
64
65 public
66 void add(LoggingEvent event) {
67 ea[last] = event;
68 if(++last == maxSize)
69 last = 0;
70
71 if(numElems < maxSize)
72 numElems++;
73 else if(++first == maxSize)
74 first = 0;
75 }
76
77
78
79
80
81
82
83
84
85 public
86 LoggingEvent get(int i) {
87 if(i < 0 || i >= numElems)
88 return null;
89
90 return ea[(first + i) % maxSize];
91 }
92
93 public
94 int getMaxSize() {
95 return maxSize;
96 }
97
98
99
100
101
102 public
103 LoggingEvent get() {
104 LoggingEvent r = null;
105 if(numElems > 0) {
106 numElems--;
107 r = ea[first];
108 ea[first] = null;
109 if(++first == maxSize)
110 first = 0;
111 }
112 return r;
113 }
114
115
116
117
118
119
120 public
121 int length() {
122 return numElems;
123 }
124
125
126
127
128
129
130 public
131 void resize(int newSize) {
132 if(newSize < 0) {
133 throw new IllegalArgumentException("Negative array size ["+newSize+
134 "] not allowed.");
135 }
136 if(newSize == numElems)
137 return;
138
139 LoggingEvent[] temp = new LoggingEvent[newSize];
140
141 int loopLen = newSize < numElems ? newSize : numElems;
142
143 for(int i = 0; i < loopLen; i++) {
144 temp[i] = ea[first];
145 ea[first] = null;
146 if(++first == numElems)
147 first = 0;
148 }
149 ea = temp;
150 first = 0;
151 numElems = loopLen;
152 maxSize = newSize;
153 if (loopLen == newSize) {
154 last = 0;
155 } else {
156 last = loopLen;
157 }
158 }
159 }