~ indentation or something

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-10-10 10:03:00 +02:00
parent 2fb72180f9
commit 442ffc16bb
1 changed files with 93 additions and 93 deletions

View File

@ -5,116 +5,116 @@ import org.jetbrains.annotations.NotNull;
import java.util.Iterator; import java.util.Iterator;
public class RingList<E> implements List<E> { public class RingList<E> implements List<E> {
private class Wrapper { private class Wrapper {
E e; E e;
Wrapper succ; Wrapper succ;
Wrapper(E e) { Wrapper(E e) {
this.e = e; this.e = e;
succ = null; succ = null;
} }
} }
private Wrapper first; private Wrapper first;
private Wrapper last; private Wrapper last;
private int size; private int size;
public RingList() { public RingList() {
first = new Wrapper(null); first = new Wrapper(null);
last = first; last = first;
last.succ = first; last.succ = first;
size = 0; size = 0;
} }
public boolean add(E e) { public boolean add(E e) {
Wrapper w = new Wrapper(e); Wrapper w = new Wrapper(e);
last.succ = w; last.succ = w;
last = w; last = w;
last.succ = first; last.succ = first;
size++; size++;
return true; return true;
} }
private Wrapper getWrapper(int index) { private Wrapper getWrapper(int index) {
if(index == -1) return first; if (index == -1) return first;
Wrapper wRun; Wrapper wRun;
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
wRun = this.first.succ; wRun = this.first.succ;
for (int iRun = 0; iRun < index; iRun++) { for (int iRun = 0; iRun < index; iRun++) {
wRun = wRun.succ; wRun = wRun.succ;
} }
return wRun; return wRun;
} }
public E get(int index) { public E get(int index) {
return getWrapper(index).e; return getWrapper(index).e;
} }
public int size() { public int size() {
return size; return size;
} }
@Override @Override
public E remove(int index) { public E remove(int index) {
Wrapper wrapper = getWrapper(index - 1); Wrapper wrapper = getWrapper(index - 1);
E result = wrapper.succ.e; E result = wrapper.succ.e;
wrapper.succ = wrapper.succ.succ; wrapper.succ = wrapper.succ.succ;
size--; size--;
return result; return result;
} }
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
try { try {
int index = indexOf((E) o); int index = indexOf((E) o);
if(index == -1) return false; if (index == -1) return false;
remove(index); remove(index);
return true; return true;
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
return false; return false;
} }
@Override @Override
public int indexOf(E e) { public int indexOf(E e) {
Wrapper wrapper = this.first.succ; Wrapper wrapper = this.first.succ;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if(wrapper.e.equals(e)) { if (wrapper.e.equals(e)) {
return i; return i;
}else wrapper = wrapper.succ; } else wrapper = wrapper.succ;
} }
return -1; return -1;
} }
@NotNull @NotNull
public Iterator<E> iterator() { public Iterator<E> iterator() {
return new Iterator<E>() { return new Iterator<E>() {
Wrapper next = first.succ; Wrapper next = first.succ;
Wrapper current; Wrapper current;
public boolean hasNext() { public boolean hasNext() {
return next != first; return next != first;
} }
public E next() { public E next() {
current = next; current = next;
if (next != first) { if (next != first) {
next = next.succ; next = next.succ;
} }
return current.e; return current.e;
} }
public void remove() { public void remove() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}; };
} }
} }