KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

52     public YearMonth(String JavaDoc source) throws NumberFormatException JavaDoc {
53         int negative = 0;
54
55         if (source.charAt(0) == '-') {
56             negative = 1;
57         }
58         if (source.length() < (7 + negative)) {
59             throw new NumberFormatException JavaDoc(
60                     Messages.getMessage("badYearMonth00"));
61         }
62
63         // look for first '-'
64
int pos = source.substring(negative).indexOf('-');
65         if (pos < 0) {
66             throw new NumberFormatException JavaDoc(
67                     Messages.getMessage("badYearMonth00"));
68         }
69         if (negative > 0) pos++; //adjust index for orginal string
70

71         setValue(Integer.parseInt(source.substring(0,pos)),
72                  Integer.parseInt(source.substring(pos+1,pos+3)),
73                  source.substring(pos+3));
74     }
75
76     public int getYear() {
77         return year;
78     }
79
80     public void setYear(int year) {
81         // validate year, more than 4 digits are allowed!
82
if (year == 0) {
83             throw new NumberFormatException JavaDoc(
84                     Messages.getMessage("badYearMonth00"));
85         }
86
87         this.year = year;
88     }
89
90     public int getMonth() {
91         return month;
92     }
93
94     public void setMonth(int month) {
95         // validate month
96
if (month < 1 || month > 12) {
97             throw new NumberFormatException JavaDoc(
98                     Messages.getMessage("badYearMonth00"));
99         }
100         this.month = month;
101     }
102
103     public String JavaDoc getTimezone() {
104         return timezone;
105     }
106
107     public void setTimezone(String JavaDoc timezone) {
108         // validate timezone
109
if (timezone != null && timezone.length() > 0) {
110             // Format [+/-]HH:MM
111
if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) {
112                     if (timezone.length() != 6 ||
113                         !Character.isDigit(timezone.charAt(1)) ||
114                         !Character.isDigit(timezone.charAt(2)) ||
115                         timezone.charAt(3) != ':' ||
116                         !Character.isDigit(timezone.charAt(4)) ||
117                         !Character.isDigit(timezone.charAt(5)))
118                         throw new NumberFormatException JavaDoc(
119                                 Messages.getMessage("badTimezone00"));
120
121             } else if (!timezone.equals("Z")) {
122                 throw new NumberFormatException JavaDoc(
123                         Messages.getMessage("badTimezone00"));
124             }
125             // if we got this far, its good
126
this.timezone = timezone;
127         }
128     }
129
130     public void setValue(int year, int month, String JavaDoc timezone) throws NumberFormatException JavaDoc {
131         setYear(year);
132         setMonth(month);
133         setTimezone(timezone);
134     }
135
136     public void setValue(int year, int month) throws NumberFormatException JavaDoc {
137         setYear(year);
138         setMonth(month);
139     }
140
141     public String JavaDoc toString() {
142         // use NumberFormat to ensure leading zeros
143
NumberFormat JavaDoc nf = NumberFormat.getInstance();
144         nf.setGroupingUsed(false);
145
146         // year
147
nf.setMinimumIntegerDigits(4);
148         String JavaDoc s = nf.format(year) + "-";
149
150         // month
151
nf.setMinimumIntegerDigits(2);
152         s += nf.format(month);
153
154         // timezone
155
if (timezone != null) {
156             s = s + timezone;
157         }
158         return s;
159     }
160
161     public boolean equals(Object JavaDoc obj) {
162         if (!(obj instanceof YearMonth)) return false;
163         YearMonth other = (YearMonth) obj;
164         if (obj == null) return false;
165         if (this == obj) return true;
166
167         boolean equals = (this.year == other.year && this.month == other.month);
168         if (timezone != null) {
169             equals = equals && timezone.equals(other.timezone);
170         }
171         return equals;
172     }
173
174     /**
175      * Return the value of (month + year) XORed with the hashCode of
176      * timezone iff one is defined.
177      *
178      * @return an <code>int</code> value
179      */

180     public int hashCode() {
181         return null == timezone
182             ? (month + year)
183             : (month + year) ^ timezone.hashCode();
184     }
185 }
186
Popular Tags