001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.types;
020:
021: import java.text.NumberFormat;
022:
023: /**
024: * Implementation of the XML Schema type gMonthDay
025: *
026: * @see <a href="http://www.w3.org/TR/xmlschema-2/#gMonthDay">XML Schema 3.2.12</a>
027: */
028: public class MonthDay implements java.io.Serializable {
029:
030: private static final long serialVersionUID = -345189609825249318L;
031:
032: int month;
033: int day;
034: String timezone;
035:
036: /** Constructs a MonthDay with the given values No timezone is specified */
037: public MonthDay(int month, int day) throws NumberFormatException {
038: setValue(month, day);
039: }
040:
041: /**
042: * Constructs a MonthDay with the given values, including a timezone string The timezone is
043: * validated but not used.
044: */
045: public MonthDay(int month, int day, String timezone)
046: throws NumberFormatException {
047: setValue(month, day, timezone);
048: }
049:
050: /** Construct a MonthDay from a String in the format --MM-DD[timezone] */
051: public MonthDay(String source) throws NumberFormatException {
052: if (source.length() < 6) {
053: throw new NumberFormatException();
054: //Messages.getMessage("badMonthDay00"));
055: }
056:
057: if (source.charAt(0) != '-' || source.charAt(1) != '-'
058: || source.charAt(4) != '-') {
059: throw new NumberFormatException();
060: //Messages.getMessage("badMonthDay00"));
061: }
062:
063: setValue(Integer.parseInt(source.substring(2, 4)), Integer
064: .parseInt(source.substring(5, 7)), source.substring(7));
065: }
066:
067: public int getMonth() {
068: return month;
069: }
070:
071: public void setMonth(int month) {
072: // validate month
073: if (month < 1 || month > 12) {
074: throw new NumberFormatException();
075: //Messages.getMessage("badMonthDay00"));
076: }
077: this .month = month;
078: }
079:
080: public int getDay() {
081: return day;
082: }
083:
084: /** Set the day NOTE: if the month isn't set yet, the day isn't validated */
085: public void setDay(int day) {
086: // validate day
087: if (day < 1 || day > 31) {
088: throw new NumberFormatException();
089: //Messages.getMessage("badMonthDay00"));
090: }
091: // 30 days has September... All the rest have 31 (except Feb!)
092: // NOTE: if month isn't set, we don't validate day.
093: if ((month == 2 && day > 29)
094: || ((month == 9 || month == 4 || month == 6 || month == 11) && day > 30)) {
095: throw new NumberFormatException();
096: // Messages.getMessage("badMonthDay00"));
097: }
098: this .day = day;
099: }
100:
101: public String getTimezone() {
102: return timezone;
103: }
104:
105: public void setTimezone(String timezone) {
106: // validate timezone
107: if (timezone != null && timezone.length() > 0) {
108: // Format [+/-]HH:MM
109: if (timezone.charAt(0) == '+'
110: || (timezone.charAt(0) == '-')) {
111: if (timezone.length() != 6
112: || !Character.isDigit(timezone.charAt(1))
113: || !Character.isDigit(timezone.charAt(2))
114: || timezone.charAt(3) != ':'
115: || !Character.isDigit(timezone.charAt(4))
116: || !Character.isDigit(timezone.charAt(5)))
117: throw new NumberFormatException();
118: // Messages.getMessage("badTimezone00"));
119:
120: } else if (!timezone.equals("Z")) {
121: throw new NumberFormatException();
122: //Messages.getMessage("badTimezone00"));
123: }
124: // if we got this far, its good
125: this .timezone = timezone;
126: }
127: }
128:
129: public void setValue(int month, int day, String timezone)
130: throws NumberFormatException {
131: setMonth(month);
132: setDay(day);
133: setTimezone(timezone);
134: }
135:
136: public void setValue(int month, int day)
137: throws NumberFormatException {
138: setMonth(month);
139: setDay(day);
140: }
141:
142: public String toString() {
143: // use NumberFormat to ensure leading zeros
144: NumberFormat nf = NumberFormat.getInstance();
145: nf.setGroupingUsed(false);
146:
147: // month & Day: --MM-DD
148: nf.setMinimumIntegerDigits(2);
149: String s = "--" + nf.format(month) + "-" + nf.format(day);
150:
151: // timezone
152: if (timezone != null) {
153: s = s + timezone;
154: }
155: return s;
156: }
157:
158: public boolean equals(Object obj) {
159: if (!(obj instanceof MonthDay))
160: return false;
161: MonthDay other = (MonthDay) obj;
162: if (this == obj)
163: return true;
164:
165: boolean equals = (this .month == other.month && this .day == other.day);
166: if (timezone != null) {
167: equals = equals && timezone.equals(other.timezone);
168: }
169: return equals;
170: }
171:
172: /**
173: * Return the value of (month + day) XORed with the hashCode of timezone iff one is defined.
174: *
175: * @return an <code>int</code> value
176: */
177: public int hashCode() {
178: return null == timezone ? (month + day) : (month + day)
179: ^ timezone.hashCode();
180: }
181: }
|