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 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 gYear type.
42   * <p>
43   * The format is defined by W3C XML Schema Recommendation and ISO8601 i.e
44   * <tt>(-)CCYY(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 GYear extends DateTimeBase {
51  
52    /** SerialVersionUID */
53    private static final long serialVersionUID = -8977039151222106864L;
54  
55    /** The gYear SimpleDateFormat string. */
56    private static final String YEAR_FORMAT = "yyyy";
57    /** Prefix of any complaint we make. */
58    private static final String BAD_GYEAR = "Bad gYear format: ";
59  
60    /**
61     * public only for the generated source code
62     */
63    public GYear() {
64      // Nothing to do
65    }
66  
67    /**
68     * Instantiates a new gYear given the value of the century and year.
69     * 
70     * @param century the month value
71     * @param year the year value
72     */
73    public GYear(short century, short year) {
74      setCentury(century);
75      setYear(year);
76    }
77  
78    /**
79     * Instantiates a new gYear given the value of the year. Here, the year is the normal
80     * representation of a year, that is, a four-digit value.
81     * 
82     * @param year the year value
83     */
84    public GYear(int year) {
85      short century = (short) (year / 100);
86      year = year % 100;
87      setCentury(century);
88      setYear((short) year);
89    }
90  
91    /**
92     * Constructs a XML Schema GYear instance given all the values of the different fields. By default
93     * a GYear is not UTC and is local.
94     * 
95     * @param values an array of shorts that represent the different fields of Time.
96     */
97    public GYear(short[] values) {
98      this.setValues(values);
99    }
100 
101   /**
102    * Construct a GYear from a string value
103    * 
104    * @param gyear the string representation of the GYear to instantiate
105    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
106    *         rigth format (see the description of this class)
107    */
108   public GYear(String gyear) throws ParseException {
109     parseGYearInternal(gyear, this);
110   }
111 
112   /**
113    * Sets all the fields by reading the values in an array
114    * <p>
115    * if a Time Zone is specificied it has to be set by using
116    * {@link DateTimeBase#setZone(short, short) setZone}.
117    * 
118    * @param values an array of shorts with the values the array is supposed to be of length 2 and
119    *        ordered like the following:
120    *        <ul>
121    *        <li>century</li>
122    *        <li>year</li>
123    *        </ul>
124    */
125   public void setValues(short[] values) {
126     if (values.length != 2) {
127       throw new IllegalArgumentException("GYear#setValues: not the right number of values");
128     }
129     this.setCentury(values[0]);
130     this.setYear(values[1]);
131   }
132 
133   /**
134    * Returns an array of short with all the fields that describe this gYear type.
135    * <p>
136    * Note:the time zone is not included.
137    * 
138    * @return an array of short with all the fields that describe this Date type.
139    */
140   public short[] getValues() {
141     short[] result = new short[2];
142     result[0] = this.getCentury();
143     result[1] = this.getYear();
144     return result;
145   } // getValues
146 
147   /**
148    * converts this gYear into a local java Date.
149    * 
150    * @return a local date representing this Date.
151    */
152   public java.util.Date toDate() {
153     SimpleDateFormat df = new SimpleDateFormat(YEAR_FORMAT);
154     setDateFormatTimeZone(df);
155 
156     java.util.Date date = null;
157     try {
158       date = df.parse(this.toString());
159     } catch (ParseException e) {
160       // this can't happen since toString() should return the proper string format
161       e.printStackTrace();
162       return null;
163     }
164 
165     return date;
166   } // toDate()
167 
168   /**
169    * convert this gYear to a string The format is defined by W3C XML Schema recommendation and
170    * ISO8601 i.e (+|-)CCYY-MM(Z|(+|-)hh:mm)
171    * 
172    * @return a string representing this Date
173    */
174   public String toString() {
175     StringBuffer result = new StringBuffer();
176     if (isNegative()) {
177       result.append('-');
178     }
179 
180     if ((this.getCentury() / 10) == 0) {
181       result.append(0);
182     }
183     result.append(this.getCentury());
184 
185     if ((this.getYear() / 10) == 0) {
186       result.append(0);
187     }
188     result.append(this.getYear());
189 
190     appendTimeZoneString(result);
191 
192     return result.toString();
193   }// toString
194 
195   /**
196    * parse a String and convert it into an java.lang.Object
197    * 
198    * @param str the string to parse
199    * @return an Object represented by the string
200    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
201    *         rigth format (see the description of this class)
202    */
203   public static Object parse(String str) throws ParseException {
204     return parseGYear(str);
205   }
206 
207   /**
208    * parse a String and convert it into a gYear.
209    * 
210    * @param str the string to parse
211    * @return the Date represented by the string
212    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
213    *         rigth format (see the description of this class)
214    */
215   public static GYear parseGYear(String str) throws ParseException {
216     return parseGYearInternal(str, null);
217   }
218 
219   private static GYear parseGYearInternal(String str, GYear result) throws ParseException {
220     if (str == null) {
221       throw new IllegalArgumentException("The string to be parsed must not be null.");
222     }
223 
224     if (result == null) {
225       result = new GYear();
226     }
227 
228     char[] chars = str.toCharArray();
229 
230     int idx = 0;
231 
232     // Negative?
233     if (chars[idx] == '-') {
234       result.setNegative();
235       idx++;
236     }
237 
238     // Century
239     if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])
240         || !Character.isDigit(chars[idx + 2]) || !Character.isDigit(chars[idx + 3])) {
241       throw new ParseException(
242           BAD_GYEAR + str + "\nA gYear must follow the pattern CCYY(Z|((+|-)hh:mm)).", idx);
243     }
244 
245     short value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
246     short value2 = (short) ((chars[idx + 2] - '0') * 10 + (chars[idx + 3] - '0'));
247     if (value1 == 0 && value2 == 0) {
248       throw new ParseException(BAD_GYEAR + str + "\n'0000' is not allowed as a year.", idx);
249     }
250 
251     result.setCentury(value1);
252     result.setYear(value2);
253 
254     idx += 4;
255 
256     parseTimeZone(str, result, chars, idx, BAD_GYEAR);
257 
258     return result;
259   }// parse
260 
261   /////////////////////////// DISALLOWED METHODS ///////////////////////////
262 
263   public boolean hasMonth() {
264     return false;
265   }
266 
267   public short getMonth() {
268     String err = "org.exolab.castor.types.GYear does not have a Month field.";
269     throw new UnsupportedOperationException(err);
270   }
271 
272   public void setMonth(short year) {
273     String err = "org.exolab.castor.types.GYear does not have a Month field.";
274     throw new UnsupportedOperationException(err);
275   }
276 
277   public boolean hasDay() {
278     return false;
279   }
280 
281   public short getDay() {
282     String err = "org.exolab.castor.types.GYear does not have a Day field.";
283     throw new UnsupportedOperationException(err);
284   }
285 
286   public void setDay(short month) {
287     String err = "org.exolab.castor.types.GYear does not have a Day field.";
288     throw new UnsupportedOperationException(err);
289   }
290 
291   public boolean hasHour() {
292     return false;
293   }
294 
295   public short getHour() {
296     String err = "org.exolab.castor.types.GYear does not have an Hour field.";
297     throw new UnsupportedOperationException(err);
298   }
299 
300   public void setHour(short hour) {
301     String err = "org.exolab.castor.types.GYear does not have an Hour field.";
302     throw new UnsupportedOperationException(err);
303   }
304 
305   public boolean hasMinute() {
306     return false;
307   }
308 
309   public short getMinute() {
310     String err = "org.exolab.castor.types.GYear does not have a Minute field.";
311     throw new UnsupportedOperationException(err);
312   }
313 
314   public void setMinute(short minute) {
315     String err = "org.exolab.castor.types.GYear does not have a Minute field.";
316     throw new UnsupportedOperationException(err);
317   }
318 
319   public boolean hasSeconds() {
320     return false;
321   }
322 
323   public short getSeconds() {
324     String err = "org.exolab.castor.types.GYear does not have a Seconds field.";
325     throw new UnsupportedOperationException(err);
326   }
327 
328   public void setSecond(short second) {
329     String err = "org.exolab.castor.types.GYear does not have a Seconds field.";
330     throw new UnsupportedOperationException(err);
331   }
332 
333   public boolean hasMilli() {
334     return false;
335   }
336 
337   public short getMilli() {
338     String err = "org.exolab.castor.types.GYear does not have a Milliseconds field.";
339     throw new UnsupportedOperationException(err);
340   }
341 
342   public void setMilliSecond(short millisecond) {
343     String err = "org.exolab.castor.types.GYear does not have a Milliseconds field.";
344     throw new UnsupportedOperationException(err);
345   }
346 
347 }