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