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