Skip to content

Commit

Permalink
Add @Nullable to the parameter of equals(Object) (#2093)
Browse files Browse the repository at this point in the history
Motivation:

`Object.equals(Object)` accepts `null`, but many of our `equals()`
implementations do not have `@Nullable` annotation on their parameters.

Modifications:

- Add `@Nullable` to the parameter of `equals(Object)`

Result:

- Better `null`-related inference
  • Loading branch information
trustin authored Sep 20, 2019
1 parent df3fde1 commit f0d870f
Show file tree
Hide file tree
Showing 59 changed files with 104 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public final class MyClass {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
... usual type check ...
// OK
return name.equals(((MyClass) obj).name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.function.Function;

import javax.annotation.Nullable;

import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;

Expand Down Expand Up @@ -113,7 +115,7 @@ Function<Client<I, O>, Client<I, O>> decorator() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.function.ToIntFunction;

import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -262,7 +264,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ private static void validateWeight(int weight) {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ static final class PoolKey {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.linecorp.armeria.client.circuitbreaker;

import javax.annotation.Nullable;

/**
* An immutable object that stores the count of events.
*/
Expand Down Expand Up @@ -99,7 +101,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.net.IDN;

import javax.annotation.Nullable;

import io.netty.handler.codec.dns.DnsQuestion;
import io.netty.handler.codec.dns.DnsRecordType;

Expand Down Expand Up @@ -56,7 +58,7 @@ public long timeToLive() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.linecorp.armeria.common;

import javax.annotation.Nullable;

/**
* Support APIs for creating well-behaved {@link HttpData} objects. {@link HttpData} generally should extend
* {@link AbstractHttpData} to interact with other {@link HttpData} implementations, via, e.g., {@code equals}.
Expand All @@ -37,7 +39,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof AbstractHttpData)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.Nullable;

import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.server.DefaultServiceRequestContext;

Expand Down Expand Up @@ -195,7 +197,7 @@ public final int hashCode() {
}

@Override
public final boolean equals(Object obj) {
public final boolean equals(@Nullable Object obj) {
return super.equals(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.linecorp.armeria.common;

import javax.annotation.Nullable;

/**
* Directives for HTTP caching mechanisms in requests or responses. Use {@link ServerCacheControl} for
* response-side and {@link ClientCacheControl} for request-side.
Expand Down Expand Up @@ -115,7 +117,7 @@ final StringBuilder newHeaderValueBuffer() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public String asHeaderValue() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (!super.equals(o)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.linecorp.armeria.common;

import javax.annotation.Nullable;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;

Expand Down Expand Up @@ -64,7 +66,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.List;

import javax.annotation.Nullable;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;

Expand Down Expand Up @@ -59,7 +61,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public HttpHeadersBuilder toBuilder() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
return o instanceof HttpHeaders && super.equals(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public RequestHeadersBuilder toBuilder() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
return o instanceof RequestHeaders && super.equals(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ResponseHeadersBuilder toBuilder() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
return o instanceof ResponseHeaders && super.equals(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public final int hashCode() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down Expand Up @@ -1142,7 +1142,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Nullable;

import io.netty.handler.codec.Headers;

final class ImmutableHttpParameters implements HttpParameters {
Expand Down Expand Up @@ -510,7 +512,7 @@ public int hashCode() {

@Override
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
return delegate.equals(obj);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/linecorp/armeria/common/Scheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
return this == obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
return this == obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.Set;

import javax.annotation.Nullable;

import com.google.common.base.Ascii;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -55,7 +57,7 @@ public Entry(String uriText, MediaType primaryMediaType, MediaType... alternativ
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public String asHeaderValue() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (!super.equals(o)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import com.google.common.collect.Iterators;
import com.google.common.math.IntMath;

Expand Down Expand Up @@ -103,7 +105,7 @@ public boolean contains(Object e) {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
return this == o;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Collection;
import java.util.List;

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -207,7 +209,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public String query() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Class<?> type() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.List;

import javax.annotation.Nullable;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -89,7 +91,7 @@ boolean isProxyProtocol() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Loading

0 comments on commit f0d870f

Please sign in to comment.