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.HashSet;
  24 import java.util.AbstractSet;
  25 import java.util.Collection;
  26 import java.util.Iterator;
  27 import java.util.Set;
  28 import java.util.function.Supplier;
  29 
  30 /**
  31  * @library
  32  *
  33  * A simple mutable set implementation that provides only default
  34  * implementations of all methods. ie. none of the Set interface default methods
  35  * have overridden implementations.
  36  *
  37  * @param <E> type of set members
  38  */
  39 public class ExtendsAbstractSet<S extends Set<E>, E> extends AbstractSet<E> {
  40 
  41     protected final S set;
  42 
  43     public ExtendsAbstractSet() {
  44         this(() -> { return (S) new HashSet<E>();});
  45     }
  46 
  47     public ExtendsAbstractSet(Collection<E> source) {
  48         this();
  49         addAll(source);
  50     }
  51 
  52     protected ExtendsAbstractSet(Supplier<S> backer) {
  53         this.set = backer.get();
  54     }
  55 
  56     public boolean add(E element) {
  57         return set.add(element);
  58     }
  59 
  60     public boolean remove(Object element) {
  61         return set.remove(element);
  62     }
  63 
  64     public Iterator<E> iterator() {
  65         return new Iterator<E>() {
  66             Iterator<E> source = set.iterator();
  67 
  68             public boolean hasNext() {
  69                 return source.hasNext();
  70             }
  71 
  72             public E next() {
  73                 return source.next();
  74             }
  75 
  76             public void remove() {
  77                 source.remove();
  78             }
  79         };
  80     }
  81 
  82     public int size() {
  83         return set.size();
  84     }
  85 }