1 // $Id: RecordStack.java,v 1.2 2008/09/26 21:17:42 haschart Exp $
\r
3 * Copyright (C) 2004 Bas Peters
\r
5 * This file is part of MARC4J
\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
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
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
24 import java.util.ArrayList;
\r
25 import java.util.List;
\r
27 import org.marc4j.marc.Record;
\r
30 * Provides <code>push</code> and <code>pop</code> operations for
\r
31 * <code>Record</code> objects created by <code>MarcXmlParser</code>.
\r
33 * @author Bas Peters
\r
34 * @version $Revision: 1.2 $
\r
36 public class RecordStack {
\r
38 private List<Record> list;
\r
39 private RuntimeException re = null;
\r
40 private boolean eof = false;
\r
43 * Default constuctor.
\r
46 public RecordStack() {
\r
47 list = new ArrayList<Record>();
\r
51 * Pushes a <code>Record</code> object on the stack.
\r
56 public synchronized void push(Record record) {
\r
57 while (list.size() > 0) {
\r
60 } catch (Exception e) {
\r
68 * Removes the <code>Record</code> object from the stack and returns that
\r
71 * @return Record - the record object
\r
73 public synchronized Record pop() {
\r
74 while (list.size() <= 0 && (eof != true)) {
\r
77 } catch (Exception e) {
\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
90 * Returns true if there are more <code>Record</code> objects to expect,
\r
95 public synchronized boolean hasNext() {
\r
96 while (list.size() <= 0 && (eof != true)) {
\r
99 } catch (Exception e) {
\r
102 if (re != null) throw(re);
\r
103 if (!isEmpty() || !eof)
\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
113 public synchronized void passException(RuntimeException e) {
\r
120 * Called when the end of the document is reached.
\r
123 public synchronized void end() {
\r
129 * Returns true if the queue is empty, false otherwise.
\r
133 private synchronized boolean isEmpty() {
\r
134 return (list.size() == 0 ? true : false);
\r