CircularBuffer.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.server.util;
022 
023 import java.util.Iterator;
024 
025 import org.apache.log4j.Logger;
026 
027 public class CircularBuffer implements Iterable
028 {
029 
030     private static final Logger _logger = Logger.getLogger(CircularBuffer.class);
031 
032     private final Object[] _log;
033     private int _size;
034     private int _index;
035 
036     public CircularBuffer(int size)
037     {
038         _log = new Object[size];
039     }
040 
041     public void add(Object o)
042     {
043         _log[_index++= o;
044         _size = Math.min(_size+1, _log.length);
045         if(_index >= _log.length)
046         {
047             _index = 0;
048         }
049     }
050 
051     public Object get(int i)
052     {
053         if(i >= _log.length)
054         {
055             throw new ArrayIndexOutOfBoundsException(i);
056         }
057         return _log[index(i)];
058     }
059 
060     public int size() {
061         return _size;
062     }
063 
064     public Iterator iterator()
065     {
066         return new Iterator()
067         {
068             private int i = 0;
069 
070             public boolean hasNext()
071             {
072                 return i < _size;
073             }
074 
075             public Object next()
076             {
077                 return get(i++);
078             }
079 
080             public void remove()
081             {
082                 throw new UnsupportedOperationException();
083             }
084         };
085     }
086 
087     public String toString()
088     {
089         StringBuilder s = new StringBuilder();
090         boolean first = true;
091         for(Object o : this)
092         {
093             if(!first)
094             {
095                 s.append(", ");
096             }
097             else
098             {
099                 first = false;
100             }
101             s.append(o);
102         }
103         return s.toString();
104     }
105 
106     public void dump()
107     {
108         for(Object o : this)
109         {
110          _logger.info(o);
111         }
112     }
113 
114     int index(int i)
115     {
116         return _size == _log.length ? (_index + i% _log.length : i;
117     }
118 
119     public static void main(String[] artgv)
120     {
121         String[] items = new String[]{
122                 "A","B","C","D","E","F","G","H","I","J","K"
123         };
124         CircularBuffer buffer = new CircularBuffer(5);
125         for(String s : items)
126         {
127             buffer.add(s);
128             _logger.info(buffer);
129         }
130     }
131 }