01: package com.calipso.reportgenerator.reportcalculator;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05: import java.io.FileInputStream;
06: import java.io.ObjectInputStream;
07:
08: /**
09: *
10: * User: jbassino
11: * Date: 16-sep-2005
12: * Time: 13:35:22
13: */
14: public class BlockMatrixIterator implements Iterator {
15: private BlockMatrix blockMatrix;
16:
17: private BlockSerializerThread blockSerializerThread;
18: private Iterator iteratedPart;
19: private int index = 0;
20:
21: public BlockMatrixIterator(BlockMatrix blockMatrix)
22: throws Exception {
23: this .blockMatrix = blockMatrix;
24: if (!blockMatrix.isEmpty()) {
25: //iteratedPart = prepareFirstPart(index);
26: preparePart(index);
27: index++;
28: iteratedPart = blockSerializerThread.getPreparedPart()
29: .iterator();
30: if (index < blockMatrix.getFileNames().size()) {
31: preparePart(index);
32: index++;
33: }
34: }
35: }
36:
37: /*private Iterator prepareFirstPart(int index) throws Exception{
38: FileInputStream fileInputStream = new FileInputStream(blockMatrix.getFileName(index));
39: ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
40: Matrix matrix = (Matrix)objectInputStream.readObject();
41: return matrix.iterator();
42: }*/
43:
44: private void preparePart(int index) {
45: blockSerializerThread = new BlockSerializerThread(blockMatrix
46: .getFileName(index));
47: blockSerializerThread.setPriority(Thread.MIN_PRIORITY);
48: blockSerializerThread.start();
49: }
50:
51: public synchronized boolean hasNext() {
52: try {
53: if (iteratedPart.hasNext()) {
54: return true;
55: } else if (index <= blockMatrix.getFileNames().size()
56: && blockSerializerThread != null) {
57: /*while(blockSerializerThread.getPreparedPart()==null){
58: this.wait();
59: } */
60: iteratedPart = null;
61: iteratedPart = blockSerializerThread.getPreparedPart()
62: .iterator();
63: if (index < blockMatrix.getFileNames().size()) {
64: preparePart(index);
65: }
66: index++;
67: return iteratedPart.hasNext();
68: }
69: } catch (Exception e) {
70: throw new NoSuchElementException(e.getMessage());
71: } catch (Throwable t) {
72: throw new NoSuchElementException(t.getMessage());
73: }
74: System.gc();
75: return false;
76: }
77:
78: public Object next() {
79: return iteratedPart.next();
80: }
81:
82: public void remove() {
83: throw new NoSuchMethodError();
84: }
85: }
|