Changeset 5311 for openmrs-modules/gmapsimageviewer/src/org/openmrs/module/gmapsimageviewer/CircularBuffer.java
- Timestamp:
- 08/18/08 18:57:52 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs-modules/gmapsimageviewer/src/org/openmrs/module/gmapsimageviewer/CircularBuffer.java
r5176 r5311 6 6 import java.util.NoSuchElementException; 7 7 8 /** 9 * This class implements the Collection interface. The CircularBuffer is 10 * initialised with the given capacity. As Objects are added, they may overwrite 11 * older ones. 12 * 13 * @param <E> 14 */ 8 15 public class CircularBuffer<E> implements Collection<E> { 9 16 10 private Object[] array;17 private Object[] array; 11 18 12 private int next = 0;19 private int next = 0; 13 20 14 private boolean full = false;21 private boolean full = false; 15 22 16 public CircularBuffer(int size) { 17 array = new Object[size]; 18 } 23 /** 24 * Creates a new CircularBuffer with the given capacity. 25 */ 26 public CircularBuffer(int size) { 27 array = new Object[size]; 28 } 19 29 20 public boolean add(E o) { 21 if (array.length == 0) 22 return false; 23 if (contains(o)) 24 return false; 30 /** 31 * @see java.util.Collection#add(Object); 32 */ 33 public boolean add(E o) { 34 if (array.length == 0) 35 return false; 36 if (contains(o)) 37 return false; 25 38 26 array[next] = o;27 next = (next + 1) % array.length;28 if(!full && next==0)29 full = true;30 return true;31 }39 array[next] = o; 40 next = (next + 1) % array.length; 41 if (!full && next == 0) 42 full = true; 43 return true; 44 } 32 45 33 public boolean addAll(Collection<? extends E> o) { 34 boolean returnvalue = false; 35 for (E e : o) { 36 if (add(e)) 37 returnvalue = true; 38 } 39 return returnvalue; 40 } 46 /** 47 * @see java.util.Collection#addAll(Collection) 48 */ 49 public boolean addAll(Collection<? extends E> o) { 50 boolean returnvalue = false; 51 for (E e : o) { 52 if (add(e)) 53 returnvalue = true; 54 } 55 return returnvalue; 56 } 41 57 42 public void clear() { 43 array = new Object[array.length]; 44 full = false; 45 next = 0; 46 } 58 /** 59 * @see java.util.Collection#clear() 60 */ 61 public void clear() { 62 array = new Object[array.length]; 63 full = false; 64 next = 0; 65 } 47 66 48 public boolean contains(Object o) { 49 for (Object e : array) { 50 if (o == null ? e == null : o.equals(e)) 51 return true; 52 } 53 return false; 54 } 67 /** 68 * @see java.util.Collection#contains(Object) 69 */ 70 public boolean contains(Object o) { 71 for (Object e : array) { 72 if (o == null ? e == null : o.equals(e)) 73 return true; 74 } 75 return false; 76 } 55 77 56 public boolean containsAll(Collection<?> c) { 57 if (c == null) 58 throw new NullPointerException("Collection was null"); 59 boolean returnvalue = true; 60 for (Object e : c) { 61 if (!contains(e)) 62 return false; 63 } 64 return returnvalue; 65 } 78 /** 79 * @see java.util.Collection#containsAll(Collection) 80 */ 81 public boolean containsAll(Collection<?> c) { 82 if (c == null) 83 throw new NullPointerException("Collection was null"); 84 boolean returnvalue = true; 85 for (Object e : c) { 86 if (!contains(e)) 87 return false; 88 } 89 return returnvalue; 90 } 66 91 67 public boolean isEmpty() { 68 if(!full && next==0) 69 return true; 70 return false; 71 } 92 /** 93 * @see java.util.Collection#isEmpty() 94 */ 95 public boolean isEmpty() { 96 if (!full && next == 0) 97 return true; 98 return false; 99 } 72 100 73 @SuppressWarnings("unchecked") 74 public Iterator<E> iterator() { 75 return new Iterator<E>() { 76 E[] o = (E[]) toArray(); 101 /** 102 * @see java.util.Collection#iterator() 103 */ 104 @SuppressWarnings("unchecked") 105 public Iterator<E> iterator() { 106 return new Iterator<E>() { 107 E[] o = (E[]) toArray(); 77 108 78 int index = 0;109 int index = 0; 79 110 80 public boolean hasNext() {81 if (index < o.length)82 return true;83 o = null;//Prevents memory leak84 return false;85 }111 public boolean hasNext() { 112 if (index < o.length) 113 return true; 114 o = null;// Prevents memory leak 115 return false; 116 } 86 117 87 public E next() {88 if (hasNext()) {89 return o[index++];90 }91 throw new NoSuchElementException();92 }118 public E next() { 119 if (hasNext()) { 120 return o[index++]; 121 } 122 throw new NoSuchElementException(); 123 } 93 124 94 public void remove() {95 throw new UnsupportedOperationException();96 }97 };98 }125 public void remove() { 126 throw new UnsupportedOperationException(); 127 } 128 }; 129 } 99 130 100 public boolean remove(Object o) { 101 throw new UnsupportedOperationException(); 102 } 131 /** 132 * Not supported in this implementation. 133 * 134 * @see java.util.Collection#remove(Object) 135 */ 136 public boolean remove(Object o) { 137 throw new UnsupportedOperationException(); 138 } 103 139 104 public boolean removeAll(Collection<?> c) { 105 throw new UnsupportedOperationException(); 106 } 140 /** 141 * Not supported in this implementation. 142 * 143 * @see java.util.Collection#removeAll(Collection) 144 */ 145 public boolean removeAll(Collection<?> c) { 146 throw new UnsupportedOperationException(); 147 } 107 148 108 public boolean retainAll(Collection<?> c) { 109 throw new UnsupportedOperationException(); 110 } 149 /** 150 * Not supported in this implementation. 151 * 152 * @see java.util.Collection# 153 */ 154 public boolean retainAll(Collection<?> c) { 155 throw new UnsupportedOperationException(); 156 } 111 157 112 public int size() { 113 if (full) 114 return array.length; 115 return next; 116 } 158 /** 159 * @see java.util.Collection#size() 160 */ 161 public int size() { 162 if (full) 163 return array.length; 164 return next; 165 } 117 166 118 public Object[] toArray() { 119 Object[] o = new Object[size()]; 120 System.arraycopy(array, 0, o, 0, o.length); 121 return o; 122 } 167 /** 168 * @see java.util.Collection#toArray() 169 */ 170 public Object[] toArray() { 171 Object[] o = new Object[size()]; 172 System.arraycopy(array, 0, o, 0, o.length); 173 return o; 174 } 123 175 124 public <T> T[] toArray(T[] a) { 125 if (a.length < size()) { 126 ArrayList<Object> aList = new ArrayList<Object>(); 127 for (Object item : toArray()) { 128 aList.add(item); 129 } 130 return aList.toArray(a); 131 } 132 System.arraycopy(array, 0, a, 0, size()); 133 if (a.length > size()) 134 a[size()] = null; 135 return a; 136 } 176 /** 177 * @see java.util.Collection#toArray(Object[]) 178 */ 179 public <T> T[] toArray(T[] a) { 180 if (a.length < size()) { 181 ArrayList<Object> aList = new ArrayList<Object>(); 182 for (Object item : toArray()) { 183 aList.add(item); 184 } 185 return aList.toArray(a); 186 } 187 System.arraycopy(array, 0, a, 0, size()); 188 if (a.length > size()) 189 a[size()] = null; 190 return a; 191 } 137 192 138 193 }