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