Skip to content

Instantly share code, notes, and snippets.

@mammothbane
Last active December 12, 2015 05:48
Show Gist options
  • Save mammothbane/781181162db2e9924cbc to your computer and use it in GitHub Desktop.
Save mammothbane/781181162db2e9924cbc to your computer and use it in GitHub Desktop.
successful List<E> implementation in half an hour
import java.util.*;
public class MyList<E> implements List<E> {
public boolean add(E e) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public boolean contains(Object o) {
return false;
}
public boolean containsAll(Collection<?> c) {
return c.size() == 0;
}
public boolean equals(Object o) {
return o instanceof MyList;
}
public E get(int index) {
return null;
}
public int hashCode() {
return 0;
}
public int indexOf(Object o) {
return -1;
}
public boolean isEmpty() {
return true;
}
public Iterator<E> iterator() {
return this.listIterator();
}
public int lastIndexOf(Object o) {
return -1;
}
public ListIterator<E> listIterator() {
return new MyIterator();
}
public ListIterator<E> listIterator(int index) {
return new MyIterator();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public int size() {
return 0;
}
public List<E> subList(int fromIndex, int toIndex) {
return this;
}
public Object[] toArray() {
return new Object[0];
}
public <T> T[] toArray(T[] a) {
if (a.length != 0) {
a[0] = null;
}
return a;
}
private class MyIterator implements ListIterator<E> {
public void add(E item) {
throw new UnsupportedOperationException();
}
public void remove() {
throw new UnsupportedOperationException();
}
public void set(E e) {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
return false;
}
public boolean hasPrevious() {
return false;
}
public E next() {
throw new NoSuchElementException();
}
public E previous() {
throw new NoSuchElementException();
}
public int nextIndex() {
return 0;
}
public int previousIndex() {
return -1;
}
}
}
@yuriybabenko
Copy link

Hah. And here we demonstrate the importance of proper requirements gathering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment