Path: blob/master/test/jdk/java/util/Collection/testlibrary/ExtendsAbstractCollection.java
41153 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.util.AbstractCollection;23import java.util.ArrayList;24import java.util.Collection;25import java.util.Iterator;26import java.util.function.Supplier;2728/**29* @library30*31* A simple mutable collection implementation that provides only default32* implementations of all methods. ie. none of the Collection interface default33* methods have overridden implementations.34*35* @param <E> type of collection elements36*/37public class ExtendsAbstractCollection<E> extends AbstractCollection<E> {3839protected final Collection<E> coll;4041public ExtendsAbstractCollection() {42this(ArrayList<E>::new);43}4445public ExtendsAbstractCollection(Collection<E> source) {46this();47coll.addAll(source);48}4950protected ExtendsAbstractCollection(Supplier<Collection<E>> backer) {51this.coll = backer.get();52}5354public boolean add(E element) {55return coll.add(element);56}5758public boolean remove(Object element) {59return coll.remove(element);60}6162public Iterator<E> iterator() {63return new Iterator<E>() {64Iterator<E> source = coll.iterator();6566public boolean hasNext() {67return source.hasNext();68}6970public E next() {71return source.next();72}7374public void remove() {75source.remove();76}77};78}7980public int size() {81return coll.size();82}83}848586