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