KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

52     public Day(String JavaDoc source) throws NumberFormatException JavaDoc {
53         if (source.length() < 5) {
54             throw new NumberFormatException JavaDoc(
55                     Messages.getMessage("badDay00"));
56         }
57
58         if (source.charAt(0) != '-' ||
59             source.charAt(1) != '-' ||
60             source.charAt(2) != '-' ) {
61             throw new NumberFormatException JavaDoc(
62                     Messages.getMessage("badDay00"));
63         }
64
65         setValue(Integer.parseInt(source.substring(3,5)),
66                  source.substring(5));
67     }
68
69     public int getDay() {
70         return day;
71     }
72
73     /**
74      * Set the day
75      */

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

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