Original 2.4
[marc4j.git] / src / org / marc4j / marc / impl / DataFieldImpl.java
1 // $Id: DataFieldImpl.java,v 1.6 2006/08/04 12:31:41 bpeters 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 package org.marc4j.marc.impl;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.Iterator;\r
25 import java.util.List;\r
26 \r
27 import org.marc4j.marc.DataField;\r
28 import org.marc4j.marc.IllegalAddException;\r
29 import org.marc4j.marc.Subfield;\r
30 \r
31 /**\r
32  * Represents a data field in a MARC record.\r
33  * \r
34  * @author Bas Peters\r
35  * @version $Revision: 1.6 $\r
36  */\r
37 public class DataFieldImpl extends VariableFieldImpl implements DataField {\r
38 \r
39     private Long id;\r
40     \r
41     private char ind1;\r
42 \r
43     private char ind2;\r
44 \r
45     private List subfields;\r
46 \r
47     /**\r
48      * Creates a new <code>DataField</code>.\r
49      */\r
50     public DataFieldImpl() {\r
51         subfields = new ArrayList();\r
52     }\r
53 \r
54     /**\r
55      * Creates a new <code>DataField</code> and sets the tag name and the\r
56      * first and second indicator.\r
57      * \r
58      * @param tag\r
59      *            the tag name\r
60      * @param ind1\r
61      *            the first indicator\r
62      * @param ind2\r
63      *            the second indicator\r
64      */\r
65     public DataFieldImpl(String tag, char ind1, char ind2) {\r
66         this();\r
67         this.setTag(tag);\r
68         this.setIndicator1(ind1);\r
69         this.setIndicator2(ind2);\r
70     }\r
71 \r
72     /**\r
73      * Sets the first indicator.\r
74      * \r
75      * @param ind1\r
76      *            the first indicator\r
77      */\r
78     public void setIndicator1(char ind1) {\r
79         this.ind1 = ind1;\r
80     }\r
81 \r
82     /**\r
83      * Returns the first indicator\r
84      * \r
85      * @return char - the first indicator\r
86      */\r
87     public char getIndicator1() {\r
88         return ind1;\r
89     }\r
90 \r
91     /**\r
92      * Sets the second indicator.\r
93      * \r
94      * @param ind2\r
95      *            the second indicator\r
96      */\r
97     public void setIndicator2(char ind2) {\r
98         this.ind2 = ind2;\r
99     }\r
100 \r
101     /**\r
102      * Returns the second indicator\r
103      * \r
104      * @return char - the second indicator\r
105      */\r
106     public char getIndicator2() {\r
107         return ind2;\r
108     }\r
109 \r
110     /**\r
111      * Adds a <code>Subfield</code>.\r
112      * \r
113      * @param subfield\r
114      *            the <code>Subfield</code> object\r
115      * @throws IllegalAddException\r
116      *             when the parameter is not a <code>Subfield</code> instance\r
117      */\r
118     public void addSubfield(Subfield subfield) {\r
119         if (subfield instanceof SubfieldImpl)\r
120             subfields.add(subfield);\r
121         else\r
122             throw new IllegalAddException("Subfield");\r
123     }\r
124 \r
125     /**\r
126      * Inserts a <code>Subfield</code> at the specified position.\r
127      * \r
128      * @param index\r
129      *            the position within the list\r
130      * @param subfield\r
131      *            the <code>Subfield</code> object\r
132      * @throws IllegalAddException\r
133      *             when the parameter is not a <code>Subfield</code> instance\r
134      */\r
135     public void addSubfield(int index, Subfield subfield) {\r
136         subfields.add(index, subfield);\r
137     }\r
138 \r
139     /**\r
140      * Removes a <code>Subfield</code>.\r
141      */\r
142     public void removeSubfield(Subfield subfield) {\r
143         subfields.remove(subfield);\r
144     }\r
145 \r
146     /**\r
147      * Returns the list of <code>Subfield</code> objects.\r
148      * \r
149      * @return List - the list of <code>Subfield</code> objects\r
150      */\r
151     public List getSubfields() {\r
152         return subfields;\r
153     }\r
154 \r
155     public List getSubfields(char code) {\r
156         List retSubfields = new ArrayList();\r
157         Iterator i = subfields.iterator();\r
158         while (i.hasNext()) {\r
159             Subfield sf = (Subfield) i.next();\r
160             if (sf.getCode() == code)\r
161                 retSubfields.add(sf);\r
162         }\r
163         return retSubfields;\r
164     }\r
165 \r
166     public Subfield getSubfield(char code) {\r
167         Iterator i = subfields.iterator();\r
168         while (i.hasNext()) {\r
169             Subfield sf = (Subfield) i.next();\r
170             if (sf.getCode() == code)\r
171                 return sf;\r
172         }\r
173         return null;\r
174     }\r
175 \r
176     public boolean find(String pattern) {\r
177         Iterator i = subfields.iterator();\r
178         while (i.hasNext()) {\r
179             Subfield sf = (Subfield) i.next();\r
180             if (sf.find(pattern))\r
181                 return true;\r
182         }\r
183         return false;\r
184     }\r
185 \r
186     /**\r
187      * Returns a string representation of this data field.\r
188      * \r
189      * <p>\r
190      * Example:\r
191      * \r
192      * <pre>\r
193      *    245 10$aSummerland /$cMichael Chabon.\r
194      * </pre>\r
195      * \r
196      * @return String - a string representation of this data field\r
197      */\r
198     public String toString() {\r
199         StringBuffer sb = new StringBuffer();\r
200         sb.append(super.toString());\r
201         sb.append(' ');\r
202         sb.append(getIndicator1());\r
203         sb.append(getIndicator2());\r
204         Iterator i = getSubfields().iterator();\r
205         while (i.hasNext()) {\r
206             Subfield sf = (Subfield) i.next();\r
207             sb.append(sf.toString());\r
208         }\r
209         return sb.toString();\r
210     }\r
211 \r
212     public void setId(Long id) {\r
213         this.id = id;\r
214     }\r
215 \r
216     public Long getId() {\r
217         return id;\r
218     }\r
219 \r
220 }