View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.HashSet;
5   import java.util.Set;
6   
7   /**
8    * An immutable wrapper for a HashSet.
9    * <p>
10   * Copyright (c) 2016-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
12   * distributed under a three-clause BSD-style license, which can be found at
13   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
17   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   * @param <E> the type of content of this Set
19   */
20  public class ImmutableHashSet<E> extends ImmutableAbstractSet<E>
21  {
22      /** */
23      private static final long serialVersionUID = 20160507L;
24  
25      /**
26       * @param collection Collection&lt;? extends E&gt;; the collection to use for the immutable set.
27       */
28      public ImmutableHashSet(final Collection<? extends E> collection)
29      {
30          super(new HashSet<E>(collection), Immutable.COPY);
31      }
32  
33      /**
34       * @param set Set&lt;E&gt;; the set to use for the immutable set.
35       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
36       */
37      public ImmutableHashSet(final Set<E> set, final Immutable copyOrWrap)
38      {
39          super(copyOrWrap == Immutable.COPY ? new HashSet<E>(set) : set, copyOrWrap);
40      }
41  
42      /**
43       * @param collection ImmutableAbstractCollection&lt;? extends E&gt;; the collection to use for the immutable set.
44       */
45      public ImmutableHashSet(final ImmutableAbstractCollection<? extends E> collection)
46      {
47          super(new HashSet<E>(collection.getCollection()), Immutable.COPY);
48      }
49  
50      /**
51       * @param set ImmutableAbstractSet&lt;E&gt;; the set to use for the immutable set.
52       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
53       */
54      public ImmutableHashSet(final ImmutableAbstractSet<E> set, final Immutable copyOrWrap)
55      {
56          super(copyOrWrap == Immutable.COPY ? new HashSet<E>(set.getCollection()) : set.getCollection(), copyOrWrap);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      protected Set<E> getCollection()
62      {
63          return super.getCollection();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final Set<E> toSet()
69      {
70          return new HashSet<E>(getCollection());
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      @SuppressWarnings("checkstyle:designforextension")
76      public String toString()
77      {
78          Set<E> set = getCollection();
79          if (null == set)
80          {
81              return "ImmutableHashSet []";
82          }
83          return "ImmutableHashSet [" + set.toString() + "]";
84      }
85  
86  }