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);
}
public static HttpMethod valueOf(String method) {
if (method == null) {
throw new NullPointerException("method");
public static HttpMethod valueOf(String name) {
if (name == null) {
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) {
return result;
} else {
return new HttpMethod(method);
return new HttpMethod(name);
}
}
private final String method;
private final String name;
public HttpMethod(String method) {
if (method == null) {
throw new NullPointerException("method");
public HttpMethod(String name) {
if (name == null) {
throw new NullPointerException("name");
}
method = method.trim().toUpperCase();
if (method.length() == 0) {
throw new IllegalArgumentException("empty method");
name = name.trim().toUpperCase();
if (name.length() == 0) {
throw new IllegalArgumentException("empty name");
}
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
public int hashCode() {
return method.hashCode();
return getName().hashCode();
}
@Override
@ -136,15 +149,15 @@ public class HttpMethod implements Comparable<HttpMethod> {
}
HttpMethod that = (HttpMethod) o;
return method.equals(that.method);
return getName().equals(that.getName());
}
@Override
public String toString() {
return method;
return getName();
}
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_1 = new HttpVersion("HTTP", 1, 1);
public static HttpVersion valueOf(String value) {
value = value.toUpperCase();
if (value.equals("HTTP/1.1")) {
public static HttpVersion valueOf(String text) {
if (text == null) {
throw new NullPointerException("text");
}
text = text.trim().toUpperCase();
if (text.equals("HTTP/1.1")) {
return HTTP_1_1;
}
if (value.equals("HTTP/1.0")) {
if (text.equals("HTTP/1.0")) {
return HTTP_1_0;
}
return new HttpVersion(value);
return new HttpVersion(text);
}
private final String protocolName;
private final int majorVersion;
private final int minorVersion;
private final String string;
private final String text;
public HttpVersion(String value) {
if (value == null) {
throw new NullPointerException("value");
public HttpVersion(String text) {
if (text == null) {
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()) {
throw new IllegalArgumentException("invalid version format: " + value);
throw new IllegalArgumentException("invalid version format: " + text);
}
protocolName = m.group(1);
majorVersion = Integer.parseInt(m.group(2));
minorVersion = Integer.parseInt(m.group(3));
string = protocolName + '/' + majorVersion + '.' + minorVersion;
this.text = protocolName + '/' + majorVersion + '.' + minorVersion;
}
public HttpVersion(
@ -86,8 +95,8 @@ public class HttpVersion implements Comparable<HttpVersion> {
}
for (int i = 0; i < protocolName.length(); i ++) {
if (Character.isWhitespace(protocolName.charAt(i))) {
throw new IllegalArgumentException("whitespace in protocolName");
if (Character.isISOControl(protocolName.charAt(i))) {
throw new IllegalArgumentException("control character in protocolName");
}
}
@ -101,7 +110,7 @@ public class HttpVersion implements Comparable<HttpVersion> {
this.protocolName = protocolName;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
string = protocolName + '/' + majorVersion + '.' + minorVersion;
text = protocolName + '/' + majorVersion + '.' + minorVersion;
}
public String getProtocolName() {
@ -116,9 +125,13 @@ public class HttpVersion implements Comparable<HttpVersion> {
return minorVersion;
}
public String getText() {
return text;
}
@Override
public String toString() {
return string;
return getText();
}
@Override