KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportcalculator > BlockMatrixIterator


1 package com.calipso.reportgenerator.reportcalculator;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.NoSuchElementException JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7
8 /**
9  *
10  * User: jbassino
11  * Date: 16-sep-2005
12  * Time: 13:35:22
13  */

14 public class BlockMatrixIterator implements Iterator JavaDoc {
15   private BlockMatrix blockMatrix;
16
17   private BlockSerializerThread blockSerializerThread;
18   private Iterator JavaDoc iteratedPart;
19   private int index = 0;
20
21   public BlockMatrixIterator(BlockMatrix blockMatrix) throws Exception JavaDoc{
22     this.blockMatrix = blockMatrix;
23     if(!blockMatrix.isEmpty()){
24       //iteratedPart = prepareFirstPart(index);
25
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   /*private Iterator prepareFirstPart(int index) throws Exception{
36     FileInputStream fileInputStream = new FileInputStream(blockMatrix.getFileName(index));
37     ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
38     Matrix matrix = (Matrix)objectInputStream.readObject();
39     return matrix.iterator();
40   }*/

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         /*while(blockSerializerThread.getPreparedPart()==null){
54           this.wait();
55         } */

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 JavaDoc e){
65       throw new NoSuchElementException JavaDoc(e.getMessage());
66     }catch(Throwable JavaDoc t){
67       throw new NoSuchElementException JavaDoc(t.getMessage());
68     }
69     System.gc();
70     return false;
71   }
72
73   public Object JavaDoc next() {
74     return iteratedPart.next();
75   }
76
77   public void remove() {
78     throw new NoSuchMethodError JavaDoc();
79   }
80 }
81
Popular Tags