01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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 singleElement() {
40: if (1 != size()) {
41: throw new IllegalStateException();
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 element) {
53: _tree = Tree.add(_tree, new TreeObject(element, _comparison));
54: }
55:
56: public void remove(Object element) {
57: _tree = Tree.removeLike(_tree, new TreeObject(element,
58: _comparison));
59: }
60:
61: public Object[] toArray(final Object[] array) {
62: Tree.traverse(_tree, new Visitor4() {
63: int i = 0;
64:
65: public void visit(Object obj) {
66: array[i++] = ((TreeObject) obj).key();
67: }
68: });
69: return array;
70: }
71:
72: public int size() {
73: return Tree.size(_tree);
74: }
75:
76: }
|