Skip to content

Instantly share code, notes, and snippets.

@juliensimon
Last active August 29, 2015 14:17
Show Gist options
  • Save juliensimon/f9e6bcd88784c670dd8a to your computer and use it in GitHub Desktop.
Save juliensimon/f9e6bcd88784c670dd8a to your computer and use it in GitHub Desktop.
public interface LinearSearchMode<T> {
public void moveElement(List<T> list, int index);
}
class LinearSearchModeMoveFirst<T> implements LinearSearchMode<T> {
@Override
public void moveElement(List<T> list, int index) {
// Move element to the front of the list
T t = list.remove(index);
list.add(0, t);
}
}
class LinearSearchModeMoveLast<T> implements LinearSearchMode<T> {
@Override
public void moveElement(List<T> list, int index) {
// Move element to the end of the list
T t = list.remove(index);
list.add(t);
}
}
class LinearSearchModeMoveUp<T> implements LinearSearchMode<T> {
@Override
public void moveElement(List<T> list, int index) {
// Move element up, unless it's already at the front of the list
if (index != 0) {
Collections.swap(list, index-1, index);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment