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="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17 * @author <a href="http://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<? extends E>; 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<E>; 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
36 * 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 * @param collection ImmutableAbstractCollection<? extends E>; the collection to use for the immutable set.
45 */
46 public ImmutableHashSet(final ImmutableAbstractCollection<? extends E> collection)
47 {
48 super(new HashSet<E>(collection.getCollection()), Immutable.COPY);
49 }
50
51 /**
52 * @param set ImmutableAbstractSet<E>; the set to use for the immutable set.
53 * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original
54 * collection
55 */
56 public ImmutableHashSet(final ImmutableAbstractSet<E> set, final Immutable copyOrWrap)
57 {
58 super(copyOrWrap == Immutable.COPY ? new HashSet<E>(set.getCollection()) : set.getCollection(), copyOrWrap);
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 protected Set<E> getCollection()
64 {
65 return super.getCollection();
66 }
67
68 /** {@inheritDoc} */
69 @Override
70 public final Set<E> toSet()
71 {
72 return new HashSet<E>(getCollection());
73 }
74
75 /** {@inheritDoc} */
76 @Override
77 @SuppressWarnings("checkstyle:designforextension")
78 public String toString()
79 {
80 Set<E> set = getCollection();
81 if (null == set)
82 {
83 return "ImmutableHashSet []";
84 }
85 return "ImmutableHashSet [" + set.toString() + "]";
86 }
87
88 }