Misc clean up

Motivation:
IntelliJ issues several warnings.

Modifications:

* `ClientCookieDecoder` and `ServerCookieDecoder`:
  * `nameEnd`, `valueBegin` and `valueEnd` don't need to be initialized
  * `keyValLoop` loop doesn't been to be labelled, as it's the most inner one (same thing for labelled breaks)
  * Remove `if (i != headerLen)` as condition is always true
* `ClientCookieEncoder` javadoc still mention old logic
* `DefaultCookie`, `ServerCookieEncoder` and `DefaultHttpHeaders` use ternary ops that can be turned into simple boolean ones
* `DefaultHeaders` uses a for(int) loop over an array. It can be turned into a foreach one as javac doesn't allocate an iterator to iterate over arrays
* `DefaultHttp2Headers` and `AbstractByteBuf` `equal` can be turned into a single boolean statement
Result:

Cleaner code
This commit is contained in:
Stephane Landelle 2016-11-21 22:18:47 +01:00 committed by Scott Mitchell
parent 4e2530c171
commit ba95c401a7
9 changed files with 74 additions and 96 deletions

View File

@ -1308,13 +1308,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof ByteBuf) {
return ByteBufUtil.equals(this, (ByteBuf) o);
}
return false;
return this == o || (o instanceof ByteBuf && ByteBufUtil.equals(this, (ByteBuf) o));
}
@Override

View File

@ -313,10 +313,8 @@ public class DefaultHttpHeaders extends HttpHeaders {
@Override
public boolean equals(Object o) {
if (!(o instanceof DefaultHttpHeaders)) {
return false;
}
return headers.equals(((DefaultHttpHeaders) o).headers, CASE_SENSITIVE_HASHER);
return o instanceof DefaultHttpHeaders
&& headers.equals(((DefaultHttpHeaders) o).headers, CASE_SENSITIVE_HASHER);
}
@Override

View File

@ -82,19 +82,17 @@ public final class ClientCookieDecoder extends CookieDecoder {
}
int nameBegin = i;
int nameEnd = i;
int valueBegin = -1;
int valueEnd = -1;
if (i != headerLen) {
keyValLoop: for (;;) {
int nameEnd;
int valueBegin;
int valueEnd;
for (;;) {
char curChar = header.charAt(i);
if (curChar == ';') {
// NAME; (no value till ';')
nameEnd = i;
valueBegin = valueEnd = -1;
break keyValLoop;
break;
} else if (curChar == '=') {
// NAME=VALUE
@ -103,14 +101,14 @@ public final class ClientCookieDecoder extends CookieDecoder {
if (i == headerLen) {
// NAME= (empty value, i.e. nothing after '=')
valueBegin = valueEnd = 0;
break keyValLoop;
break;
}
valueBegin = i;
// NAME=VALUE;
int semiPos = header.indexOf(';', i);
valueEnd = i = semiPos > 0 ? semiPos : headerLen;
break keyValLoop;
break;
} else {
i++;
}
@ -122,7 +120,6 @@ public final class ClientCookieDecoder extends CookieDecoder {
break;
}
}
}
if (valueEnd > 0 && header.charAt(valueEnd - 1) == ',') {
// old multiple cookies separator, skipping it
@ -158,7 +155,7 @@ public final class ClientCookieDecoder extends CookieDecoder {
private boolean secure;
private boolean httpOnly;
public CookieBuilder(DefaultCookie cookie, String header) {
CookieBuilder(DefaultCookie cookie, String header) {
this.cookie = cookie;
this.header = header;
}
@ -177,7 +174,7 @@ public final class ClientCookieDecoder extends CookieDecoder {
return Long.MIN_VALUE;
}
public Cookie cookie() {
Cookie cookie() {
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setMaxAge(mergeMaxAgeAndExpires());
@ -199,7 +196,7 @@ public final class ClientCookieDecoder extends CookieDecoder {
* @param valueEnd
* where the value ends in the header
*/
public void appendAttribute(int keyStart, int keyEnd, int valueStart, int valueEnd) {
void appendAttribute(int keyStart, int keyEnd, int valueStart, int valueEnd) {
int length = keyEnd - keyStart;
if (length == 4) {
@ -209,7 +206,7 @@ public final class ClientCookieDecoder extends CookieDecoder {
} else if (length == 7) {
parse7(keyStart, valueStart, valueEnd);
} else if (length == 8) {
parse8(keyStart, valueStart, valueEnd);
parse8(keyStart);
}
}
@ -244,7 +241,7 @@ public final class ClientCookieDecoder extends CookieDecoder {
}
}
private void parse8(int nameStart, int valueStart, int valueEnd) {
private void parse8(int nameStart) {
if (header.regionMatches(true, nameStart, CookieHeaderNames.HTTPONLY, 0, 8)) {
httpOnly = true;
}

View File

@ -34,9 +34,6 @@ import java.util.List;
* A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used client side, so
* only name=value pairs are sent.
*
* User-Agents are not supposed to interpret cookies, so, if present, {@link Cookie#rawValue()} will be used.
* Otherwise, {@link Cookie#value()} will be used unquoted.
*
* Note that multiple cookies are supposed to be sent at once in a single "Cookie" header.
*
* <pre>

View File

@ -153,8 +153,6 @@ public class DefaultCookie implements Cookie {
if (that.domain() != null) {
return false;
}
} else if (that.domain() == null) {
return false;
} else {
return domain().equalsIgnoreCase(that.domain());
}

View File

@ -96,19 +96,18 @@ public final class ServerCookieDecoder extends CookieDecoder {
}
int nameBegin = i;
int nameEnd = i;
int valueBegin = -1;
int valueEnd = -1;
int nameEnd;
int valueBegin;
int valueEnd;
if (i != headerLen) {
keyValLoop: for (;;) {
for (;;) {
char curChar = header.charAt(i);
if (curChar == ';') {
// NAME; (no value till ';')
nameEnd = i;
valueBegin = valueEnd = -1;
break keyValLoop;
break;
} else if (curChar == '=') {
// NAME=VALUE
@ -117,14 +116,14 @@ public final class ServerCookieDecoder extends CookieDecoder {
if (i == headerLen) {
// NAME= (empty value, i.e. nothing after '=')
valueBegin = valueEnd = 0;
break keyValLoop;
break;
}
valueBegin = i;
// NAME=VALUE;
int semiPos = header.indexOf(';', i);
valueEnd = i = semiPos > 0 ? semiPos : headerLen;
break keyValLoop;
break;
} else {
i++;
}
@ -136,7 +135,6 @@ public final class ServerCookieDecoder extends CookieDecoder {
break;
}
}
}
if (rfc2965Style && (header.regionMatches(nameBegin, RFC2965_PATH, 0, RFC2965_PATH.length()) ||
header.regionMatches(nameBegin, RFC2965_DOMAIN, 0, RFC2965_DOMAIN.length()) ||

View File

@ -213,7 +213,7 @@ public final class ServerCookieEncoder extends CookieEncoder {
Map<String, Integer> nameToIndex = strict && cookiesIt.hasNext() ? new HashMap<String, Integer>() : null;
int i = 0;
encoded.add(encode(firstCookie));
boolean hasDupdName = nameToIndex != null ? nameToIndex.put(firstCookie.name(), i++) != null : false;
boolean hasDupdName = nameToIndex != null && nameToIndex.put(firstCookie.name(), i++) != null;
while (cookiesIt.hasNext()) {
Cookie c = cookiesIt.next();
encoded.add(encode(c));

View File

@ -120,11 +120,7 @@ public class DefaultHttp2Headers
@Override
public boolean equals(Object o) {
if (!(o instanceof Http2Headers)) {
return false;
}
return equals((Http2Headers) o, CASE_SENSITIVE_HASHER);
return o instanceof Http2Headers && equals((Http2Headers) o, CASE_SENSITIVE_HASHER);
}
@Override

View File

@ -328,8 +328,8 @@ public class DefaultHeaders<K, V, T extends Headers<K, V, T>> implements Headers
@Override
public T addObject(K name, Object... values) {
for (int i = 0; i < values.length; i++) {
addObject(name, values[i]);
for (Object value: values) {
addObject(name, value);
}
return thisT();
}