Add HttpHeaders.contains(name, value, ...) to save creation of LinkedList for simple checks

This commit is contained in:
Norman Maurer 2013-10-11 21:28:02 +02:00
parent b72761e99c
commit beb6d37603
2 changed files with 57 additions and 19 deletions

View File

@ -348,6 +348,32 @@ public class DefaultHttpHeaders extends HttpHeaders {
return head == head.after;
}
@Override
public boolean contains(String name, String value, boolean ignoreCaseValue) {
if (name == null) {
throw new NullPointerException("name");
}
int h = hash(name);
int i = index(h);
HeaderEntry e = entries[i];
while (e != null) {
if (e.hash == h && eq(name, e.key)) {
if (ignoreCaseValue) {
if (e.value.equalsIgnoreCase(value)) {
return true;
}
} else {
if (e.value.equals(value)) {
return true;
}
}
}
e = e.next;
}
return false;
}
@Override
public Set<String> names() {

View File

@ -980,12 +980,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
}
// Multiple 'Expect' headers. Search through them.
for (String v: message.headers().getAll(Names.EXPECT)) {
if (Values.CONTINUE.equalsIgnoreCase(v)) {
return true;
}
}
return false;
return message.headers().contains(Names.EXPECT, Values.CONTINUE, true);
}
/**
@ -1131,17 +1126,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
* @return True if transfer encoding is chunked, otherwise false
*/
public static boolean isTransferEncodingChunked(HttpMessage message) {
List<String> transferEncodingHeaders = message.headers().getAll(Names.TRANSFER_ENCODING);
if (transferEncodingHeaders.isEmpty()) {
return false;
}
for (String value: transferEncodingHeaders) {
if (value.equalsIgnoreCase(Values.CHUNKED)) {
return true;
}
}
return false;
return message.headers().contains(Names.TRANSFER_ENCODING, Values.CHUNKED, true);
}
public static void removeTransferEncodingChunked(HttpMessage m) {
@ -1169,8 +1154,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
}
public static boolean isContentLengthSet(HttpMessage m) {
List<String> contentLength = m.headers().getAll(Names.CONTENT_LENGTH);
return !contentLength.isEmpty();
return m.headers().contains(Names.CONTENT_LENGTH);
}
protected HttpHeaders() { }
@ -1336,4 +1320,32 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
* @return {@code this}
*/
public abstract HttpHeaders clear();
/**
* Returns {@code true} if a header with the name and value exists.
*
* @param name the headername
* @param value the value
* @param ignoreCaseValue {@code true} if case should be ignored
* @return contains {@code true} if it contains it {@code false} otherwise
*/
public boolean contains(String name, String value, boolean ignoreCaseValue) {
List<String> values = getAll(name);
if (values.isEmpty()) {
return false;
}
for (String v: values) {
if (ignoreCaseValue) {
if (v.equalsIgnoreCase(value)) {
return true;
}
} else {
if (v.equals(value)) {
return true;
}
}
}
return false;
}
}