001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.collections; 025 026 import java.io.Serializable; 027 import java.util.Collection; 028 import java.util.Iterator; 029 import java.util.List; 030 import java.util.ListIterator; 031 032 public class RingBufferList<A> implements List<A>, Serializable { 033 private static final long serialVersionUID = 7887830184838493458L; 034 035 private int start = -1; 036 037 private int end = -1; 038 039 private Object values[]; 040 041 public RingBufferList() { 042 this(10); 043 } 044 045 public RingBufferList(int maximumSize) { 046 values = new Object[maximumSize]; 047 } 048 049 public int maxSize() { 050 return values.length; 051 } 052 053 public boolean add(A a) { 054 if (end >= 0) { 055 end++; 056 if (end >= values.length) { 057 end = 0; 058 } 059 if (end == start) { 060 start++; 061 } 062 if (start >= values.length) { 063 start = 0; 064 } 065 } else { 066 start = 0; 067 end = 0; 068 } 069 values[end] = a; 070 return true; 071 } 072 073 public int size() { 074 if (end < 0) { 075 return 0; 076 } 077 return end < start ? values.length : end - start + 1; 078 } 079 080 081 public String toString() { 082 StringBuilder s = new StringBuilder(); 083 for (int i = 0; i < size(); i++) { 084 s.append(get(i)); 085 if (i < size() - 1) { 086 s.append(", "); 087 } 088 } 089 return s.toString(); 090 } 091 092 public void addFirst(A a) { 093 if (end >= 0) { 094 start--; 095 if (start < 0) { 096 start = values.length - 1; 097 } 098 if (start == end) { 099 end--; 100 } 101 if (end < 0) { 102 end = values.length - 1; 103 } 104 } else { 105 start = 0; 106 end = 0; 107 } 108 values[start] = a; 109 } 110 111 @SuppressWarnings("unchecked") 112 public A get(int index) { 113 return (A) values[(start + index) % values.length]; 114 } 115 116 @SuppressWarnings("unchecked") 117 public A set(int index, A a) { 118 A old = (A) values[(start + index) % values.length]; 119 values[(start + index) % values.length] = a; 120 return old; 121 } 122 123 public void clear() { 124 start = -1; 125 end = -1; 126 } 127 128 public void add(int index, A element) { 129 new Exception("not implemented").printStackTrace(); 130 } 131 132 public boolean addAll(Collection<? extends A> c) { 133 for (A a : c) { 134 add(a); 135 } 136 return false; 137 } 138 139 public boolean addAll(int index, Collection<? extends A> c) { 140 new Exception("not implemented").printStackTrace(); 141 return false; 142 } 143 144 public boolean contains(Object o) { 145 for (int i = size(); --i >= 0;) { 146 if (o.equals(get(i))) { 147 return true; 148 } 149 } 150 return false; 151 } 152 153 public boolean containsAll(Collection<?> c) { 154 for (Object o : c) { 155 if (!contains(o)) { 156 return false; 157 } 158 } 159 return true; 160 } 161 162 public int indexOf(Object o) { 163 for (int i = 0; i < size(); i++) { 164 if (o.equals(get(i))) { 165 return i; 166 } 167 } 168 return -1; 169 } 170 171 public boolean isEmpty() { 172 return size() == 0; 173 } 174 175 public Iterator<A> iterator() { 176 return new RingBufferIterator(); 177 } 178 179 private class RingBufferIterator implements Iterator<A> { 180 181 int pos = 0; 182 183 public RingBufferIterator() { 184 } 185 186 public boolean hasNext() { 187 return pos < size(); 188 } 189 190 public A next() { 191 A a = get(pos); 192 pos++; 193 return a; 194 } 195 196 public void remove() { 197 new Exception("not implemented").printStackTrace(); 198 } 199 200 } 201 202 public int lastIndexOf(Object o) { 203 for (int i = size(); --i >= 0;) { 204 if (o.equals(get(i))) { 205 return i; 206 } 207 } 208 return -1; 209 } 210 211 public ListIterator<A> listIterator() { 212 new Exception("not implemented").printStackTrace(); 213 return null; 214 } 215 216 public ListIterator<A> listIterator(int index) { 217 new Exception("not implemented").printStackTrace(); 218 return null; 219 } 220 221 public boolean remove(Object o) { 222 new Exception("not implemented").printStackTrace(); 223 return false; 224 } 225 226 public A remove(int index) { 227 new Exception("not implemented").printStackTrace(); 228 return null; 229 } 230 231 public boolean removeAll(Collection<?> c) { 232 new Exception("not implemented").printStackTrace(); 233 return false; 234 } 235 236 public boolean retainAll(Collection<?> c) { 237 new Exception("not implemented").printStackTrace(); 238 return false; 239 } 240 241 public List<A> subList(int fromIndex, int toIndex) { 242 new Exception("not implemented").printStackTrace(); 243 return null; 244 } 245 246 public Object[] toArray() { 247 new Exception("not implemented").printStackTrace(); 248 return null; 249 } 250 251 public <T> T[] toArray(T[] a) { 252 new Exception("not implemented").printStackTrace(); 253 return null; 254 } 255 256 }