1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.util.ArrayList;
  24 import java.util.AbstractList;
  25 import java.util.Collection;
  26 import java.util.Iterator;
  27 import java.util.List;
  28 import java.util.function.Supplier;
  29 
  30 /**
  31  * @library
  32  *
  33  * A simple mutable list implementation that provides only default
  34  * implementations of all methods. ie. none of the List interface default
  35  * methods have overridden implementations.
  36  *
  37  * @param <E> type of list elements
  38  */
  39 public class ExtendsAbstractList<E> extends AbstractList<E> {
  40 
  41     protected final List<E> list;
  42 
  43     public ExtendsAbstractList() {
  44         this(ArrayList<E>::new);
  45     }
  46 
  47     protected ExtendsAbstractList(Supplier<List<E>> supplier) {
  48         this.list = supplier.get();
  49     }
  50 
  51     public ExtendsAbstractList(Collection<E> source) {
  52         this();
  53         addAll(source);
  54     }
  55 
  56     public boolean add(E element) {
  57         return list.add(element);
  58     }
  59 
  60     public E get(int index) {
  61         return list.get(index);
  62     }
  63 
  64     public boolean remove(Object element) {
  65         return list.remove(element);
  66     }
  67 
  68     public E set(int index, E element) {
  69         return list.set(index, element);
  70     }
  71 
  72     public void add(int index, E element) {
  73         list.add(index, element);
  74     }
  75 
  76     public E remove(int index) {
  77         return list.remove(index);
  78     }
  79 
  80     public Iterator<E> iterator() {
  81         return new Iterator<E>() {
  82             Iterator<E> source = list.iterator();
  83 
  84             public boolean hasNext() {
  85                 return source.hasNext();
  86             }
  87 
  88             public E next() {
  89                 return source.next();
  90             }
  91 
  92             public void remove() {
  93                 source.remove();
  94             }
  95         };
  96     }
  97 
  98     public int size() {
  99         return list.size();
 100     }
 101 }