View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$ Date Author Changes 04/18/2002 Arnaud String constructor 05/24/2001 Arnaud Blandin Created
34   */
35  package org.exolab.castor.types;
36  
37  import java.text.SimpleDateFormat;
38  import java.text.ParseException;
39  
40  /**
41   * Describe an XML schema gDay type.
42   * <p>
43   * The format is defined by W3C XML Schema Recommendation and ISO8601 i.e
44   * <tt>---DD(Z|(+|-)hh:mm)</tt>
45   * 
46   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
47   * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
48   * @version $Revision$
49   */
50  public class GDay extends DateTimeBase {
51  
52    /** SerialVersionUID */
53    private static final long serialVersionUID = 8571596440117087631L;
54  
55    /** The gDay SimpleDateFormat string. */
56    private static final String DAY_FORMAT = "---dd";
57    /** Prefix of any complaint we make. */
58    private static final String BAD_GDAY = "Bad gDay format: ";
59  
60    /**
61     * public only for the generated source code
62     */
63    public GDay() {
64      // Nothing needed
65    }
66  
67    /**
68     * Constructs a XML Schema GDay instance given all the values of the different fields. By default
69     * a GDay is not UTC and is local.
70     * 
71     * @param day
72     */
73    public GDay(short day) {
74      setDay(day);
75    }
76  
77    /**
78     * Constructs a XML Schema GDay instance given all the values of the different fields. By default
79     * a GDay is not UTC and is local.
80     * 
81     * @param day
82     */
83    public GDay(int day) {
84      setDay((short) day);
85    }
86  
87    /**
88     * Constructs a GDay from a string value.
89     * 
90     * @param gday the string representation of the GDay to instantiate
91     * @throws ParseException a parse exception is thrown if the string to parse does not follow the
92     *         rigth format (see the description of this class)
93     */
94    public GDay(String gday) throws ParseException {
95      parseGDayInternal(gday, this);
96    }
97  
98    /**
99     * Sets all the fields by reading the values in an array
100    * <p>
101    * if a Time Zone is specificied it has to be set by using
102    * {@link DateTimeBase#setZone(short, short) setZone}.
103    *
104    * @param values an array of shorts with the values the array is supposed to be of length 1 and
105    *        ordered like the following:
106    *        <ul>
107    *        <li>Day</li>
108    *        </ul>
109    */
110   public void setValues(short[] values) {
111     if (values.length != 1) {
112       throw new IllegalArgumentException("GDay#setValues: not the right number of values");
113     }
114     this.setDay(values[0]);
115   }
116 
117   /**
118    * Returns an array of short with all the fields that describe this gDay type.
119    * <p>
120    * Note:the time zone is not included.
121    * 
122    * @return an array of short with all the fields that describe this Date type.
123    */
124   public short[] getValues() {
125     short[] result = new short[1];
126     result[0] = this.getDay();
127     return result;
128   } // getValues
129 
130   /**
131    * converts this GDay into a local java Date.
132    * 
133    * @return a local date representing this Date.
134    */
135   public java.util.Date toDate() {
136     SimpleDateFormat df = new SimpleDateFormat(DAY_FORMAT);
137     setDateFormatTimeZone(df);
138 
139     java.util.Date date = null;
140     try {
141       date = df.parse(this.toString());
142     } catch (ParseException e) {
143       // this can't happen since toString() should return the proper string format
144       e.printStackTrace();
145       return null;
146     }
147 
148     return date;
149   } // toDate()
150 
151   /**
152    * convert this GDay to a string The format is defined by W3C XML Schema recommendation and
153    * ISO8601 i.e ---DD(Z|(+|-)hh:mm)
154    * 
155    * @return a string representing this Date
156    */
157   public String toString() {
158     StringBuffer result = new StringBuffer("---");
159 
160     if ((this.getDay() / 10) == 0) {
161       result.append(0);
162     }
163     result.append(this.getDay());
164 
165     appendTimeZoneString(result);
166 
167     return result.toString();
168   } // toString
169 
170   /**
171    * parse a String and convert it into an java.lang.Object
172    * 
173    * @param str the string to parse
174    * @return an Object represented by the string
175    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
176    *         rigth format (see the description of this class)
177    */
178   public static Object parse(String str) throws ParseException {
179     return parseGDay(str);
180   }
181 
182   /**
183    * parse a String and convert it into a GDay.
184    * 
185    * @param str the string to parse
186    * @return the Date represented by the string
187    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
188    *         rigth format (see the description of this class)
189    */
190   public static GDay parseGDay(String str) throws ParseException {
191     GDay result = new GDay();
192     return parseGDayInternal(str, result);
193   }
194 
195   private static GDay parseGDayInternal(String str, GDay result) throws ParseException {
196     if (str == null) {
197       throw new IllegalArgumentException("The string to be parsed must not be null.");
198     }
199 
200     if (result == null) {
201       result = new GDay();
202     }
203 
204     char[] chars = str.toCharArray();
205     if (chars[0] != '-' || chars[1] != '-' || chars[2] != '-') {
206       throw new ParseException(
207           BAD_GDAY + str + "\nA gDay must follow the pattern ---DD(Z|((+|-)hh:mm)).", 0);
208     }
209 
210     int idx = 2;
211     idx = parseDay(str, result, chars, idx, BAD_GDAY);
212     parseTimeZone(str, result, chars, idx, BAD_GDAY);
213 
214     return result;
215   } // parse
216 
217   /////////////////////////// DISALLOWED METHODS ///////////////////////////
218 
219   public boolean hasIsNegative() {
220     return false;
221   }
222 
223   public boolean isNegative() {
224     String err = "org.exolab.castor.types.GDay does not have a 'negative' field.";
225     throw new UnsupportedOperationException(err);
226   }
227 
228   public void setNegative() {
229     String err = "org.exolab.castor.types.GDay cannot be negative.";
230     throw new UnsupportedOperationException(err);
231   }
232 
233   public boolean hasCentury() {
234     return false;
235   }
236 
237   public short getCentury() {
238     String err = "org.exolab.castor.types.GDay does not have a Century field.";
239     throw new UnsupportedOperationException(err);
240   }
241 
242   public void setCentury(short century) {
243     String err = "org.exolab.castor.types.GDay does not have a Century field.";
244     throw new UnsupportedOperationException(err);
245   }
246 
247   public boolean hasYear() {
248     return false;
249   }
250 
251   public short getYear() {
252     String err = "org.exolab.castor.types.GDay does not have a Year field.";
253     throw new UnsupportedOperationException(err);
254   }
255 
256   public void setYear(short year) {
257     String err = "org.exolab.castor.types.GDay does not have a Year field.";
258     throw new UnsupportedOperationException(err);
259   }
260 
261   public boolean hasMonth() {
262     return false;
263   }
264 
265   public short getMonth() {
266     String err = "org.exolab.castor.types.GDay does not have a Month field.";
267     throw new UnsupportedOperationException(err);
268   }
269 
270   public void setMonth(short month) {
271     String err = "org.exolab.castor.types.GDay does not have a Month field.";
272     throw new UnsupportedOperationException(err);
273   }
274 
275   public boolean hasHour() {
276     return false;
277   }
278 
279   public short getHour() {
280     String err = "org.exolab.castor.types.GDay does not have an Hour field.";
281     throw new UnsupportedOperationException(err);
282   }
283 
284   public void setHour(short hour) {
285     String err = "org.exolab.castor.types.GDay does not have an Hour field.";
286     throw new UnsupportedOperationException(err);
287   }
288 
289   public boolean hasMinute() {
290     return false;
291   }
292 
293   public short getMinute() {
294     String err = "org.exolab.castor.types.GDay does not have a Minute field.";
295     throw new UnsupportedOperationException(err);
296   }
297 
298   public void setMinute(short minute) {
299     String err = "org.exolab.castor.types.GDay does not have a Minute field.";
300     throw new UnsupportedOperationException(err);
301   }
302 
303   public boolean hasSeconds() {
304     return false;
305   }
306 
307   public short getSeconds() {
308     String err = "org.exolab.castor.types.GDay does not have a Seconds field.";
309     throw new UnsupportedOperationException(err);
310   }
311 
312   public void setSecond(short second) {
313     String err = "org.exolab.castor.types.GDay does not have a Seconds field.";
314     throw new UnsupportedOperationException(err);
315   }
316 
317   public boolean hasMilli() {
318     return false;
319   }
320 
321   public short getMilli() {
322     String err = "org.exolab.castor.types.GDay does not have a Milliseconds field.";
323     throw new UnsupportedOperationException(err);
324   }
325 
326   public void setMilliSecond(short millisecond) {
327     String err = "org.exolab.castor.types.GDay does not have a Milliseconds field.";
328     throw new UnsupportedOperationException(err);
329   }
330 
331 }