1 package com.calipso.reportgenerator.reportcalculator; 2 3 import java.util.Iterator ; 4 import java.util.NoSuchElementException ; 5 import java.io.FileInputStream ; 6 import java.io.ObjectInputStream ; 7 8 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) throws Exception { 22 this.blockMatrix = blockMatrix; 23 if(!blockMatrix.isEmpty()){ 24 preparePart(index); 26 index++; 27 iteratedPart = blockSerializerThread.getPreparedPart().iterator(); 28 if(index < blockMatrix.getFileNames().size()){ 29 preparePart(index); 30 index++; 31 } 32 } 33 } 34 35 41 42 private void preparePart(int index) { 43 blockSerializerThread = new BlockSerializerThread(blockMatrix.getFileName(index)); 44 blockSerializerThread.setPriority(Thread.MIN_PRIORITY); 45 blockSerializerThread.start(); 46 } 47 48 public synchronized boolean hasNext() { 49 try{ 50 if(iteratedPart.hasNext()){ 51 return true; 52 }else if(index <= blockMatrix.getFileNames().size() && blockSerializerThread!=null){ 53 56 iteratedPart = null; 57 iteratedPart = blockSerializerThread.getPreparedPart().iterator(); 58 if(index < blockMatrix.getFileNames().size()){ 59 preparePart(index); 60 } 61 index++; 62 return iteratedPart.hasNext(); 63 } 64 }catch(Exception e){ 65 throw new NoSuchElementException (e.getMessage()); 66 }catch(Throwable t){ 67 throw new NoSuchElementException (t.getMessage()); 68 } 69 System.gc(); 70 return false; 71 } 72 73 public Object next() { 74 return iteratedPart.next(); 75 } 76 77 public void remove() { 78 throw new NoSuchMethodError (); 79 } 80 } 81 | Popular Tags |