Code clean up

This commit is contained in:
Trustin Lee 2009-02-11 09:24:28 +00:00
parent 3f009da453
commit 3acbe08eac
2 changed files with 61 additions and 35 deletions

View File

@ -94,39 +94,52 @@ public class HttpMethod implements Comparable<HttpMethod> {
methodMap.put(CONNECT.toString(), CONNECT); methodMap.put(CONNECT.toString(), CONNECT);
} }
public static HttpMethod valueOf(String method) { public static HttpMethod valueOf(String name) {
if (method == null) { if (name == null) {
throw new NullPointerException("method"); throw new NullPointerException("name");
} }
method = method.trim().toUpperCase();
HttpMethod result = methodMap.get(method); name = name.trim().toUpperCase();
if (name.length() == 0) {
throw new IllegalArgumentException("empty name");
}
HttpMethod result = methodMap.get(name);
if (result != null) { if (result != null) {
return result; return result;
} else { } else {
return new HttpMethod(method); return new HttpMethod(name);
} }
} }
private final String method; private final String name;
public HttpMethod(String method) { public HttpMethod(String name) {
if (method == null) { if (name == null) {
throw new NullPointerException("method"); throw new NullPointerException("name");
} }
method = method.trim().toUpperCase(); name = name.trim().toUpperCase();
if (name.length() == 0) {
if (method.length() == 0) { throw new IllegalArgumentException("empty name");
throw new IllegalArgumentException("empty method");
} }
this.method = method; for (int i = 0; i < name.length(); i ++) {
if (Character.isISOControl(name.charAt(i))) {
throw new IllegalArgumentException("control character in name");
}
}
this.name = name;
}
public String getName() {
return name;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return method.hashCode(); return getName().hashCode();
} }
@Override @Override
@ -136,15 +149,15 @@ public class HttpMethod implements Comparable<HttpMethod> {
} }
HttpMethod that = (HttpMethod) o; HttpMethod that = (HttpMethod) o;
return method.equals(that.method); return getName().equals(that.getName());
} }
@Override @Override
public String toString() { public String toString() {
return method; return getName();
} }
public int compareTo(HttpMethod o) { public int compareTo(HttpMethod o) {
return method.compareTo(o.method); return getName().compareTo(o.getName());
} }
} }

View File

@ -42,36 +42,45 @@ public class HttpVersion implements Comparable<HttpVersion> {
public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0); public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0);
public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1); public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1);
public static HttpVersion valueOf(String value) { public static HttpVersion valueOf(String text) {
value = value.toUpperCase(); if (text == null) {
if (value.equals("HTTP/1.1")) { throw new NullPointerException("text");
}
text = text.trim().toUpperCase();
if (text.equals("HTTP/1.1")) {
return HTTP_1_1; return HTTP_1_1;
} }
if (value.equals("HTTP/1.0")) { if (text.equals("HTTP/1.0")) {
return HTTP_1_0; return HTTP_1_0;
} }
return new HttpVersion(value); return new HttpVersion(text);
} }
private final String protocolName; private final String protocolName;
private final int majorVersion; private final int majorVersion;
private final int minorVersion; private final int minorVersion;
private final String string; private final String text;
public HttpVersion(String value) { public HttpVersion(String text) {
if (value == null) { if (text == null) {
throw new NullPointerException("value"); throw new NullPointerException("text");
} }
Matcher m = VERSION_PATTERN.matcher(value); text = text.trim().toUpperCase();
if (text.length() == 0) {
throw new IllegalArgumentException("empty text");
}
Matcher m = VERSION_PATTERN.matcher(text);
if (!m.matches()) { if (!m.matches()) {
throw new IllegalArgumentException("invalid version format: " + value); throw new IllegalArgumentException("invalid version format: " + text);
} }
protocolName = m.group(1); protocolName = m.group(1);
majorVersion = Integer.parseInt(m.group(2)); majorVersion = Integer.parseInt(m.group(2));
minorVersion = Integer.parseInt(m.group(3)); minorVersion = Integer.parseInt(m.group(3));
string = protocolName + '/' + majorVersion + '.' + minorVersion; this.text = protocolName + '/' + majorVersion + '.' + minorVersion;
} }
public HttpVersion( public HttpVersion(
@ -86,8 +95,8 @@ public class HttpVersion implements Comparable<HttpVersion> {
} }
for (int i = 0; i < protocolName.length(); i ++) { for (int i = 0; i < protocolName.length(); i ++) {
if (Character.isWhitespace(protocolName.charAt(i))) { if (Character.isISOControl(protocolName.charAt(i))) {
throw new IllegalArgumentException("whitespace in protocolName"); throw new IllegalArgumentException("control character in protocolName");
} }
} }
@ -101,7 +110,7 @@ public class HttpVersion implements Comparable<HttpVersion> {
this.protocolName = protocolName; this.protocolName = protocolName;
this.majorVersion = majorVersion; this.majorVersion = majorVersion;
this.minorVersion = minorVersion; this.minorVersion = minorVersion;
string = protocolName + '/' + majorVersion + '.' + minorVersion; text = protocolName + '/' + majorVersion + '.' + minorVersion;
} }
public String getProtocolName() { public String getProtocolName() {
@ -116,9 +125,13 @@ public class HttpVersion implements Comparable<HttpVersion> {
return minorVersion; return minorVersion;
} }
public String getText() {
return text;
}
@Override @Override
public String toString() { public String toString() {
return string; return getText();
} }
@Override @Override