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-2021 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   * @param <E> the type of content of this Set
18   */
19  public class ImmutableHashSet<E> extends ImmutableAbstractSet<E>
20  {
21      /** */
22      private static final long serialVersionUID = 20160507L;
23  
24      /**
25       * Construct a new ImmutableHashSet containing a copy of the provided collection.
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       * Construct a new ImmutableHashSet containing the provided set.
35       * @param set Set&lt;E&gt;; the set to use for the immutable set.
36       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
37       */
38      public ImmutableHashSet(final Set<E> set, final Immutable copyOrWrap)
39      {
40          super(copyOrWrap == Immutable.COPY ? new HashSet<E>(set) : set, copyOrWrap);
41      }
42  
43      /**
44       * Construct a new ImmutableHashSet containing a copy of the provided Collection.
45       * @param collection ImmutableAbstractCollection&lt;? extends E&gt;; the collection to use for the immutable set.
46       */
47      public ImmutableHashSet(final ImmutableAbstractCollection<? extends E> collection)
48      {
49          super(new HashSet<E>(collection.getUnderlyingCollection()), Immutable.COPY);
50      }
51  
52      /**
53       * Construct a new ImmutableHashSet containing the provided set.
54       * @param set ImmutableAbstractSet&lt;E&gt;; the set to use for the immutable set.
55       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
56       */
57      public ImmutableHashSet(final ImmutableAbstractSet<E> set, final Immutable copyOrWrap)
58      {
59          super(copyOrWrap == Immutable.COPY ? new HashSet<E>(set.getUnderlyingCollection()) : set.getUnderlyingCollection(),
60                  copyOrWrap);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      protected Set<E> getUnderlyingCollection()
66      {
67          return super.getUnderlyingCollection();
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public final Set<E> toSet()
73      {
74          return new HashSet<E>(getUnderlyingCollection());
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      @SuppressWarnings("checkstyle:designforextension")
80      public String toString()
81      {
82          Set<E> set = getUnderlyingCollection();
83          if (null == set)
84          {
85              return "ImmutableHashSet []";
86          }
87          return "ImmutableHashSet [" + set.toString() + "]";
88      }
89  
90  }