View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.LinkedHashSet;
4   import java.util.Set;
5   
6   /**
7    * A Set interface without the methods that can change it. The constructor of the ImmutableSet needs to be given an initial Set.
8    * <p>
9    * Copyright (c) 2016-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
11   * distributed under a three-clause BSD-style license, which can be found at
12   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
13   * </p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
16   * @param <E> the type of content of this Set
17   */
18  public interface ImmutableSet<E> extends ImmutableCollection<E>
19  {
20      /**
21       * Returns a modifiable copy of this immutable set.
22       * @return a modifiable copy of this immutable set.
23       */
24      Set<E> toSet();
25  
26      /**
27       * Force to redefine equals for the implementations of immutable collection classes.
28       * @param obj Object; the object to compare this collection with
29       * @return whether the objects are equal
30       */
31      @Override
32      boolean equals(Object obj);
33  
34      /**
35       * Force to redefine hashCode for the implementations of immutable collection classes.
36       * @return the calculated hashCode
37       */
38      @Override
39      int hashCode();
40  
41      /**
42       * Force to redefine toString.
43       * @return String; a description of this immutable set
44       */
45      @Override
46      String toString();
47  
48      /**
49       * Return an empty ImmutableSet, backed by a LinkedHashSet.
50       * @param <E> the value type
51       * @return ImmutableSet&lt;K, V&gt;; an empty ImmutableSet
52       */
53      static <E> ImmutableSet<E> of()
54      {
55          return new ImmutableLinkedHashSet<>(new LinkedHashSet<E>(), Immutable.WRAP);
56      }
57  
58      /**
59       * Return an ImmutableSet with 1 entry, backed by a LinkedHashSet.
60       * @param <E> the value type
61       * @param v1 E; value 1
62       * @return ImmutableSet&lt;K, V&gt;; an ImmutableSet with 1 entry, backed by a LinkedHashSet
63       */
64      static <E> ImmutableSet<E> of(final E v1)
65      {
66          LinkedHashSet<E> set = new LinkedHashSet<>();
67          set.add(v1);
68          return new ImmutableLinkedHashSet<>(set, Immutable.WRAP);
69      }
70  
71      /**
72       * Return an ImmutableSet with 2 entries, backed by a LinkedHashSet.
73       * @param <E> the value type
74       * @param v1 E; value 1
75       * @param v2 E; value 2
76       * @return ImmutableSet&lt;K, V&gt;; an ImmutableSet with 2 entries, backed by a LinkedHashSet
77       */
78      static <E> ImmutableSet<E> of(final E v1, final E v2)
79      {
80          LinkedHashSet<E> set = new LinkedHashSet<>();
81          set.add(v1);
82          set.add(v2);
83          return new ImmutableLinkedHashSet<>(set, Immutable.WRAP);
84      }
85  
86      /**
87       * Return an ImmutableSet with 3 entries, backed by a LinkedHashSet.
88       * @param <E> the value type
89       * @param v1 E; value 1
90       * @param v2 E; value 2
91       * @param v3 E; value 3
92       * @return ImmutableSet&lt;K, V&gt;; an ImmutableSet with 3 entries, backed by a LinkedHashSet
93       */
94      static <E> ImmutableSet<E> of(final E v1, final E v2, final E v3)
95      {
96          LinkedHashSet<E> set = new LinkedHashSet<>();
97          set.add(v1);
98          set.add(v2);
99          set.add(v3);
100         return new ImmutableLinkedHashSet<>(set, Immutable.WRAP);
101     }
102 
103     /**
104      * Return an ImmutableSet with 4 entries, backed by a LinkedHashSet.
105      * @param <E> the value type
106      * @param v1 E; value 1
107      * @param v2 E; value 2
108      * @param v3 E; value 3
109      * @param v4 E; value 4
110      * @return ImmutableSet&lt;K, V&gt;; an ImmutableSet with 4 entries, backed by a LinkedHashSet
111      */
112     static <E> ImmutableSet<E> of(final E v1, final E v2, final E v3, final E v4)
113     {
114         LinkedHashSet<E> set = new LinkedHashSet<>();
115         set.add(v1);
116         set.add(v2);
117         set.add(v3);
118         set.add(v4);
119         return new ImmutableLinkedHashSet<>(set, Immutable.WRAP);
120     }
121 
122     /**
123      * Return an ImmutableSet with 5 or more entries, backed by a LinkedHashSet.
124      * @param <E> the value type
125      * @param v1 E; value 1
126      * @param v2 E; value 2
127      * @param v3 E; value 3
128      * @param v4 E; value 4
129      * @param v5 E; value 5
130      * @param vn E...; values 6 and beyond
131      * @return ImmutableSet&lt;K, V&gt;; an ImmutableSet with 5 or more entries, backed by a LinkedHashSet
132      */
133     @SuppressWarnings("unchecked")
134     static <E> ImmutableSet<E> of(final E v1, final E v2, final E v3, final E v4, final E v5, final E... vn)
135     {
136         LinkedHashSet<E> set = new LinkedHashSet<>();
137         set.add(v1);
138         set.add(v2);
139         set.add(v3);
140         set.add(v4);
141         set.add(v5);
142         for (E v : vn)
143         {
144             set.add(v);
145         }
146         return new ImmutableLinkedHashSet<>(set, Immutable.WRAP);
147     }
148 }