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