Skip to content

Instantly share code, notes, and snippets.

@Felk
Created August 29, 2017 12:49
Show Gist options
  • Save Felk/185fec05c0d081908ec668a3fb208723 to your computer and use it in GitHub Desktop.
Save Felk/185fec05c0d081908ec668a3fb208723 to your computer and use it in GitHub Desktop.
package de.soptim.egon;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
public class LinkedHashSetList<T> extends LinkedHashSet<T> implements List<T> {
@Override
public T get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
}
return stream().skip(index).findFirst().get();
}
@Override
public T remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
}
final Iterator<T> iter = this.iterator();
for (int i = 0; i < index; i++) {
iter.next();
}
T item = iter.next();
iter.remove();
return item;
}
@Override
public int indexOf(Object o) {
int index = 0;
for (T item : this) {
if (item.equals(o)) {
return index;
}
index++;
}
return -1;
}
@Override
public int lastIndexOf(Object o) {
return indexOf(o);
}
@Override
public ListIterator<T> listIterator() {
return listIterator(0);
}
@Override
public ListIterator<T> listIterator(int index) {
// trivial when using LinkedList's iterator with the optional modification operations left unsupported.
// alternatively some more bookkeeping logic to support modifications.
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
// trivial when using a list with the optional modification operations left unsupported.
// alternatively some more bookkeeping logic to support modifications.
}
/////////////////////////
// optional operations //
/////////////////////////
@Override
public T set(int index, T element) {
// unclear semantics, better left unsupported. Alternatively possible with some bookkeeping
throw new UnsupportedOperationException();
}
@Override
public void add(int index, T element) {
// unclear semantics, better left unsupported. Alternatively possible with some bookkeeping
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
// unclear semantics, better left unsupported. Alternatively possible with some bookkeeping
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment