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