1 // $Id: RecordImpl.java,v 1.5 2008/09/26 21:17:43 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
21 package org.marc4j.marc.impl;
\r
23 import java.util.ArrayList;
\r
24 import java.util.Collections;
\r
25 import java.util.Iterator;
\r
26 import java.util.List;
\r
28 import org.marc4j.marc.ControlField;
\r
29 import org.marc4j.marc.DataField;
\r
30 import org.marc4j.marc.IllegalAddException;
\r
31 import org.marc4j.marc.Leader;
\r
32 import org.marc4j.marc.Record;
\r
33 import org.marc4j.marc.VariableField;
\r
36 * Represents a MARC record.
\r
38 * @author Bas Peters
\r
39 * @version $Revision: 1.5 $
\r
41 public class RecordImpl implements Record {
\r
46 private static final long serialVersionUID = 1906886225329068668L;
\r
50 private Leader leader;
\r
52 private List<ControlField> controlFields;
\r
54 private List<DataField> dataFields;
\r
56 private String type;
\r
59 * Creates a new <code>Record</code>.
\r
61 public RecordImpl() {
\r
62 controlFields = new ArrayList<ControlField>();
\r
63 dataFields = new ArrayList<DataField>();
\r
66 public void setType(String type) {
\r
70 public String getType() {
\r
74 public void setLeader(Leader leader) {
\r
75 this.leader = leader;
\r
78 public Leader getLeader() {
\r
83 * Adds a <code>VariableField</code> being a <code>ControlField</code>
\r
84 * or <code>DataField</code>.
\r
86 * If the <code>VariableField</code> is a control number field (001) and
\r
87 * the record already has a control number field, the field is replaced with
\r
91 * the <code>VariableField</code>
\r
92 * @throws IllegalAddException
\r
93 * when the parameter is not a <code>VariableField</code>
\r
96 @SuppressWarnings("unchecked")
\r
97 public void addVariableField(VariableField field) {
\r
98 if (!(field instanceof VariableField))
\r
99 throw new IllegalAddException("Expected VariableField instance");
\r
101 String tag = field.getTag();
\r
102 if (Verifier.isControlNumberField(tag)) {
\r
103 if (Verifier.hasControlNumberField(controlFields))
\r
104 // TODO create converter from VariableField to ControlField
\r
105 controlFields.set(0, ((ControlField) field));
\r
107 controlFields.add(0, ((ControlField) field));
\r
108 Collections.sort(controlFields);
\r
109 } else if (Verifier.isControlField(tag)) {
\r
110 controlFields.add(((ControlField) field));
\r
111 Collections.sort(controlFields);
\r
113 dataFields.add(((DataField) field));
\r
114 Collections.sort(dataFields);
\r
119 public void removeVariableField(VariableField field) {
\r
120 String tag = field.getTag();
\r
121 if (Verifier.isControlField(tag))
\r
122 controlFields.remove(field);
\r
124 dataFields.remove(field);
\r
128 * Returns the control number field or <code>null</code> if no control
\r
129 * number field is available.
\r
131 * @return ControlField - the control number field
\r
133 public ControlField getControlNumberField() {
\r
134 if (Verifier.hasControlNumberField(controlFields))
\r
135 return (ControlField) controlFields.get(0);
\r
140 public List<ControlField> getControlFields() {
\r
141 return controlFields;
\r
144 public List<DataField> getDataFields() {
\r
148 public VariableField getVariableField(String tag) {
\r
149 @SuppressWarnings("rawtypes")
\r
151 if (Verifier.isControlField(tag))
\r
152 i = controlFields.iterator();
\r
154 i = dataFields.iterator();
\r
155 while (i.hasNext()) {
\r
156 VariableField field = (VariableField) i.next();
\r
157 if (field.getTag().equals(tag))
\r
163 public List<VariableField> getVariableFields(String tag) {
\r
164 List<VariableField> fields = new ArrayList<VariableField>();
\r
165 @SuppressWarnings("rawtypes")
\r
167 if (Verifier.isControlField(tag))
\r
168 i = controlFields.iterator();
\r
170 i = dataFields.iterator();
\r
171 while (i.hasNext()) {
\r
172 VariableField field = (VariableField) i.next();
\r
173 if (field.getTag().equals(tag))
\r
179 @SuppressWarnings("rawtypes")
\r
180 public List<VariableField> getVariableFields() {
\r
181 List<VariableField> fields = new ArrayList<VariableField>();
\r
183 i = ((List) controlFields).iterator();
\r
184 while (i.hasNext())
\r
185 fields.add((VariableField) i.next());
\r
186 i = dataFields.iterator();
\r
187 while (i.hasNext())
\r
188 fields.add((VariableField) i.next());
\r
192 public String getControlNumber() {
\r
193 ControlField f = getControlNumberField();
\r
194 String result = (f == null || f.getData() == null) ? null : new String(f.getData());
\r
198 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
199 public List<VariableField> getVariableFields(String[] tags) {
\r
200 List list = new ArrayList();
\r
201 for (int i = 0; i < tags.length; i++) {
\r
202 String tag = tags[i];
\r
203 List fields = getVariableFields(tag);
\r
204 if (fields.size() > 0)
\r
205 list.addAll(fields);
\r
211 * Returns a string representation of this record.
\r
218 * LEADER 00714cam a2200205 a 4500
\r
220 * 005 20030616111422.0
\r
221 * 008 020805s2002 nyu j 000 1 eng
\r
222 * 020 $a0786808772
\r
223 * 020 $a0786816155 (pbk.)
\r
224 * 040 $aDLC$cDLC$dDLC
\r
225 * 100 1 $aChabon, Michael.
\r
226 * 245 10$aSummerland /$cMichael Chabon.
\r
228 * 260 $aNew York :$bMiramax Books/Hyperion Books for Children,$cc2002.
\r
229 * 300 $a500 p. ;$c22 cm.
\r
231 * 650 1$aBaseball$vFiction.
\r
232 * 650 1$aMagic$vFiction.
\r
236 * @return String - a string representation of this record
\r
238 public String toString() {
\r
239 StringBuffer sb = new StringBuffer();
\r
240 sb.append("LEADER ");
\r
241 sb.append(getLeader().toString());
\r
243 @SuppressWarnings("rawtypes")
\r
244 Iterator i = getVariableFields().iterator();
\r
245 while (i.hasNext()) {
\r
246 VariableField field = (VariableField) i.next();
\r
247 sb.append(field.toString());
\r
250 return sb.toString();
\r
253 @SuppressWarnings("unchecked")
\r
254 public List<VariableField> find(String pattern) {
\r
255 @SuppressWarnings("rawtypes")
\r
256 List result = new ArrayList();
\r
257 @SuppressWarnings("rawtypes")
\r
258 Iterator i = controlFields.iterator();
\r
259 while (i.hasNext()) {
\r
260 VariableField field = (VariableField) i.next();
\r
261 if (field.find(pattern))
\r
264 i = dataFields.iterator();
\r
265 while (i.hasNext()) {
\r
266 VariableField field = (VariableField) i.next();
\r
267 if (field.find(pattern))
\r
273 @SuppressWarnings({ "unchecked", "rawtypes" })
\r
274 public List<VariableField> find(String tag, String pattern) {
\r
275 List result = new ArrayList();
\r
276 Iterator i = getVariableFields(tag).iterator();
\r
277 while (i.hasNext()) {
\r
278 VariableField field = (VariableField) i.next();
\r
279 if (field.find(pattern))
\r
285 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
286 public List<VariableField> find(String[] tag, String pattern) {
\r
287 List result = new ArrayList();
\r
288 Iterator i = getVariableFields(tag).iterator();
\r
289 while (i.hasNext()) {
\r
290 VariableField field = (VariableField) i.next();
\r
291 if (field.find(pattern))
\r
297 public void setId(Long id) {
\r
301 public Long getId() {
\r