exo6: Code cleanup.
[Project_POO.git] / exo2 / ListExtension.java
index bba85d2fb1c60307cdf0d25333d18f6de501fb5d..df2c789ccfca45e3d89721e8a09a406b3e2931f5 100644 (file)
@@ -1,7 +1,8 @@
 import java.util.List;
 import java.util.ListIterator;
+import java.util.AbstractList;
 
-public class ListExtension<E> {
+public class ListExtension<E> extends AbstractList<E> {
     private List<E> list;
 
     public ListExtension() {
@@ -28,37 +29,53 @@ public class ListExtension<E> {
         return list;
     }
 
+    /**
+     * [get description]
+     * @param  index [description]
+     * @return       [description]
+     */
+    public E get(int index) {
+        return list.get(index);
+    }
+
+    /**
+     * [size description]
+     * @return [description]
+     */
+    public int size() {
+        return list.size();
+    }
+
     public boolean add(E e) {
         return list.add(e);
     }
 
     /**
-     * Should mimic the List<E> add(int index, T value) method
-     * @param  value [description]
-     * @return       [description]
+     * Should mimic the List<E> add(int index, E value) method
+     * @param index [description]
+     * @param value [description]
+     * @return      [description]
      */
-    public void addMiddleIter(E value) {
-        int mid = list.size() / 2;
+    public void add(int index, E value) {
         ListIterator<E> iter = list.listIterator();
         int i = 0;
-        // go to the element at mid index
-        while (iter.hasNext() && i < mid) {
+        // go to the element at index + 1
+        while (iter.hasNext() && i <= index) {
             iter.next();
             i++;
         }
-        iter.next(); // Insert before mid + 1
         iter.add(value);
     }
 
-    public void addNEMiddle(int Nelements) {
+    //FIXME: replace parameter by the list of objects to add
+    public void addNelements(int index, int Nelements) {
         ListIterator<E> iter = list.listIterator();
         int i = 0;
-        // go to the element at the middle index
-        while (iter.hasNext() && i < list.size() / 2) {
+        // go to the element at index + 1
+        while (iter.hasNext() && i <= index) {
             iter.next();
             i++;
         }
-        iter.next(); // Insert before mid + 1
         for (int j = 0; j < Nelements; j++) {
             iter.add((E)new Object());
         }