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-2022 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      /** {@inheritDoc} */
61      @Override
62      public final NavigableSet<E> toSet()
63      {
64          return new TreeSet<E>(getUnderlyingCollection());
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      protected NavigableSet<E> getUnderlyingCollection()
70      {
71          return (NavigableSet<E>) super.getUnderlyingCollection();
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public final Comparator<? super E> comparator()
77      {
78          return getUnderlyingCollection().comparator();
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public final ImmutableSortedSet<E> subSet(final E fromElement, final E toElement)
84      {
85          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().subSet(fromElement, toElement), Immutable.WRAP);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public final ImmutableSortedSet<E> headSet(final E toElement)
91      {
92          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().headSet(toElement), Immutable.WRAP);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public final ImmutableSortedSet<E> tailSet(final E fromElement)
98      {
99          return new ImmutableTreeSet<E>((TreeSet<E>) getUnderlyingCollection().tailSet(fromElement), Immutable.WRAP);
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final E first()
105     {
106         return getUnderlyingCollection().first();
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public final E last()
112     {
113         return getUnderlyingCollection().last();
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public final E lower(final E e)
119     {
120         return getUnderlyingCollection().lower(e);
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final E floor(final E e)
126     {
127         return getUnderlyingCollection().floor(e);
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public final E ceiling(final E e)
133     {
134         return getUnderlyingCollection().ceiling(e);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public final E higher(final E e)
140     {
141         return getUnderlyingCollection().higher(e);
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public final ImmutableNavigableSet<E> descendingSet()
147     {
148         return new ImmutableTreeSet<E>(getUnderlyingCollection().descendingSet());
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final ImmutableIterator<E> descendingIterator()
154     {
155         return new ImmutableIterator<E>(getUnderlyingCollection().descendingIterator());
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public final ImmutableNavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
161             final boolean toInclusive)
162     {
163         return new ImmutableTreeSet<E>(getUnderlyingCollection().subSet(fromElement, fromInclusive, toElement, toInclusive),
164                 Immutable.WRAP);
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public final ImmutableNavigableSet<E> headSet(final E toElement, final boolean inclusive)
170     {
171         return new ImmutableTreeSet<E>(getUnderlyingCollection().headSet(toElement, inclusive), Immutable.WRAP);
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public final ImmutableNavigableSet<E> tailSet(final E fromElement, final boolean inclusive)
177     {
178         return new ImmutableTreeSet<E>(getUnderlyingCollection().tailSet(fromElement, inclusive), Immutable.WRAP);
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public final String toString()
184     {
185         NavigableSet<E> set = getUnderlyingCollection();
186         if (null == set)
187         {
188             return "ImmutableTreeSet []";
189         }
190         return "ImmutableTreeSet [" + set.toString() + "]";
191     }
192 
193 }