Original 2.4
[marc4j.git] / src / org / marc4j / RecordStack.java
1 // $Id: RecordStack.java,v 1.2 2008/09/26 21:17:42 haschart Exp $\r
2 /**\r
3  * Copyright (C) 2004 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\r
7  * MARC4J is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU Lesser General Public \r
9  * License as published by the Free Software Foundation; either \r
10  * version 2.1 of the License, or (at your option) any later version.\r
11  *\r
12  * MARC4J is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
15  * Lesser General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU Lesser General Public \r
18  * License along with MARC4J; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  *\r
21  */\r
22 package org.marc4j;\r
23 \r
24 import java.util.ArrayList;\r
25 import java.util.List;\r
26 \r
27 import org.marc4j.marc.Record;\r
28 \r
29 /**\r
30  * Provides <code>push</code> and <code>pop</code> operations for\r
31  * <code>Record</code> objects created by <code>MarcXmlParser</code>.\r
32  * \r
33  * @author Bas Peters\r
34  * @version $Revision: 1.2 $\r
35  */\r
36 public class RecordStack {\r
37 \r
38   private List list;\r
39   private RuntimeException re = null;\r
40   private boolean eof = false;\r
41 \r
42   /**\r
43    * Default constuctor.\r
44    *  \r
45    */\r
46   public RecordStack() {\r
47     list = new ArrayList();\r
48   }\r
49 \r
50   /**\r
51    * Pushes a <code>Record</code> object on the stack.\r
52    * \r
53    * @param record\r
54    *          the record object\r
55    */\r
56   public synchronized void push(Record record) {\r
57     while (list.size() > 0) {\r
58       try {\r
59         wait();\r
60       } catch (Exception e) {\r
61       }\r
62     }\r
63     list.add(record);\r
64     notifyAll();\r
65   }\r
66 \r
67   /**\r
68    * Removes the <code>Record</code> object from the stack and returns that\r
69    * object.\r
70    * \r
71    * @return Record - the record object\r
72    */\r
73   public synchronized Record pop() {\r
74     while (list.size() <= 0 && (eof != true)) {\r
75       try {\r
76         wait();\r
77       } catch (Exception e) {\r
78       }\r
79     }\r
80     if (re != null) throw(re);\r
81     Record record = null;\r
82     if (list.size() > 0)\r
83       record = (Record) list.remove(0);\r
84     notifyAll();\r
85     return record;\r
86 \r
87   }\r
88 \r
89   /**\r
90    * Returns true if there are more <code>Record</code> objects to expect,\r
91    * false otherwise.\r
92    * \r
93    * @return boolean\r
94    */\r
95   public synchronized boolean hasNext() {\r
96     while (list.size() <= 0 && (eof != true)) {\r
97       try {\r
98         wait();\r
99       } catch (Exception e) {\r
100       }\r
101     }\r
102     if (re != null) throw(re);\r
103     if (!isEmpty() || !eof)\r
104       return true;\r
105     return false;\r
106   }\r
107 \r
108   /**\r
109    * Passes the exception to the thread where the MarcXMLReader is running, so that the  next() call\r
110    * that is blocked waiting for this thread, will receive the exception.\r
111    *  \r
112    */\r
113   public synchronized void passException(RuntimeException e) {\r
114     re = e;\r
115     eof = true;\r
116     notifyAll();\r
117   }\r
118 \r
119   /**\r
120    * Called when the end of the document is reached.\r
121    *  \r
122    */\r
123   public synchronized void end() {\r
124     eof = true;\r
125     notifyAll();\r
126   }\r
127 \r
128   /**\r
129    * Returns true if the queue is empty, false otherwise.\r
130    * \r
131    * @return boolean\r
132    */\r
133   private synchronized boolean isEmpty() {\r
134     return (list.size() == 0 ? true : false);\r
135   }\r
136 \r
137 }