KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > types > Year


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.types;
17
18 import org.apache.axis.utils.Messages;
19
20 import java.text.NumberFormat JavaDoc;
21
22 /**
23  * Implementation of the XML Schema type gYear
24  *
25  * @author Tom Jordahl <tomj@macromedia.com>
26  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#gYear">XML Schema 3.2.11</a>
27  */

28 public class Year implements java.io.Serializable JavaDoc {
29     int year;
30     String JavaDoc timezone = null;
31
32     /**
33      * Constructs a Year with the given values
34      * No timezone is specified
35      */

36     public Year(int year) throws NumberFormatException JavaDoc {
37         setValue(year);
38     }
39
40     /**
41      * Constructs a Year with the given values, including a timezone string
42      * The timezone is validated but not used.
43      */

44     public Year(int year, String JavaDoc timezone) throws NumberFormatException JavaDoc {
45         setValue(year, timezone);
46     }
47
48     /**
49      * Construct a Year from a String in the format [-]CCYY[timezone]
50      */

51     public Year(String JavaDoc source) throws NumberFormatException JavaDoc {
52         int negative = 0;
53
54         if (source.charAt(0) == '-') {
55             negative = 1;
56         }
57         if (source.length() < (4 + negative)) {
58             throw new NumberFormatException JavaDoc(
59                     Messages.getMessage("badYear00"));
60         }
61
62         // calculate how many more than 4 digits (if any) in the year
63
int pos = 4 + negative; // skip minus sign if present
64
while (pos < source.length() && Character.isDigit(source.charAt(pos))) {
65             ++pos;
66         }
67
68         setValue(Integer.parseInt(source.substring(0,pos)),
69                  source.substring(pos));
70     }
71
72     public int getYear() {
73         return year;
74     }
75
76     public void setYear(int year) {
77         // validate year, more than 4 digits are allowed!
78
if (year == 0) {
79             throw new NumberFormatException JavaDoc(
80                     Messages.getMessage("badYear00"));
81         }
82
83         this.year = year;
84     }
85
86     public String JavaDoc getTimezone() {
87         return timezone;
88     }
89
90     public void setTimezone(String JavaDoc timezone) {
91         // validate timezone
92
if (timezone != null && timezone.length() > 0) {
93             // Format [+/-]HH:MM
94
if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) {
95                     if (timezone.length() != 6 ||
96                         !Character.isDigit(timezone.charAt(1)) ||
97                         !Character.isDigit(timezone.charAt(2)) ||
98                         timezone.charAt(3) != ':' ||
99                         !Character.isDigit(timezone.charAt(4)) ||
100                         !Character.isDigit(timezone.charAt(5)))
101                         throw new NumberFormatException JavaDoc(
102                                 Messages.getMessage("badTimezone00"));
103
104             } else if (!timezone.equals("Z")) {
105                 throw new NumberFormatException JavaDoc(
106                         Messages.getMessage("badTimezone00"));
107             }
108             // if we got this far, its good
109
this.timezone = timezone;
110         }
111     }
112
113     public void setValue(int year, String JavaDoc timezone)
114         throws NumberFormatException JavaDoc {
115         setYear(year);
116         setTimezone(timezone);
117     }
118
119     public void setValue(int year) throws NumberFormatException JavaDoc {
120         setYear(year);
121     }
122
123     public String JavaDoc toString() {
124         // use NumberFormat to ensure leading zeros
125
NumberFormat JavaDoc nf = NumberFormat.getInstance();
126         nf.setGroupingUsed(false);
127
128         // year
129
nf.setMinimumIntegerDigits(4);
130         String JavaDoc s = nf.format(year);
131
132         // timezone
133
if (timezone != null) {
134             s = s + timezone;
135         }
136         return s;
137     }
138
139     public boolean equals(Object JavaDoc obj) {
140         if (!(obj instanceof Year)) return false;
141         Year other = (Year) obj;
142         if (obj == null) return false;
143         if (this == obj) return true;
144
145         boolean equals = (this.year == other.year);
146         if (timezone != null) {
147             equals = equals && timezone.equals(other.timezone);
148         }
149         return equals;
150     }
151
152     /**
153      * Return the value of year XORed with the hashCode of timezone
154      * iff one is defined.
155      *
156      * @return an <code>int</code> value
157      */

158     public int hashCode() {
159         return null == timezone ? year : year ^ timezone.hashCode();
160     }
161 }
162
Popular Tags