Take advantage of invocation chaining in HttpHeaders

This commit is contained in:
Trustin Lee 2013-01-16 23:56:51 +09:00
parent 34820511ff
commit 540bc99549
2 changed files with 47 additions and 20 deletions

View File

@ -87,17 +87,18 @@ public class DefaultHttpHeaders extends HttpHeaders {
}
@Override
public void add(final String name, final Object value) {
public HttpHeaders add(final String name, final Object value) {
validateHeaderName0(name);
String strVal = toString(value);
validateHeaderValue(strVal);
int h = hash(name);
int i = index(h);
add0(h, i, name, strVal);
return this;
}
@Override
public void add(String name, Iterable<?> values) {
public HttpHeaders add(String name, Iterable<?> values) {
validateHeaderName0(name);
int h = hash(name);
int i = index(h);
@ -106,6 +107,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
validateHeaderValue(vstr);
add0(h, i, name, vstr);
}
return this;
}
private void add0(int h, int i, final String name, final String value) {
@ -120,13 +122,14 @@ public class DefaultHttpHeaders extends HttpHeaders {
}
@Override
public void remove(final String name) {
public HttpHeaders remove(final String name) {
if (name == null) {
throw new NullPointerException("name");
}
int h = hash(name);
int i = index(h);
remove0(h, i, name);
return this;
}
private void remove0(int h, int i, String name) {
@ -166,7 +169,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
}
@Override
public void set(final String name, final Object value) {
public HttpHeaders set(final String name, final Object value) {
validateHeaderName0(name);
String strVal = toString(value);
validateHeaderValue(strVal);
@ -174,10 +177,11 @@ public class DefaultHttpHeaders extends HttpHeaders {
int i = index(h);
remove0(h, i, name);
add0(h, i, name, strVal);
return this;
}
@Override
public void set(final String name, final Iterable<?> values) {
public HttpHeaders set(final String name, final Iterable<?> values) {
if (values == null) {
throw new NullPointerException("values");
}
@ -196,14 +200,17 @@ public class DefaultHttpHeaders extends HttpHeaders {
validateHeaderValue(strVal);
add0(h, i, name, strVal);
}
return this;
}
@Override
public void clear() {
public HttpHeaders clear() {
for (int i = 0; i < entries.length; i ++) {
entries[i] = null;
}
head.before = head.after = head;
return this;
}
@Override

View File

@ -66,32 +66,32 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
}
@Override
public void add(String name, Object value) {
public HttpHeaders add(String name, Object value) {
throw new UnsupportedOperationException("read only");
}
@Override
public void add(String name, Iterable<?> values) {
public HttpHeaders add(String name, Iterable<?> values) {
throw new UnsupportedOperationException("read only");
}
@Override
public void set(String name, Object value) {
public HttpHeaders set(String name, Object value) {
throw new UnsupportedOperationException("read only");
}
@Override
public void set(String name, Iterable<?> values) {
public HttpHeaders set(String name, Iterable<?> values) {
throw new UnsupportedOperationException("read only");
}
@Override
public void remove(String name) {
public HttpHeaders remove(String name) {
throw new UnsupportedOperationException("read only");
}
@Override
public void clear() {
public HttpHeaders clear() {
throw new UnsupportedOperationException("read only");
}
@ -1230,8 +1230,10 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*
* @param name The name of the header being added
* @param value The value of the header being added
*
* @return {@code this}
*/
public abstract void add(String name, Object value);
public abstract HttpHeaders add(String name, Object value);
/**
* Adds a new header with the specified name and values.
@ -1248,16 +1250,23 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*
* @param name The name of the headers being set
* @param values The values of the headers being set
* @return {@code this}
*/
public abstract void add(String name, Iterable<?> values);
public abstract HttpHeaders add(String name, Iterable<?> values);
public void add(HttpHeaders headers) {
/**
* Adds all header entries of the specified {@code headers}.
*
* @return {@code this}
*/
public HttpHeaders add(HttpHeaders headers) {
if (headers == null) {
throw new NullPointerException("headers");
}
for (Map.Entry<String, String> e: headers) {
add(e.getKey(), e.getValue());
}
return this;
}
/**
@ -1271,8 +1280,9 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*
* @param name The name of the header being set
* @param value The value of the header being set
* @return {@code this}
*/
public abstract void set(String name, Object value);
public abstract HttpHeaders set(String name, Object value);
/**
* Sets a header with the specified name and values.
@ -1291,10 +1301,16 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*
* @param name The name of the headers being set
* @param values The values of the headers being set
* @return {@code this}
*/
public abstract void set(String name, Iterable<?> values);
public abstract HttpHeaders set(String name, Iterable<?> values);
public void set(HttpHeaders headers) {
/**
* Cleans the current header entries and copies all header entries of the specified {@code headers}.
*
* @return {@code this}
*/
public HttpHeaders set(HttpHeaders headers) {
if (headers == null) {
throw new NullPointerException("headers");
}
@ -1302,17 +1318,21 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
for (Map.Entry<String, String> e: headers) {
add(e.getKey(), e.getValue());
}
return this;
}
/**
* Removes the header with the specified name.
*
* @param name The name of the header to remove
* @return {@code this}
*/
public abstract void remove(String name);
public abstract HttpHeaders remove(String name);
/**
* Removes all headers from this {@link HttpMessage}.
*
* @return {@code this}
*/
public abstract void clear();
public abstract HttpHeaders clear();
}