| 1 | import java.util.List; |
| 2 | import java.util.ListIterator; |
| 3 | |
| 4 | public class ListExtension<E> { |
| 5 | private List<E> list; |
| 6 | |
| 7 | public ListExtension() { |
| 8 | setList(null); |
| 9 | } |
| 10 | |
| 11 | public ListExtension(List<E> l) { |
| 12 | setList(l); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * [setList description] |
| 17 | * @param l [description] |
| 18 | */ |
| 19 | public void setList(List<E> l) { |
| 20 | list = l; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * [getList description] |
| 25 | * @return [description] |
| 26 | */ |
| 27 | public List<E> getList() { |
| 28 | return list; |
| 29 | } |
| 30 | |
| 31 | public boolean add(E e) { |
| 32 | return list.add(e); |
| 33 | } |
| 34 | |
| 35 | public int size() { |
| 36 | return list.size(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Should mimic the List<E> add(int index, T value) method |
| 41 | * @param index [description] |
| 42 | * @param value [description] |
| 43 | * @return [description] |
| 44 | */ |
| 45 | public void addIter(int index, E value) { |
| 46 | ListIterator<E> iter = list.listIterator(); |
| 47 | int i = 0; |
| 48 | // go to the element at index + 1 |
| 49 | while (iter.hasNext() && i <= index) { |
| 50 | iter.next(); |
| 51 | i++; |
| 52 | } |
| 53 | iter.add(value); |
| 54 | } |
| 55 | |
| 56 | //FIXME: replace parameter by the list of objects to add |
| 57 | public void addNelements(int index, int Nelements) { |
| 58 | ListIterator<E> iter = list.listIterator(); |
| 59 | int i = 0; |
| 60 | // go to the element at index + 1 |
| 61 | while (iter.hasNext() && i <= index) { |
| 62 | iter.next(); |
| 63 | i++; |
| 64 | } |
| 65 | for (int j = 0; j < Nelements; j++) { |
| 66 | iter.add((E)new Object()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } |