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 gMonth
025: *
026: * @see <a href="http://www.w3.org/TR/xmlschema-2/#gMonth">XML Schema 3.2.14</a>
027: */
028: public class Month implements java.io.Serializable {
029:
030: private static final long serialVersionUID = -7469265802807262347L;
031:
032: int month;
033: String timezone;
034:
035: /** Constructs a Month with the given values No timezone is specified */
036: public Month(int month) throws NumberFormatException {
037: setValue(month);
038: }
039:
040: /**
041: * Constructs a Month with the given values, including a timezone string The timezone is
042: * validated but not used.
043: */
044: public Month(int month, String timezone)
045: throws NumberFormatException {
046: setValue(month, timezone);
047: }
048:
049: /** Construct a Month from a String in the format --MM--[timezone] */
050: public Month(String source) throws NumberFormatException {
051: if (source.length() < (6)) {
052: throw new NumberFormatException();
053: // Messages.getMessage("badMonth00"));
054: }
055:
056: if (source.charAt(0) != '-' || source.charAt(1) != '-'
057: || source.charAt(4) != '-' || source.charAt(5) != '-') {
058: throw new NumberFormatException();
059: //Messages.getMessage("badMonth00"));
060: }
061:
062: setValue(Integer.parseInt(source.substring(2, 4)), source
063: .substring(6));
064: }
065:
066: public int getMonth() {
067: return month;
068: }
069:
070: public void setMonth(int month) {
071: // validate month
072: if (month < 1 || month > 12) {
073: throw new NumberFormatException();
074: // Messages.getMessage("badMonth00"));
075: }
076: this .month = month;
077: }
078:
079: public String getTimezone() {
080: return timezone;
081: }
082:
083: public void setTimezone(String timezone) {
084: // validate timezone
085: if (timezone != null && timezone.length() > 0) {
086: // Format [+/-]HH:MM
087: if (timezone.charAt(0) == '+'
088: || (timezone.charAt(0) == '-')) {
089: if (timezone.length() != 6
090: || !Character.isDigit(timezone.charAt(1))
091: || !Character.isDigit(timezone.charAt(2))
092: || timezone.charAt(3) != ':'
093: || !Character.isDigit(timezone.charAt(4))
094: || !Character.isDigit(timezone.charAt(5)))
095: throw new NumberFormatException();
096: //Messages.getMessage("badTimezone00"));
097:
098: } else if (!timezone.equals("Z")) {
099: throw new NumberFormatException();
100: //Messages.getMessage("badTimezone00"));
101: }
102: // if we got this far, its good
103: this .timezone = timezone;
104: }
105: }
106:
107: public void setValue(int month, String timezone)
108: throws NumberFormatException {
109: setMonth(month);
110: setTimezone(timezone);
111: }
112:
113: public void setValue(int month) throws NumberFormatException {
114: setMonth(month);
115: }
116:
117: public String toString() {
118: // use NumberFormat to ensure leading zeros
119: NumberFormat nf = NumberFormat.getInstance();
120: nf.setGroupingUsed(false);
121:
122: // month
123: nf.setMinimumIntegerDigits(2);
124: String s = "--" + nf.format(month) + "--";
125:
126: // timezone
127: if (timezone != null) {
128: s = s + timezone;
129: }
130: return s;
131: }
132:
133: public boolean equals(Object obj) {
134: if (!(obj instanceof Month))
135: return false;
136: Month other = (Month) obj;
137: if (this == obj)
138: return true;
139:
140: boolean equals = (this .month == other.month);
141: if (timezone != null) {
142: equals = equals && timezone.equals(other.timezone);
143: }
144: return equals;
145: }
146:
147: /**
148: * Return the value of month XORed with the hashCode of timezone iff one is defined.
149: *
150: * @return an <code>int</code> value
151: */
152: public int hashCode() {
153: return null == timezone ? month : month ^ timezone.hashCode();
154: }
155: }
|