1 package org.djutils.immutablecollections;
2
3 import java.util.Collection;
4 import java.util.LinkedHashSet;
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 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck
16 * $, initial version May 7, 2016 <br>
17 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20 * @param <E> the type of content of this Set
21 */
22 public class ImmutableLinkedHashSet<E> extends ImmutableAbstractSet<E>
23 {
24 /** */
25 private static final long serialVersionUID = 20160507L;
26
27 /**
28 * @param collection Collection<? extends E>; the collection to use for the immutable set.
29 */
30 public ImmutableLinkedHashSet(final Collection<? extends E> collection)
31 {
32 super(new LinkedHashSet<E>(collection), Immutable.COPY);
33 }
34
35 /**
36 * @param collection Set<E>; the collection 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
38 * collection
39 */
40 public ImmutableLinkedHashSet(final Set<E> collection, final Immutable copyOrWrap)
41 {
42 super(copyOrWrap == Immutable.COPY ? new LinkedHashSet<E>(collection) : collection, copyOrWrap);
43 }
44
45 /**
46 * @param collection ImmutableAbstractCollection<? extends E>; the collection to use for the immutable set.
47 */
48 public ImmutableLinkedHashSet(final ImmutableAbstractCollection<? extends E> collection)
49 {
50 super(new LinkedHashSet<E>(collection.getCollection()), Immutable.COPY);
51 }
52
53 /**
54 * @param set ImmutableAbstractSet<E>; the collection 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
56 * collection
57 */
58 public ImmutableLinkedHashSet(final ImmutableAbstractSet<E> set, final Immutable copyOrWrap)
59 {
60 super(copyOrWrap == Immutable.COPY ? new LinkedHashSet<E>(set.getCollection()) : set.getCollection(),
61 copyOrWrap);
62 }
63
64 /** {@inheritDoc} */
65 @Override
66 protected Set<E> getCollection()
67 {
68 return super.getCollection();
69 }
70
71 /** {@inheritDoc} */
72 @Override
73 public final Set<E> toSet()
74 {
75 return new LinkedHashSet<E>(getCollection());
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public final String toString()
81 {
82 Set<E> set = getCollection();
83 if (null == set)
84 {
85 return "ImmutableLinkedHashSet []";
86 }
87 return "ImmutableLinkedHashSet [" + set.toString() + "]";
88 }
89
90 }