001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.btree;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.btree.algebra.*;
025:
026: public class BTreeRangeUnion implements BTreeRange {
027:
028: private final BTreeRangeSingle[] _ranges;
029:
030: public BTreeRangeUnion(BTreeRangeSingle[] ranges) {
031: this (toSortedCollection(ranges));
032: }
033:
034: public BTreeRangeUnion(SortedCollection4 sorted) {
035: if (null == sorted) {
036: throw new ArgumentNullException();
037: }
038: _ranges = toArray(sorted);
039: }
040:
041: public void accept(BTreeRangeVisitor visitor) {
042: visitor.visit(this );
043: }
044:
045: public boolean isEmpty() {
046: for (int i = 0; i < _ranges.length; i++) {
047: if (!_ranges[i].isEmpty()) {
048: return false;
049: }
050: }
051: return true;
052: }
053:
054: private static SortedCollection4 toSortedCollection(
055: BTreeRangeSingle[] ranges) {
056: if (null == ranges) {
057: throw new ArgumentNullException();
058: }
059: SortedCollection4 collection = new SortedCollection4(
060: BTreeRangeSingle.COMPARISON);
061: for (int i = 0; i < ranges.length; i++) {
062: BTreeRangeSingle range = ranges[i];
063: if (!range.isEmpty()) {
064: collection.add(range);
065: }
066: }
067: return collection;
068: }
069:
070: private static BTreeRangeSingle[] toArray(
071: SortedCollection4 collection) {
072: return (BTreeRangeSingle[]) collection
073: .toArray(new BTreeRangeSingle[collection.size()]);
074: }
075:
076: public BTreeRange extendToFirst() {
077: throw new NotImplementedException();
078: }
079:
080: public BTreeRange extendToLast() {
081: throw new NotImplementedException();
082: }
083:
084: public BTreeRange extendToLastOf(BTreeRange upperRange) {
085: throw new NotImplementedException();
086: }
087:
088: public BTreeRange greater() {
089: throw new NotImplementedException();
090: }
091:
092: public BTreeRange intersect(BTreeRange range) {
093: if (null == range) {
094: throw new ArgumentNullException();
095: }
096: return new BTreeRangeUnionIntersect(this ).dispatch(range);
097: }
098:
099: public Iterator4 pointers() {
100: return Iterators.concat(Iterators.map(_ranges, new Function4() {
101: public Object apply(Object range) {
102: return ((BTreeRange) range).pointers();
103: }
104: }));
105: }
106:
107: public Iterator4 keys() {
108: return Iterators.concat(Iterators.map(_ranges, new Function4() {
109: public Object apply(Object range) {
110: return ((BTreeRange) range).keys();
111: }
112: }));
113: }
114:
115: public int size() {
116: int size = 0;
117: for (int i = 0; i < _ranges.length; i++) {
118: size += _ranges[i].size();
119: }
120: return size;
121: }
122:
123: public BTreeRange smaller() {
124: throw new NotImplementedException();
125: }
126:
127: public BTreeRange union(BTreeRange other) {
128: if (null == other) {
129: throw new ArgumentNullException();
130: }
131: return new BTreeRangeUnionUnion(this ).dispatch(other);
132: }
133:
134: public Iterator4 ranges() {
135: return new ArrayIterator4(_ranges);
136: }
137:
138: public BTreePointer lastPointer() {
139: throw new NotImplementedException();
140: }
141: }
|