KickJava   Java API By Example, From Geeks To Geeks.

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


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 gMonthDay
24  *
25  * @author Tom Jordahl <tomj@macromedia.com>
26  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#gMonthDay">XML Schema 3.2.12</a>
27  */

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

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

46     public MonthDay(int month, int day, String JavaDoc timezone)
47         throws NumberFormatException JavaDoc {
48         setValue(month, day, timezone);
49     }
50
51     /**
52      * Construct a MonthDay from a String in the format --MM-DD[timezone]
53      */

54     public MonthDay(String JavaDoc source) throws NumberFormatException JavaDoc {
55         if (source.length() < 6) {
56             throw new NumberFormatException JavaDoc(
57                     Messages.getMessage("badMonthDay00"));
58         }
59
60         if (source.charAt(0) != '-' ||
61             source.charAt(1) != '-' ||
62             source.charAt(4) != '-' ) {
63             throw new NumberFormatException JavaDoc(
64                     Messages.getMessage("badMonthDay00"));
65         }
66
67         setValue(Integer.parseInt(source.substring(2,4)),
68                  Integer.parseInt(source.substring(5,7)),
69                  source.substring(7));
70     }
71
72     public int getMonth() {
73         return month;
74     }
75
76     public void setMonth(int month) {
77         // validate month
78
if (month < 1 || month > 12) {
79             throw new NumberFormatException JavaDoc(
80                     Messages.getMessage("badMonthDay00"));
81         }
82         this.month = month;
83     }
84
85     public int getDay() {
86         return day;
87     }
88
89     /**
90      * Set the day
91      * NOTE: if the month isn't set yet, the day isn't validated
92      */

93     public void setDay(int day) {
94         // validate day
95
if (day < 1 || day > 31) {
96             throw new NumberFormatException JavaDoc(
97                     Messages.getMessage("badMonthDay00"));
98         }
99         // 30 days has September... All the rest have 31 (except Feb!)
100
// NOTE: if month isn't set, we don't validate day.
101
if ((month == 2 && day > 29) ||
102            ((month == 9 || month == 4 || month == 6 || month == 11) && day > 30)) {
103             throw new NumberFormatException JavaDoc(
104                     Messages.getMessage("badMonthDay00"));
105         }
106         this.day = day;
107     }
108
109     public String JavaDoc getTimezone() {
110         return timezone;
111     }
112
113     public void setTimezone(String JavaDoc timezone) {
114         // validate timezone
115
if (timezone != null && timezone.length() > 0) {
116             // Format [+/-]HH:MM
117
if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) {
118                     if (timezone.length() != 6 ||
119                         !Character.isDigit(timezone.charAt(1)) ||
120                         !Character.isDigit(timezone.charAt(2)) ||
121                         timezone.charAt(3) != ':' ||
122                         !Character.isDigit(timezone.charAt(4)) ||
123                         !Character.isDigit(timezone.charAt(5)))
124                         throw new NumberFormatException JavaDoc(
125                                 Messages.getMessage("badTimezone00"));
126
127             } else if (!timezone.equals("Z")) {
128                 throw new NumberFormatException JavaDoc(
129                         Messages.getMessage("badTimezone00"));
130             }
131             // if we got this far, its good
132
this.timezone = timezone;
133         }
134     }
135
136     public void setValue(int month, int day, String JavaDoc timezone)
137         throws NumberFormatException JavaDoc {
138         setMonth(month);
139         setDay(day);
140         setTimezone(timezone);
141     }
142
143     public void setValue(int month, int day) throws NumberFormatException JavaDoc {
144         setMonth(month);
145         setDay(day);
146     }
147
148     public String JavaDoc toString() {
149         // use NumberFormat to ensure leading zeros
150
NumberFormat JavaDoc nf = NumberFormat.getInstance();
151         nf.setGroupingUsed(false);
152
153         // month & Day: --MM-DD
154
nf.setMinimumIntegerDigits(2);
155         String JavaDoc s = "--" + nf.format(month) + "-" + nf.format(day);
156
157         // timezone
158
if (timezone != null) {
159             s = s + timezone;
160         }
161         return s;
162     }
163
164     public boolean equals(Object JavaDoc obj) {
165         if (!(obj instanceof MonthDay)) return false;
166         MonthDay other = (MonthDay) obj;
167         if (obj == null) return false;
168         if (this == obj) return true;
169
170         boolean equals = (this.month == other.month && this.day == other.day);
171         if (timezone != null) {
172             equals = equals && timezone.equals(other.timezone);
173         }
174         return equals;
175     }
176
177     /**
178      * Return the value of (month + day) XORed with the hashCode of
179      * timezone iff one is defined.
180      *
181      * @return an <code>int</code> value
182      */

183     public int hashCode() {
184         return null == timezone
185             ? (month + day)
186             : (month + day) ^ timezone.hashCode();
187     }
188 }
189
Popular Tags