KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > SortedCollection4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation;
22
23 /**
24  * @exclude
25  */

26 public class SortedCollection4 {
27     
28     private final Comparison4 _comparison;
29     private Tree _tree;
30
31     public SortedCollection4(Comparison4 comparison) {
32         if (null == comparison) {
33             throw new ArgumentNullException();
34         }
35         _comparison = comparison;
36         _tree = null;
37     }
38     
39     public Object JavaDoc singleElement() {
40         if (1 != size()) {
41             throw new IllegalStateException JavaDoc();
42         }
43         return _tree.key();
44     }
45     
46     public void addAll(Iterator4 iterator) {
47         while (iterator.moveNext()) {
48             add(iterator.current());
49         }
50     }
51
52     public void add(Object JavaDoc element) {
53         _tree = Tree.add(_tree, new TreeObject(element, _comparison));
54     }
55
56     public void remove(Object JavaDoc element) {
57         _tree = Tree.removeLike(_tree, new TreeObject(element, _comparison));
58     }
59
60     public Object JavaDoc[] toArray(final Object JavaDoc[] array) {
61         Tree.traverse(_tree, new Visitor4() {
62             int i = 0;
63             public void visit(Object JavaDoc obj) {
64                 array[i++] = ((TreeObject)obj).key();
65             }
66         });
67         return array;
68     }
69     
70     public int size() {
71         return Tree.size(_tree);
72     }
73     
74 }
75
Popular Tags