1 30 31 package org.apache.commons.httpclient; 32 33 71 public class HttpVersion implements Comparable { 72 73 74 private int major = 0; 75 76 77 private int minor = 0; 78 79 80 public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9); 81 82 83 public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0); 84 85 86 public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1); 87 88 96 public HttpVersion(int major, int minor) { 97 if (major < 0) { 98 throw new IllegalArgumentException ("HTTP major version number may not be negative"); 99 } 100 this.major = major; 101 if (minor < 0) { 102 throw new IllegalArgumentException ("HTTP minor version number may not be negative"); 103 } 104 this.minor = minor; 105 } 106 107 112 public int getMajor() { 113 return major; 114 } 115 116 121 public int getMinor() { 122 return minor; 123 } 124 125 128 public int hashCode() { 129 return this.major * 100000 + this.minor; 130 } 131 132 135 public boolean equals(Object obj) { 136 if (this == obj) { 137 return true; 138 } 139 if (!(obj instanceof HttpVersion)) { 140 return false; 141 } 142 return equals((HttpVersion)obj); 143 } 144 145 153 public int compareTo(HttpVersion anotherVer) { 154 if (anotherVer == null) { 155 throw new IllegalArgumentException ("Version parameter may not be null"); 156 } 157 int delta = getMajor() - anotherVer.getMajor(); 158 if (delta == 0) { 159 delta = getMinor() - anotherVer.getMinor(); 160 } 161 return delta; 162 } 163 164 167 public int compareTo(Object o) { 168 return compareTo((HttpVersion)o); 169 } 170 171 177 public boolean equals(HttpVersion version) { 178 return compareTo(version) == 0; 179 } 180 181 187 public boolean greaterEquals(HttpVersion version) { 188 return compareTo(version) >= 0; 189 } 190 191 197 public boolean lessEquals(HttpVersion version) { 198 return compareTo(version) <= 0; 199 } 200 201 204 public String toString() { 205 StringBuffer buffer = new StringBuffer (); 206 buffer.append("HTTP/"); 207 buffer.append(this.major); 208 buffer.append('.'); 209 buffer.append(this.minor); 210 return buffer.toString(); 211 } 212 213 220 public static HttpVersion parse(final String s) throws ProtocolException { 221 if (s == null) { 222 throw new IllegalArgumentException ("String may not be null"); 223 } 224 if (!s.startsWith("HTTP/")) { 225 throw new ProtocolException("Invalid HTTP version string: " + s); 226 } 227 int major, minor; 228 229 int i1 = "HTTP/".length(); 230 int i2 = s.indexOf(".", i1); 231 if (i2 == -1) { 232 throw new ProtocolException("Invalid HTTP version number: " + s); 233 } 234 try { 235 major = Integer.parseInt(s.substring(i1, i2)); 236 } catch (NumberFormatException e) { 237 throw new ProtocolException("Invalid HTTP major version number: " + s); 238 } 239 i1 = i2 + 1; 240 i2 = s.length(); 241 try { 242 minor = Integer.parseInt(s.substring(i1, i2)); 243 } catch (NumberFormatException e) { 244 throw new ProtocolException("Invalid HTTP minor version number: " + s); 245 } 246 return new HttpVersion(major, minor); 247 } 248 249 } 250 | Popular Tags |