applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / core / Attachment.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.objects.core;\r
23 \r
24 //---------------------------------/\r
25 //- Imported classes and packages -/\r
26 //---------------------------------/\r
27 \r
28 import org.exolab.castor.xml.Marshaller;\r
29 import org.exolab.castor.xml.Unmarshaller;\r
30 \r
31 /**\r
32  * Class Attachment.\r
33  * \r
34  * @version $Revision$ $Date$\r
35  */\r
36 public class Attachment extends uk.ac.vamsas.objects.core.AppData implements\r
37     java.io.Serializable {\r
38 \r
39   // --------------------------/\r
40   // - Class/Member Variables -/\r
41   // --------------------------/\r
42 \r
43   /**\r
44    * true implies data will be decompresses with Zip before presenting to\r
45    * application\r
46    */\r
47   private boolean _compressed = false;\r
48 \r
49   /**\r
50    * keeps track of state for field: _compressed\r
51    */\r
52   private boolean _has_compressed;\r
53 \r
54   /**\r
55    * Type of arbitrary data - TODO: decide format - use (extended) MIME types ?\r
56    */\r
57   private java.lang.String _type;\r
58 \r
59   /**\r
60    * Object the arbitrary data is associated with\r
61    * \r
62    */\r
63   private java.lang.Object _objectref;\r
64 \r
65   /**\r
66    * Primary Key for vamsas object referencing\r
67    * \r
68    */\r
69   private java.lang.String _id;\r
70 \r
71   // ----------------/\r
72   // - Constructors -/\r
73   // ----------------/\r
74 \r
75   public Attachment() {\r
76     super();\r
77   }\r
78 \r
79   // -----------/\r
80   // - Methods -/\r
81   // -----------/\r
82 \r
83   /**\r
84      */\r
85   public void deleteCompressed() {\r
86     this._has_compressed = false;\r
87   }\r
88 \r
89   /**\r
90    * Overrides the java.lang.Object.equals method.\r
91    * \r
92    * @param obj\r
93    * @return true if the objects are equal.\r
94    */\r
95   public boolean equals(final java.lang.Object obj) {\r
96     if (this == obj)\r
97       return true;\r
98 \r
99     if (super.equals(obj) == false)\r
100       return false;\r
101 \r
102     if (obj instanceof Attachment) {\r
103 \r
104       Attachment temp = (Attachment) obj;\r
105       boolean thcycle;\r
106       boolean tmcycle;\r
107       if (this._compressed != temp._compressed)\r
108         return false;\r
109       if (this._has_compressed != temp._has_compressed)\r
110         return false;\r
111       if (this._type != null) {\r
112         if (temp._type == null)\r
113           return false;\r
114         if (this._type != temp._type) {\r
115           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._type);\r
116           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._type);\r
117           if (thcycle != tmcycle) {\r
118             if (!thcycle) {\r
119               org.castor.util.CycleBreaker.releaseCycleHandle(this._type);\r
120             }\r
121             ;\r
122             if (!tmcycle) {\r
123               org.castor.util.CycleBreaker.releaseCycleHandle(temp._type);\r
124             }\r
125             ;\r
126             return false;\r
127           }\r
128           if (!thcycle) {\r
129             if (!this._type.equals(temp._type)) {\r
130               org.castor.util.CycleBreaker.releaseCycleHandle(this._type);\r
131               org.castor.util.CycleBreaker.releaseCycleHandle(temp._type);\r
132               return false;\r
133             }\r
134             org.castor.util.CycleBreaker.releaseCycleHandle(this._type);\r
135             org.castor.util.CycleBreaker.releaseCycleHandle(temp._type);\r
136           }\r
137         }\r
138       } else if (temp._type != null)\r
139         return false;\r
140       if (this._objectref != null) {\r
141         if (temp._objectref == null)\r
142           return false;\r
143         if (this._objectref != temp._objectref) {\r
144           thcycle = org.castor.util.CycleBreaker\r
145               .startingToCycle(this._objectref);\r
146           tmcycle = org.castor.util.CycleBreaker\r
147               .startingToCycle(temp._objectref);\r
148           if (thcycle != tmcycle) {\r
149             if (!thcycle) {\r
150               org.castor.util.CycleBreaker.releaseCycleHandle(this._objectref);\r
151             }\r
152             ;\r
153             if (!tmcycle) {\r
154               org.castor.util.CycleBreaker.releaseCycleHandle(temp._objectref);\r
155             }\r
156             ;\r
157             return false;\r
158           }\r
159           if (!thcycle) {\r
160             if (!this._objectref.equals(temp._objectref)) {\r
161               org.castor.util.CycleBreaker.releaseCycleHandle(this._objectref);\r
162               org.castor.util.CycleBreaker.releaseCycleHandle(temp._objectref);\r
163               return false;\r
164             }\r
165             org.castor.util.CycleBreaker.releaseCycleHandle(this._objectref);\r
166             org.castor.util.CycleBreaker.releaseCycleHandle(temp._objectref);\r
167           }\r
168         }\r
169       } else if (temp._objectref != null)\r
170         return false;\r
171       if (this._id != null) {\r
172         if (temp._id == null)\r
173           return false;\r
174         if (this._id != temp._id) {\r
175           thcycle = org.castor.util.CycleBreaker.startingToCycle(this._id);\r
176           tmcycle = org.castor.util.CycleBreaker.startingToCycle(temp._id);\r
177           if (thcycle != tmcycle) {\r
178             if (!thcycle) {\r
179               org.castor.util.CycleBreaker.releaseCycleHandle(this._id);\r
180             }\r
181             ;\r
182             if (!tmcycle) {\r
183               org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);\r
184             }\r
185             ;\r
186             return false;\r
187           }\r
188           if (!thcycle) {\r
189             if (!this._id.equals(temp._id)) {\r
190               org.castor.util.CycleBreaker.releaseCycleHandle(this._id);\r
191               org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);\r
192               return false;\r
193             }\r
194             org.castor.util.CycleBreaker.releaseCycleHandle(this._id);\r
195             org.castor.util.CycleBreaker.releaseCycleHandle(temp._id);\r
196           }\r
197         }\r
198       } else if (temp._id != null)\r
199         return false;\r
200       return true;\r
201     }\r
202     return false;\r
203   }\r
204 \r
205   /**\r
206    * Returns the value of field 'compressed'. The field 'compressed' has the\r
207    * following description: true implies data will be decompresses with Zip\r
208    * before presenting to application\r
209    * \r
210    * @return the value of field 'Compressed'.\r
211    */\r
212   public boolean getCompressed() {\r
213     return this._compressed;\r
214   }\r
215 \r
216   /**\r
217    * Returns the value of field 'id'. The field 'id' has the following\r
218    * description: Primary Key for vamsas object referencing\r
219    * \r
220    * \r
221    * @return the value of field 'Id'.\r
222    */\r
223   public java.lang.String getId() {\r
224     return this._id;\r
225   }\r
226 \r
227   /**\r
228    * Returns the value of field 'objectref'. The field 'objectref' has the\r
229    * following description: Object the arbitrary data is associated with\r
230    * \r
231    * \r
232    * @return the value of field 'Objectref'.\r
233    */\r
234   public java.lang.Object getObjectref() {\r
235     return this._objectref;\r
236   }\r
237 \r
238   /**\r
239    * Returns the value of field 'type'. The field 'type' has the following\r
240    * description: Type of arbitrary data - TODO: decide format - use (extended)\r
241    * MIME types ?\r
242    * \r
243    * @return the value of field 'Type'.\r
244    */\r
245   public java.lang.String getType() {\r
246     return this._type;\r
247   }\r
248 \r
249   /**\r
250    * Method hasCompressed.\r
251    * \r
252    * @return true if at least one Compressed has been added\r
253    */\r
254   public boolean hasCompressed() {\r
255     return this._has_compressed;\r
256   }\r
257 \r
258   /**\r
259    * Overrides the java.lang.Object.hashCode method.\r
260    * <p>\r
261    * The following steps came from <b>Effective Java Programming Language\r
262    * Guide</b> by Joshua Bloch, Chapter 3\r
263    * \r
264    * @return a hash code value for the object.\r
265    */\r
266   public int hashCode() {\r
267     int result = super.hashCode();\r
268 \r
269     long tmp;\r
270     result = 37 * result + (_compressed ? 0 : 1);\r
271     if (_type != null && !org.castor.util.CycleBreaker.startingToCycle(_type)) {\r
272       result = 37 * result + _type.hashCode();\r
273       org.castor.util.CycleBreaker.releaseCycleHandle(_type);\r
274     }\r
275     if (_objectref != null\r
276         && !org.castor.util.CycleBreaker.startingToCycle(_objectref)) {\r
277       result = 37 * result + _objectref.hashCode();\r
278       org.castor.util.CycleBreaker.releaseCycleHandle(_objectref);\r
279     }\r
280     if (_id != null && !org.castor.util.CycleBreaker.startingToCycle(_id)) {\r
281       result = 37 * result + _id.hashCode();\r
282       org.castor.util.CycleBreaker.releaseCycleHandle(_id);\r
283     }\r
284 \r
285     return result;\r
286   }\r
287 \r
288   /**\r
289    * Returns the value of field 'compressed'. The field 'compressed' has the\r
290    * following description: true implies data will be decompresses with Zip\r
291    * before presenting to application\r
292    * \r
293    * @return the value of field 'Compressed'.\r
294    */\r
295   public boolean isCompressed() {\r
296     return this._compressed;\r
297   }\r
298 \r
299   /**\r
300    * Method isValid.\r
301    * \r
302    * @return true if this object is valid according to the schema\r
303    */\r
304   public boolean isValid() {\r
305     try {\r
306       validate();\r
307     } catch (org.exolab.castor.xml.ValidationException vex) {\r
308       return false;\r
309     }\r
310     return true;\r
311   }\r
312 \r
313   /**\r
314    * \r
315    * \r
316    * @param out\r
317    * @throws org.exolab.castor.xml.MarshalException\r
318    *           if object is null or if any SAXException is thrown during\r
319    *           marshaling\r
320    * @throws org.exolab.castor.xml.ValidationException\r
321    *           if this object is an invalid instance according to the schema\r
322    */\r
323   public void marshal(final java.io.Writer out)\r
324       throws org.exolab.castor.xml.MarshalException,\r
325       org.exolab.castor.xml.ValidationException {\r
326     Marshaller.marshal(this, out);\r
327   }\r
328 \r
329   /**\r
330    * \r
331    * \r
332    * @param handler\r
333    * @throws java.io.IOException\r
334    *           if an IOException occurs during marshaling\r
335    * @throws org.exolab.castor.xml.ValidationException\r
336    *           if this object is an invalid instance according to the schema\r
337    * @throws org.exolab.castor.xml.MarshalException\r
338    *           if object is null or if any SAXException is thrown during\r
339    *           marshaling\r
340    */\r
341   public void marshal(final org.xml.sax.ContentHandler handler)\r
342       throws java.io.IOException, org.exolab.castor.xml.MarshalException,\r
343       org.exolab.castor.xml.ValidationException {\r
344     Marshaller.marshal(this, handler);\r
345   }\r
346 \r
347   /**\r
348    * Sets the value of field 'compressed'. The field 'compressed' has the\r
349    * following description: true implies data will be decompresses with Zip\r
350    * before presenting to application\r
351    * \r
352    * @param compressed\r
353    *          the value of field 'compressed'.\r
354    */\r
355   public void setCompressed(final boolean compressed) {\r
356     this._compressed = compressed;\r
357     this._has_compressed = true;\r
358   }\r
359 \r
360   /**\r
361    * Sets the value of field 'id'. The field 'id' has the following description:\r
362    * Primary Key for vamsas object referencing\r
363    * \r
364    * \r
365    * @param id\r
366    *          the value of field 'id'.\r
367    */\r
368   public void setId(final java.lang.String id) {\r
369     this._id = id;\r
370   }\r
371 \r
372   /**\r
373    * Sets the value of field 'objectref'. The field 'objectref' has the\r
374    * following description: Object the arbitrary data is associated with\r
375    * \r
376    * \r
377    * @param objectref\r
378    *          the value of field 'objectref'.\r
379    */\r
380   public void setObjectref(final java.lang.Object objectref) {\r
381     this._objectref = objectref;\r
382   }\r
383 \r
384   /**\r
385    * Sets the value of field 'type'. The field 'type' has the following\r
386    * description: Type of arbitrary data - TODO: decide format - use (extended)\r
387    * MIME types ?\r
388    * \r
389    * @param type\r
390    *          the value of field 'type'.\r
391    */\r
392   public void setType(final java.lang.String type) {\r
393     this._type = type;\r
394   }\r
395 \r
396   /**\r
397    * Method unmarshal.\r
398    * \r
399    * @param reader\r
400    * @throws org.exolab.castor.xml.MarshalException\r
401    *           if object is null or if any SAXException is thrown during\r
402    *           marshaling\r
403    * @throws org.exolab.castor.xml.ValidationException\r
404    *           if this object is an invalid instance according to the schema\r
405    * @return the unmarshaled uk.ac.vamsas.objects.core.AppData\r
406    */\r
407   public static uk.ac.vamsas.objects.core.AppData unmarshal(\r
408       final java.io.Reader reader)\r
409       throws org.exolab.castor.xml.MarshalException,\r
410       org.exolab.castor.xml.ValidationException {\r
411     return (uk.ac.vamsas.objects.core.AppData) Unmarshaller.unmarshal(\r
412         uk.ac.vamsas.objects.core.Attachment.class, reader);\r
413   }\r
414 \r
415   /**\r
416    * \r
417    * \r
418    * @throws org.exolab.castor.xml.ValidationException\r
419    *           if this object is an invalid instance according to the schema\r
420    */\r
421   public void validate() throws org.exolab.castor.xml.ValidationException {\r
422     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();\r
423     validator.validate(this);\r
424   }\r
425 \r
426 }\r