Created
September 20, 2013 22:23
-
-
Save djeikyb/6644715 to your computer and use it in GitHub Desktop.
Dumb wrapper to satisfy jsf 1.2 repeats, eg h:datatable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package gps.absenceCalc2.web.ui; | |
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import java.util.ListIterator; | |
| import java.util.SortedSet; | |
| import java.util.TreeSet; | |
| /** | |
| * This class is purely to allow jsf stuff like {@code h:dataTable} and | |
| * {@code ui:repeat} to work with an ordered set. This class behaves like a | |
| * {@link SortedSet} wherever the {@code SortedSet} and {@link List} contracts conflict. | |
| * Notably, the {@code List} contract is all about insertion order; this | |
| * class is backed by a {@link SortedSet}. | |
| * <p> | |
| * Rumour has it jsf 2.2 finally supports java's collections. | |
| * <p> | |
| * Since this is a {@code SortedSet} disguised as a {@code List}, these optional operations don't make sense and aren't supported: | |
| * <ul> | |
| * <li> {@link #addAll(int, Collection)} | |
| * <li> {@link #set(int, Object)} | |
| * <li> {@link #add(int, Object)} | |
| * </ul> | |
| */ | |
| public class SortedSetList<T> extends TreeSet<T> implements SortedSet<T>, List<T> | |
| { | |
| // TODO write unit tests. perfect candidate for unit tests | |
| /** generated by eclipse */ | |
| private static final long serialVersionUID = 1435910346784401396L; | |
| /** | |
| * @throws UnsupportedOperationException | |
| */ | |
| @Override | |
| public boolean addAll(int index, Collection<? extends T> c) | |
| { | |
| throw new UnsupportedOperationException(); | |
| } | |
| @Override | |
| public T get(int index) | |
| { | |
| Iterator<T> iter = this.iterator(); | |
| for (int i = 0; i < index; i += 1) | |
| { | |
| iter.next(); | |
| } | |
| return iter.next(); | |
| } | |
| /** | |
| * @throws UnsupportedOperationException | |
| */ | |
| @Override | |
| public T set(int index, T element) | |
| { | |
| throw new UnsupportedOperationException(); | |
| } | |
| /** | |
| * @throws UnsupportedOperationException | |
| */ | |
| @Override | |
| public void add(int index, T element) | |
| { | |
| throw new UnsupportedOperationException(); | |
| } | |
| /** | |
| * {@inheritDoc} | |
| * @throws IndexOutOfBoundsException {@inheritDoc} | |
| */ | |
| @Override | |
| public T remove(int index) | |
| { | |
| if (index < 0 || index >= this.size()) | |
| { | |
| throw new IndexOutOfBoundsException("index must be > 0 and >= size()"); | |
| } | |
| // for loop here is safe because logically we throw if this.isEmpty() | |
| Iterator<T> iter = this.iterator(); | |
| for (T result = iter.next(); iter.hasNext(); index -= 1) | |
| { | |
| if (index == 0) | |
| { | |
| this.remove(result); | |
| return result; | |
| } | |
| } | |
| return null; // never reached | |
| } | |
| @Override | |
| public int indexOf(Object o) | |
| { | |
| return indexOf(o, this.iterator()); | |
| } | |
| @Override | |
| public int lastIndexOf(Object o) | |
| { | |
| return indexOf(o, this.descendingSet().iterator()); | |
| } | |
| private int indexOf(Object o, Iterator<T> iter) | |
| { | |
| if (o == null) | |
| { | |
| for (int i = 0; iter.hasNext(); i += 1) | |
| { | |
| if (iter.next() == null) | |
| { | |
| return i; | |
| } | |
| } | |
| } | |
| else | |
| { | |
| for (int i = 0; iter.hasNext(); i += 1) | |
| { | |
| if (iter.next().equals(o)) | |
| { | |
| return i; | |
| } | |
| } | |
| } | |
| return -1; | |
| } | |
| @Override | |
| public ListIterator<T> listIterator() | |
| { | |
| return new ArrayList<T>(this).listIterator(); | |
| } | |
| @Override | |
| public ListIterator<T> listIterator(int index) | |
| { | |
| return new ArrayList<T>(this).listIterator(index); | |
| } | |
| @Override | |
| public List<T> subList(int fromIndex, int toIndex) | |
| { | |
| List<T> result = new ArrayList<T>(); | |
| Iterator<T> iter = this.iterator(); | |
| int i = 0; | |
| while (iter.hasNext()) | |
| { | |
| T e = iter.next(); | |
| if (i >= fromIndex && i < toIndex) | |
| { | |
| result.add(e); | |
| if (i == (toIndex - 1)) | |
| { | |
| break; // no sense iterating past toIndex | |
| } | |
| } | |
| i += 1; | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment