001: // kelondroMergeIterator.java
002: // --------------------------
003: // part of The Kelondro Database
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2005
007: // last major change: 08.05.2005
008: //
009: // This program is free software; you can redistribute it and/or modify
010: // it under the terms of the GNU General Public License as published by
011: // the Free Software Foundation; either version 2 of the License, or
012: // (at your option) any later version.
013: //
014: // This program is distributed in the hope that it will be useful,
015: // but WITHOUT ANY WARRANTY; without even the implied warranty of
016: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: // GNU General Public License for more details.
018: //
019: // You should have received a copy of the GNU General Public License
020: // along with this program; if not, write to the Free Software
021: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: //
023: // Using this software in any meaning (reading, learning, copying, compiling,
024: // running) means that you agree that the Author(s) is (are) not responsible
025: // for cost, loss of data or any harm that may be caused directly or indirectly
026: // by usage of this softare or this documentation. The usage of this software
027: // is on your own risk. The installation and usage (starting/running) of this
028: // software may allow other people or application to access your computer and
029: // any attached devices and is highly dependent on the configuration of the
030: // software which must be done by the user of the software; the author(s) is
031: // (are) also not responsible for proper configuration and usage of the
032: // software, even if provoked by documentation provided together with
033: // the software.
034: //
035: // Any changes to this file according to the GPL as documented in the file
036: // gpl.txt aside this file in the shipment you received can be done to the
037: // lines that follows this copyright notice here, but changes must not be
038: // done inside the copyright notive above. A re-distribution must contain
039: // the intact and unchanged copyright notice.
040: // Contributions and changes to the program code must be marked as such.
041:
042: package de.anomic.kelondro;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import java.util.Comparator;
047: import java.util.ConcurrentModificationException;
048: import java.util.Iterator;
049: import java.util.Set;
050:
051: public class kelondroMergeIterator<E> implements
052: kelondroCloneableIterator<E> {
053:
054: Comparator<E> comp;
055: kelondroCloneableIterator<E> a, b;
056: E na, nb;
057: Method merger;
058: boolean up;
059:
060: public kelondroMergeIterator(kelondroCloneableIterator<E> a,
061: kelondroCloneableIterator<E> b, Comparator<E> c, Method m,
062: boolean up) {
063: // this works currently only for String-type key iterations
064: this .a = a;
065: this .b = b;
066: this .up = up;
067: this .comp = c;
068: this .merger = m;
069: nexta();
070: nextb();
071: }
072:
073: public kelondroMergeIterator<E> clone(Object modifier) {
074: return new kelondroMergeIterator<E>(a.clone(modifier), b
075: .clone(modifier), comp, merger, up);
076: }
077:
078: public void finalize() {
079: // call finalizer of embedded objects
080: a = null;
081: b = null;
082: na = null;
083: nb = null;
084: comp = null;
085: }
086:
087: private void nexta() {
088: try {
089: if ((a != null) && (a.hasNext()))
090: na = a.next();
091: else
092: na = null;
093: } catch (ConcurrentModificationException e) {
094: na = null;
095: }
096: }
097:
098: private void nextb() {
099: try {
100: if ((b != null) && (b.hasNext()))
101: nb = b.next();
102: else
103: nb = null;
104: } catch (ConcurrentModificationException e) {
105: nb = null;
106: }
107: }
108:
109: public boolean hasNext() {
110: return (na != null) || (nb != null);
111: }
112:
113: @SuppressWarnings("unchecked")
114: public E next() {
115: E s;
116: if (na == null) {
117: s = nb;
118: nextb();
119: return s;
120: }
121: if (nb == null) {
122: s = na;
123: nexta();
124: return s;
125: }
126: // compare the Objects
127: int c = comp.compare(na, nb);
128: if (c == 0) {
129: try {
130: //System.out.print("MERGE OF " + na.toString() + " AND " + nb.toString() + ": ");
131: s = (E) this .merger.invoke(null, new Object[] {
132: (Object) na, (Object) nb });
133: //System.out.println("RESULT IS " + s.toString());
134: } catch (IllegalArgumentException e) {
135: e.printStackTrace();
136: s = null;
137: } catch (IllegalAccessException e) {
138: e.printStackTrace();
139: s = null;
140: } catch (InvocationTargetException e) {
141: e.printStackTrace();
142: s = null;
143: }
144: nexta();
145: nextb();
146: return s;
147: } else if (((up) && (c < 0)) || ((!(up)) && (c > 0))) {
148: s = na;
149: nexta();
150: return s;
151: } else {
152: s = nb;
153: nextb();
154: return s;
155: }
156: }
157:
158: public void remove() {
159: throw new java.lang.UnsupportedOperationException(
160: "merge does not support remove");
161: }
162:
163: public static <A> kelondroCloneableIterator<A> cascade(
164: Set<kelondroCloneableIterator<A>> iterators,
165: kelondroOrder<A> c, Method merger, boolean up) {
166: // this extends the ability to combine two iterators
167: // to the ability of combining a set of iterators
168: if (iterators == null)
169: return null;
170: if (iterators.size() == 0)
171: return null;
172: return cascade(iterators.iterator(), c, merger, up);
173: }
174:
175: private static <A> kelondroCloneableIterator<A> cascade(
176: Iterator<kelondroCloneableIterator<A>> iiterators,
177: kelondroOrder<A> c, Method merger, boolean up) {
178: if (iiterators == null)
179: return null;
180: if (!(iiterators.hasNext()))
181: return null;
182: kelondroCloneableIterator<A> one = iiterators.next();
183: if (!(iiterators.hasNext()))
184: return one;
185: return new kelondroMergeIterator<A>(one, cascade(iiterators, c,
186: merger, up), c, merger, up);
187: }
188:
189: public static Method simpleMerge = null;
190: static {
191: try {
192: Class<?> c = Class
193: .forName("de.anomic.kelondro.kelondroMergeIterator");
194: simpleMerge = c.getMethod("mergeEqualByReplace",
195: new Class[] { Object.class, Object.class });
196: } catch (SecurityException e) {
197: System.out.println("Error while initializing simpleMerge: "
198: + e.getMessage());
199: simpleMerge = null;
200: } catch (ClassNotFoundException e) {
201: System.out.println("Error while initializing simpleMerge: "
202: + e.getMessage());
203: simpleMerge = null;
204: } catch (NoSuchMethodException e) {
205: System.out.println("Error while initializing simpleMerge: "
206: + e.getMessage());
207: simpleMerge = null;
208: }
209: }
210:
211: public static Object mergeEqualByReplace(Object a, Object b) {
212: return a;
213: }
214:
215: }
|