1 16 package org.apache.axis.types; 17 18 import org.apache.axis.utils.Messages; 19 20 import java.text.NumberFormat ; 21 22 28 public class Year implements java.io.Serializable { 29 int year; 30 String timezone = null; 31 32 36 public Year(int year) throws NumberFormatException { 37 setValue(year); 38 } 39 40 44 public Year(int year, String timezone) throws NumberFormatException { 45 setValue(year, timezone); 46 } 47 48 51 public Year(String source) throws NumberFormatException { 52 int negative = 0; 53 54 if (source.charAt(0) == '-') { 55 negative = 1; 56 } 57 if (source.length() < (4 + negative)) { 58 throw new NumberFormatException ( 59 Messages.getMessage("badYear00")); 60 } 61 62 int pos = 4 + negative; while (pos < source.length() && Character.isDigit(source.charAt(pos))) { 65 ++pos; 66 } 67 68 setValue(Integer.parseInt(source.substring(0,pos)), 69 source.substring(pos)); 70 } 71 72 public int getYear() { 73 return year; 74 } 75 76 public void setYear(int year) { 77 if (year == 0) { 79 throw new NumberFormatException ( 80 Messages.getMessage("badYear00")); 81 } 82 83 this.year = year; 84 } 85 86 public String getTimezone() { 87 return timezone; 88 } 89 90 public void setTimezone(String timezone) { 91 if (timezone != null && timezone.length() > 0) { 93 if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) { 95 if (timezone.length() != 6 || 96 !Character.isDigit(timezone.charAt(1)) || 97 !Character.isDigit(timezone.charAt(2)) || 98 timezone.charAt(3) != ':' || 99 !Character.isDigit(timezone.charAt(4)) || 100 !Character.isDigit(timezone.charAt(5))) 101 throw new NumberFormatException ( 102 Messages.getMessage("badTimezone00")); 103 104 } else if (!timezone.equals("Z")) { 105 throw new NumberFormatException ( 106 Messages.getMessage("badTimezone00")); 107 } 108 this.timezone = timezone; 110 } 111 } 112 113 public void setValue(int year, String timezone) 114 throws NumberFormatException { 115 setYear(year); 116 setTimezone(timezone); 117 } 118 119 public void setValue(int year) throws NumberFormatException { 120 setYear(year); 121 } 122 123 public String toString() { 124 NumberFormat nf = NumberFormat.getInstance(); 126 nf.setGroupingUsed(false); 127 128 nf.setMinimumIntegerDigits(4); 130 String s = nf.format(year); 131 132 if (timezone != null) { 134 s = s + timezone; 135 } 136 return s; 137 } 138 139 public boolean equals(Object obj) { 140 if (!(obj instanceof Year)) return false; 141 Year other = (Year) obj; 142 if (obj == null) return false; 143 if (this == obj) return true; 144 145 boolean equals = (this.year == other.year); 146 if (timezone != null) { 147 equals = equals && timezone.equals(other.timezone); 148 } 149 return equals; 150 } 151 152 158 public int hashCode() { 159 return null == timezone ? year : year ^ timezone.hashCode(); 160 } 161 } 162 | Popular Tags |