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