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-2019 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   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19   * @param <E> the type of content of this Set
20   */
21  public class ImmutableTreeSet<E> extends ImmutableAbstractSet<E> implements ImmutableNavigableSet<E>
22  {
23      /** */
24      private static final long serialVersionUID = 20160507L;
25  
26      /**
27       * @param sortedSet Collection&lt;? extends E&gt;; the collection to use for the immutable set.
28       */
29      public ImmutableTreeSet(final Collection<? extends E> sortedSet)
30      {
31          super(new TreeSet<E>(sortedSet), Immutable.COPY);
32      }
33  
34      /**
35       * @param treeSet NavigableSet&lt;E&gt;; the collection to use for the immutable set.
36       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
37       */
38      public ImmutableTreeSet(final NavigableSet<E> treeSet, final Immutable copyOrWrap)
39      {
40          super(copyOrWrap == Immutable.COPY ? new TreeSet<E>(treeSet) : treeSet, copyOrWrap);
41      }
42  
43      /**
44       * @param immutableSortedSet ImmutableAbstractSet&lt;E&gt;; the collection to use for the immutable set.
45       */
46      public ImmutableTreeSet(final ImmutableAbstractSet<E> immutableSortedSet)
47      {
48          super(new TreeSet<E>(immutableSortedSet.getCollection()), Immutable.COPY);
49      }
50  
51      /**
52       * @param immutableTreeSet ImmutableTreeSet&lt;E&gt;; the collection 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 collection
54       */
55      public ImmutableTreeSet(final ImmutableTreeSet<E> immutableTreeSet, final Immutable copyOrWrap)
56      {
57          super(copyOrWrap == Immutable.COPY ? new TreeSet<E>(immutableTreeSet.getCollection())
58                  : immutableTreeSet.getCollection(), copyOrWrap);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final NavigableSet<E> toSet()
64      {
65          return new TreeSet<E>(getCollection());
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      protected NavigableSet<E> getCollection()
71      {
72          return (NavigableSet<E>) super.getCollection();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final Comparator<? super E> comparator()
78      {
79          return getCollection().comparator();
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final ImmutableSortedSet<E> subSet(final E fromElement, final E toElement)
85      {
86          return new ImmutableTreeSet<E>(getCollection().subSet(fromElement, toElement));
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final ImmutableSortedSet<E> headSet(final E toElement)
92      {
93          return new ImmutableTreeSet<E>(getCollection().headSet(toElement));
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final ImmutableSortedSet<E> tailSet(final E fromElement)
99      {
100         return new ImmutableTreeSet<E>(getCollection().tailSet(fromElement));
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final E first()
106     {
107         return getCollection().first();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public final E last()
113     {
114         return getCollection().last();
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public final E lower(final E e)
120     {
121         return getCollection().lower(e);
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public final E floor(final E e)
127     {
128         return getCollection().floor(e);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final E ceiling(final E e)
134     {
135         return getCollection().ceiling(e);
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public final E higher(final E e)
141     {
142         return getCollection().higher(e);
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public final ImmutableNavigableSet<E> descendingSet()
148     {
149         return new ImmutableTreeSet<E>(getCollection().descendingSet());
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public final ImmutableIterator<E> descendingIterator()
155     {
156         return new ImmutableIterator<E>(getCollection().descendingIterator());
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final ImmutableNavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
162             final boolean toInclusive)
163     {
164         return new ImmutableTreeSet<E>(getCollection().subSet(fromElement, fromInclusive, toElement, toInclusive));
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public final ImmutableNavigableSet<E> headSet(final E toElement, final boolean inclusive)
170     {
171         return new ImmutableTreeSet<E>(getCollection().headSet(toElement, inclusive));
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public final ImmutableNavigableSet<E> tailSet(final E fromElement, final boolean inclusive)
177     {
178         return new ImmutableTreeSet<E>(getCollection().tailSet(fromElement, inclusive));
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public final String toString()
184     {
185         NavigableSet<E> set = getCollection();
186         if (null == set)
187         {
188             return "ImmutableTreeSet []";
189         }
190         return "ImmutableTreeSet [" + set.toString() + "]";
191     }
192 
193 }