1 16 17 20 package org.apache.log4j.helpers; 21 22 import org.apache.log4j.spi.LoggingEvent; 23 24 30 public class BoundedFIFO { 31 32 LoggingEvent[] buf; 33 int numElements = 0; 34 int first = 0; 35 int next = 0; 36 int maxSize; 37 38 41 public 42 BoundedFIFO(int maxSize) { 43 if(maxSize < 1) { 44 throw new IllegalArgumentException ("The maxSize argument ("+maxSize+ 45 ") is not a positive integer."); 46 } 47 this.maxSize = maxSize; 48 buf = new LoggingEvent[maxSize]; 49 } 50 51 54 public 55 LoggingEvent get() { 56 if(numElements == 0) 57 return null; 58 59 LoggingEvent r = buf[first]; 60 buf[first] = null; 62 if(++first == maxSize) { 63 first = 0; 64 } 65 numElements--; 66 return r; 67 } 68 69 73 public 74 void put(LoggingEvent o) { 75 if(numElements != maxSize) { 76 buf[next] = o; 77 if(++next == maxSize) { 78 next = 0; 79 } 80 numElements++; 81 } 82 } 83 84 87 public 88 int getMaxSize() { 89 return maxSize; 90 } 91 92 95 public 96 boolean isFull() { 97 return numElements == maxSize; 98 } 99 100 105 public 106 int length() { 107 return numElements; 108 } 109 110 111 int min(int a, int b) { 112 return a < b ? a : b; 113 } 114 115 116 122 synchronized 123 public 124 void resize(int newSize) { 125 if(newSize == maxSize) 126 return; 127 128 129 LoggingEvent[] tmp = new LoggingEvent[newSize]; 130 131 int len1 = maxSize - first; 133 134 len1 = min(len1, newSize); 136 137 len1 = min(len1, numElements); 140 141 System.arraycopy(buf, first, tmp, 0, len1); 143 144 int len2 = 0; 146 if((len1 < numElements) && (len1 < newSize)) { 147 len2 = numElements - len1; 148 len2 = min(len2, newSize - len1); 149 System.arraycopy(buf, 0, tmp, len1, len2); 150 } 151 152 this.buf = tmp; 153 this.maxSize = newSize; 154 this.first=0; 155 this.numElements = len1+len2; 156 this.next = this.numElements; 157 if(this.next == this.maxSize) this.next = 0; 159 } 160 161 162 166 public 167 boolean wasEmpty() { 168 return numElements == 1; 169 } 170 171 175 public 176 boolean wasFull() { 177 return (numElements+1 == maxSize); 178 } 179 180 } 181 | Popular Tags |