View Javadoc

1   /*
2    * $Id: TernarySearchTreeStringSet.java 211 2010-11-22 19:21:26Z roland $
3    * Copyright (C) 2007 Roland Krueger
4    * Created on 24.06.2007
5    *
6    * Author: Roland Krueger (www.rolandkrueger.info)
7    *
8    * This file is part of RoKlib.
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public License
12   * as published by the Free Software Foundation; either version 2.1 of
13   * the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23   * USA
24   */
25  package info.rolandkrueger.roklib.util;
26  
27  import java.util.Collection;
28  import java.util.Comparator;
29  import java.util.Iterator;
30  import java.util.Set;
31  import java.util.SortedSet;
32  
33  public class TernarySearchTreeStringSet implements SortedSet<CharSequence>
34  {
35    private static Object MARKER = new Object ();
36  
37    private TernarySearchTreeMap<Object> mData;
38  
39    public TernarySearchTreeStringSet ()
40    {
41      mData = new TernarySearchTreeMap<Object> ();
42    }
43  
44    public TernarySearchTreeStringSet (CharSequence[] values)
45    {
46      this ();
47      for (CharSequence element : values)
48      {
49        add (element);
50      }
51    }
52  
53    public TernarySearchTreeStringSet (Set<CharSequence> values)
54    {
55      this ();
56      for (CharSequence element : values)
57      {
58        add (element);
59      }
60    }
61  
62    public Comparator<? super CharSequence> comparator ()
63    {
64      return mData.comparator ();
65    }
66  
67    public CharSequence first ()
68    {
69      return mData.firstKey ();
70    }
71  
72    public SortedSet<CharSequence> headSet (CharSequence toElement)
73    {
74      // TODO Auto-generated method stub
75      return null;
76    }
77  
78    public CharSequence last ()
79    {
80      return mData.lastKey ();
81    }
82  
83    public SortedSet<CharSequence> subSet (CharSequence fromElement, CharSequence toElement)
84    {
85      // TODO Auto-generated method stub
86      return null;
87    }
88  
89    public SortedSet<CharSequence> tailSet (CharSequence fromElement)
90    {
91      // TODO Auto-generated method stub
92      return null;
93    }
94  
95    public boolean add (CharSequence key)
96    {
97      return mData.put (key, MARKER) == null;
98    }
99  
100   public boolean addAll (Collection<? extends CharSequence> collection)
101   {
102     boolean changed = false;
103 
104     for (CharSequence element : collection)
105     {
106       if (! mData.containsKey (element))
107       {
108         add (element);
109         changed = true;
110       }
111     }
112     return changed;
113   }
114 
115   public void clear ()
116   {
117     mData.clear ();
118   }
119 
120   public boolean contains (Object o)
121   {
122     return mData.containsKey (o);
123   }
124 
125   public boolean containsAll (Collection<?> collection)
126   {
127     for (Object o : collection)
128     {
129       if (! mData.containsKey (o)) return false;
130     }
131     return true;
132   }
133 
134   public boolean isEmpty ()
135   {
136     return mData.isEmpty ();
137   }
138 
139   public Iterator<CharSequence> iterator ()
140   {
141     return mData.keySet ().iterator ();
142   }
143 
144   public boolean remove (Object key)
145   {
146     return mData.remove (key) == MARKER;
147   }
148 
149   public boolean removeAll (Collection<?> c)
150   {
151     boolean changed = false;
152 
153     for (Object element : c)
154     {
155       if (mData.containsKey (element))
156       {
157         changed = true;
158         mData.remove (element);
159       }
160     }
161     return changed;
162   }
163 
164   public boolean retainAll (Collection<?> c)
165   {
166     boolean changed = false;
167 
168     for (CharSequence element : mData.keySet ())
169     {
170       if (! c.contains (element))
171       {
172         changed = true;
173         mData.remove (element);
174       }
175     }
176 
177     return changed;
178   }
179 
180   public int size ()
181   {
182     return mData.size ();
183   }
184 
185   public Object[] toArray ()
186   {
187     // TODO Auto-generated method stub
188     return null;
189   }
190 
191   public <T> T[] toArray (T[] a)
192   {
193     // TODO Auto-generated method stub
194     return null;
195   }
196 
197   @Override
198   public boolean equals (Object obj)
199   {
200     return mData.equals (obj);
201   }
202 
203   @Override
204   public int hashCode ()
205   {
206     return mData.hashCode ();
207   }
208 
209   private int compare (CharSequence k1, CharSequence k2)
210   {
211     return (comparator () == null ? ((Comparable<CharSequence>) k1).compareTo (k2) : comparator ()
212         .compare ((CharSequence) k1, (CharSequence) k2));
213   }
214 
215   private static class TSTStringSetSubSet implements SortedSet<CharSequence>
216   {
217     private CharSequence mFromElement;
218     private CharSequence mToElement;
219     private TernarySearchTreeStringSet mParent;
220     private Comparator<? super CharSequence> mComparator;
221 
222     TSTStringSetSubSet (TernarySearchTreeStringSet parent, CharSequence fromElement,
223         CharSequence toElement)
224     {
225       if (parent.compare (fromElement, toElement) > 0)
226         throw new IllegalArgumentException ("Invalid parameters: fromElement > toElement");
227 
228       mParent = parent;
229       mComparator = parent.comparator ();
230       mFromElement = fromElement;
231       mToElement = toElement;
232     }
233 
234     public Comparator<? super CharSequence> comparator ()
235     {
236       return mComparator;
237     }
238 
239     public CharSequence first ()
240     {
241       // TODO Auto-generated method stub
242       return null;
243     }
244 
245     public SortedSet<CharSequence> headSet (CharSequence toElement)
246     {
247       // TODO Auto-generated method stub
248       return null;
249     }
250 
251     public CharSequence last ()
252     {
253       // TODO Auto-generated method stub
254       return null;
255     }
256 
257     public SortedSet<CharSequence> subSet (CharSequence fromElement, CharSequence toElement)
258     {
259       // TODO Auto-generated method stub
260       return null;
261     }
262 
263     public SortedSet<CharSequence> tailSet (CharSequence fromElement)
264     {
265       // TODO Auto-generated method stub
266       return null;
267     }
268 
269     public boolean add (CharSequence e)
270     {
271       // TODO Auto-generated method stub
272       return false;
273     }
274 
275     public boolean addAll (Collection<? extends CharSequence> c)
276     {
277       // TODO Auto-generated method stub
278       return false;
279     }
280 
281     public void clear ()
282     {
283       // TODO Auto-generated method stub
284 
285     }
286 
287     public boolean contains (Object o)
288     {
289       // TODO Auto-generated method stub
290       return false;
291     }
292 
293     public boolean containsAll (Collection<?> c)
294     {
295       // TODO Auto-generated method stub
296       return false;
297     }
298 
299     public boolean isEmpty ()
300     {
301       // TODO Auto-generated method stub
302       return false;
303     }
304 
305     public Iterator<CharSequence> iterator ()
306     {
307       // TODO Auto-generated method stub
308       return null;
309     }
310 
311     public boolean remove (Object o)
312     {
313       // TODO Auto-generated method stub
314       return false;
315     }
316 
317     public boolean removeAll (Collection<?> c)
318     {
319       // TODO Auto-generated method stub
320       return false;
321     }
322 
323     public boolean retainAll (Collection<?> c)
324     {
325       // TODO Auto-generated method stub
326       return false;
327     }
328 
329     public int size ()
330     {
331       // TODO Auto-generated method stub
332       return 0;
333     }
334 
335     public Object[] toArray ()
336     {
337       // TODO Auto-generated method stub
338       return null;
339     }
340 
341     public <T> T[] toArray (T[] a)
342     {
343       // TODO Auto-generated method stub
344       return null;
345     }
346 
347   }
348 }