View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.Comparator;
5   import java.util.NavigableSet;
6   import java.util.TreeSet;
7   
8   /**
9    * An immutable wrapper for a TreeSet.
10   * <p>
11   * Copyright (c) 2016-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
13   * distributed under a three-clause BSD-style license, which can be found at
14   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
15   * </p>
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
18   * @param <E> the type of content of this Set
19   */
20  public class ImmutableTreeSet<E> extends ImmutableAbstractSet<E> implements ImmutableNavigableSet<E>
21  {
22      /** */
23      private static final long serialVersionUID = 20160507L;
24  
25      /**
26       * @param sortedSet Collection&lt;? extends E&gt;; the collection to use for the immutable set.
27       */
28      public ImmutableTreeSet(final Collection<? extends E> sortedSet)
29      {
30          super(new TreeSet<E>(sortedSet), Immutable.COPY);
31      }
32  
33      /**
34       * @param treeSet NavigableSet&lt;E&gt;; the collection 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 collection
36       */
37      public ImmutableTreeSet(final NavigableSet<E> treeSet, final Immutable copyOrWrap)
38      {
39          super(copyOrWrap == Immutable.COPY ? new TreeSet<E>(treeSet) : treeSet, copyOrWrap);
40      }
41  
42      /**
43       * @param immutableSortedSet ImmutableAbstractSet&lt;E&gt;; the collection to use for the immutable set.
44       */
45      public ImmutableTreeSet(final ImmutableAbstractSet<E> immutableSortedSet)
46      {
47          super(new TreeSet<E>(immutableSortedSet.getUnderlyingCollection()), Immutable.COPY);
48      }
49  
50      /**
51       * @param immutableTreeSet ImmutableTreeSet&lt;E&gt;; the collection to use for the immutable set.
52       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
53       */
54      public ImmutableTreeSet(final ImmutableTreeSet<E> immutableTreeSet, final Immutable copyOrWrap)
55      {
56          super(copyOrWrap == Immutable.COPY ? new TreeSet<E>(immutableTreeSet.getUnderlyingCollection())
57                  : immutableTreeSet.getUnderlyingCollection(), copyOrWrap);
58      }
59  
60      @Override
61      public final NavigableSet<E> toSet()
62      {
63          return new TreeSet<E>(getUnderlyingCollection());
64      }
65  
66      @Override
67      protected NavigableSet<E> getUnderlyingCollection()
68      {
69          return (NavigableSet<E>) super.getUnderlyingCollection();
70      }
71  
72      @Override
73      public final Comparator<? super E> comparator()
74      {
75          return getUnderlyingCollection().comparator();
76      }
77  
78      @Override
79      public final ImmutableSortedSet<E> subSet(final E fromElement, final E toElement)
80      {
81          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().subSet(fromElement, toElement), Immutable.WRAP);
82      }
83  
84      @Override
85      public final ImmutableSortedSet<E> headSet(final E toElement)
86      {
87          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().headSet(toElement), Immutable.WRAP);
88      }
89  
90      @Override
91      public final ImmutableSortedSet<E> tailSet(final E fromElement)
92      {
93          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().tailSet(fromElement), Immutable.WRAP);
94      }
95  
96      @Override
97      public final E first()
98      {
99          return getUnderlyingCollection().first();
100     }
101 
102     @Override
103     public final E last()
104     {
105         return getUnderlyingCollection().last();
106     }
107 
108     @Override
109     public final E lower(final E e)
110     {
111         return getUnderlyingCollection().lower(e);
112     }
113 
114     @Override
115     public final E floor(final E e)
116     {
117         return getUnderlyingCollection().floor(e);
118     }
119 
120     @Override
121     public final E ceiling(final E e)
122     {
123         return getUnderlyingCollection().ceiling(e);
124     }
125 
126     @Override
127     public final E higher(final E e)
128     {
129         return getUnderlyingCollection().higher(e);
130     }
131 
132     @Override
133     public final ImmutableNavigableSet<E> descendingSet()
134     {
135         return new ImmutableTreeSet<E>(getUnderlyingCollection().descendingSet());
136     }
137 
138     @Override
139     public final ImmutableIterator<E> descendingIterator()
140     {
141         return new ImmutableIterator<E>(getUnderlyingCollection().descendingIterator());
142     }
143 
144     @Override
145     public final ImmutableNavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
146             final boolean toInclusive)
147     {
148         return new ImmutableTreeSet<E>(getUnderlyingCollection().subSet(fromElement, fromInclusive, toElement, toInclusive),
149                 Immutable.WRAP);
150     }
151 
152     @Override
153     public final ImmutableNavigableSet<E> headSet(final E toElement, final boolean inclusive)
154     {
155         return new ImmutableTreeSet<E>(getUnderlyingCollection().headSet(toElement, inclusive), Immutable.WRAP);
156     }
157 
158     @Override
159     public final ImmutableNavigableSet<E> tailSet(final E fromElement, final boolean inclusive)
160     {
161         return new ImmutableTreeSet<E>(getUnderlyingCollection().tailSet(fromElement, inclusive), Immutable.WRAP);
162     }
163 
164     @Override
165     public final String toString()
166     {
167         NavigableSet<E> set = getUnderlyingCollection();
168         if (null == set)
169         {
170             return "ImmutableTreeSet []";
171         }
172         return "ImmutableTreeSet [" + set.toString() + "]";
173     }
174 
175 }