KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > btree > BTreeRangeUnion


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.inside.btree;
22
23 import com.db4o.foundation.*;
24 import com.db4o.inside.btree.algebra.*;
25
26 public class BTreeRangeUnion implements BTreeRange {
27
28     private final BTreeRangeSingle[] _ranges;
29
30     public BTreeRangeUnion(BTreeRangeSingle[] ranges) {
31         this(toSortedCollection(ranges));
32     }
33
34     public BTreeRangeUnion(SortedCollection4 sorted) {
35         if (null == sorted) {
36             throw new ArgumentNullException();
37         }
38         _ranges = toArray(sorted);
39     }
40     
41     public void accept(BTreeRangeVisitor visitor) {
42         visitor.visit(this);
43     }
44     
45     public boolean isEmpty() {
46         for (int i = 0; i < _ranges.length; i++) {
47             if (!_ranges[i].isEmpty()) {
48                 return false;
49             }
50         }
51         return true;
52     }
53
54     private static SortedCollection4 toSortedCollection(BTreeRangeSingle[] ranges) {
55         if (null == ranges) {
56             throw new ArgumentNullException();
57         }
58         SortedCollection4 collection = new SortedCollection4(BTreeRangeSingle.COMPARISON);
59         for (int i = 0; i < ranges.length; i++) {
60             BTreeRangeSingle range = ranges[i];
61             if (!range.isEmpty()) {
62                 collection.add(range);
63             }
64         }
65         return collection;
66     }
67
68     private static BTreeRangeSingle[] toArray(SortedCollection4 collection) {
69         return (BTreeRangeSingle[]) collection.toArray(new BTreeRangeSingle[collection.size()]);
70     }
71
72     public BTreeRange extendToFirst() {
73         throw new NotImplementedException();
74     }
75
76     public BTreeRange extendToLast() {
77         throw new NotImplementedException();
78     }
79
80     public BTreeRange extendToLastOf(BTreeRange upperRange) {
81         throw new NotImplementedException();
82     }
83
84     public BTreeRange greater() {
85         throw new NotImplementedException();
86     }
87
88     public BTreeRange intersect(BTreeRange range) {
89         if (null == range) {
90             throw new ArgumentNullException();
91         }
92         return new BTreeRangeUnionIntersect(this).dispatch(range);
93     }
94     
95     public Iterator4 pointers() {
96         return Iterators.concat(Iterators.map(_ranges, new Function4() {
97             public Object JavaDoc apply(Object JavaDoc range) {
98                 return ((BTreeRange)range).pointers();
99             }
100         }));
101     }
102
103     public Iterator4 keys() {
104         return Iterators.concat(Iterators.map(_ranges, new Function4() {
105             public Object JavaDoc apply(Object JavaDoc range) {
106                 return ((BTreeRange)range).keys();
107             }
108         }));
109     }
110     
111     public int size() {
112         int size = 0;
113         for (int i = 0; i < _ranges.length; i++) {
114             size += _ranges[i].size();
115         }
116         return size;
117     }
118
119     public BTreeRange smaller() {
120         throw new NotImplementedException();
121     }
122
123     public BTreeRange union(BTreeRange other) {
124         if (null == other) {
125             throw new ArgumentNullException();
126         }
127         return new BTreeRangeUnionUnion(this).dispatch(other);
128     }
129
130     public Iterator4 ranges() {
131         return new ArrayIterator4(_ranges);
132     }
133
134     public BTreePointer lastPointer() {
135         throw new NotImplementedException();
136     }
137 }
138
Popular Tags