KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

154     public int hashCode() {
155         return null == timezone ? month : month ^ timezone.hashCode();
156     }
157 }
158
Popular Tags