Browse Source

Adds toString to Targets. Normalizes equals/hashCode.

pull/151/head
Adrian Cole 10 years ago
parent
commit
81781694a9
  1. 23
      core/src/main/java/feign/ReflectiveFeign.java
  2. 32
      core/src/main/java/feign/Target.java
  3. 41
      core/src/test/java/feign/FeignTest.java
  4. 27
      ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java

23
core/src/main/java/feign/ReflectiveFeign.java

@ -83,27 +83,28 @@ public class ReflectiveFeign extends Feign { @@ -83,27 +83,28 @@ public class ReflectiveFeign extends Feign {
} catch (IllegalArgumentException e) {
return false;
}
}
if ("hashCode".equals(method.getName())) {
} else if ("hashCode".equals(method.getName())) {
return hashCode();
} else if ("toString".equals(method.getName())) {
return toString();
}
return dispatch.get(method).invoke(args);
}
@Override public int hashCode() {
return target.hashCode();
}
@Override public boolean equals(Object other) {
if (other instanceof FeignInvocationHandler) {
FeignInvocationHandler that = (FeignInvocationHandler) other;
return this.target.equals(that.target);
@Override public boolean equals(Object obj) {
if (obj instanceof FeignInvocationHandler) {
FeignInvocationHandler other = (FeignInvocationHandler) obj;
return target.equals(other.target);
}
return false;
}
@Override public int hashCode() {
return target.hashCode();
}
@Override public String toString() {
return "target(" + target + ")";
return target.toString();
}
}

32
core/src/main/java/feign/Target.java

@ -15,8 +15,6 @@ @@ -15,8 +15,6 @@
*/
package feign;
import java.util.Arrays;
import static feign.Util.checkNotNull;
import static feign.Util.emptyToNull;
@ -95,19 +93,29 @@ public interface Target<T> { @@ -95,19 +93,29 @@ public interface Target<T> {
return input.request();
}
@Override public boolean equals(Object obj) {
if (obj instanceof HardCodedTarget) {
HardCodedTarget<?> other = (HardCodedTarget) obj;
return type.equals(other.type)
&& name.equals(other.name)
&& url.equals(other.url);
}
return false;
}
@Override public int hashCode() {
return Arrays.hashCode(new Object[]{type, name, url});
int result = 17;
result = 31 * result + type.hashCode();
result = 31 * result + name.hashCode();
result = 31 * result + url.hashCode();
return result;
}
@Override public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (HardCodedTarget.class != obj.getClass())
return false;
HardCodedTarget<?> that = HardCodedTarget.class.cast(obj);
return this.type.equals(that.type) && this.name.equals(that.name) && this.url.equals(that.url);
@Override public String toString() {
if (name.equals(url)) {
return "HardCodedTarget(type=" + type.getSimpleName() + ", url=" + url + ")";
}
return "HardCodedTarget(type=" + type.getSimpleName() + ", name=" + name + ", url=" + url + ")";
}
}
}

41
core/src/test/java/feign/FeignTest.java

@ -24,6 +24,7 @@ import com.google.mockwebserver.RecordedRequest; @@ -24,6 +24,7 @@ import com.google.mockwebserver.RecordedRequest;
import com.google.mockwebserver.SocketPolicy;
import dagger.Module;
import dagger.Provides;
import feign.Target.HardCodedTarget;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
@ -46,6 +47,7 @@ import static dagger.Provides.Type.SET; @@ -46,6 +47,7 @@ import static dagger.Provides.Type.SET;
import static feign.Util.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
@ -489,19 +491,38 @@ public class FeignTest { @@ -489,19 +491,38 @@ public class FeignTest {
}
}
@Test public void equalsAndHashCodeWork() {
TestInterface i1 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module());
TestInterface i2 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module());
TestInterface i3 = Feign.create(TestInterface.class, "http://localhost:8888", new TestInterface.Module());
OtherTestInterface i4 = Feign.create(OtherTestInterface.class, "http://localhost:8080", new TestInterface.Module());
assertTrue(i1.equals(i1));
assertTrue(i1.equals(i2));
assertFalse(i1.equals(i3));
assertFalse(i1.equals(i4));
@Test public void equalsHashCodeAndToStringWork() {
Target<TestInterface> t1 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
Target<TestInterface> t2 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
Target<OtherTestInterface> t3 =
new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http://localhost:8080");
TestInterface i1 = Feign.builder().target(t1);
TestInterface i2 = Feign.builder().target(t1);
TestInterface i3 = Feign.builder().target(t2);
OtherTestInterface i4 = Feign.builder().target(t3);
assertEquals(i1, i1);
assertEquals(i1, i2);
assertNotEquals(i1, i3);
assertNotEquals(i1, i4);
assertEquals(i1.hashCode(), i1.hashCode());
assertEquals(i1.hashCode(), i2.hashCode());
assertNotEquals(i1.hashCode(), i3.hashCode());
assertNotEquals(i1.hashCode(), i4.hashCode());
assertEquals(i1.hashCode(), t1.hashCode());
assertEquals(i3.hashCode(), t2.hashCode());
assertEquals(i4.hashCode(), t3.hashCode());
assertEquals(i1.toString(), i1.toString());
assertEquals(i1.toString(), i2.toString());
assertNotEquals(i1.toString(), i3.toString());
assertNotEquals(i1.toString(), i4.toString());
assertEquals(i1.toString(), t1.toString());
assertEquals(i3.toString(), t2.toString());
assertEquals(i4.toString(), t3.toString());
}
@Test public void decodeLogicSupportsByteArray() throws Exception {

27
ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package feign.ribbon;
import com.google.common.base.Objects;
import com.netflix.loadbalancer.AbstractLoadBalancer;
import com.netflix.loadbalancer.Server;
@ -25,7 +24,6 @@ import feign.Request; @@ -25,7 +24,6 @@ import feign.Request;
import feign.RequestTemplate;
import feign.Target;
import static com.google.common.base.Objects.equal;
import static com.netflix.client.ClientFactory.getNamedLoadBalancer;
import static feign.Util.checkNotNull;
import static java.lang.String.format;
@ -99,18 +97,23 @@ public class LoadBalancingTarget<T> implements Target<T> { @@ -99,18 +97,23 @@ public class LoadBalancingTarget<T> implements Target<T> {
}
}
@Override public boolean equals(Object obj) {
if (obj instanceof LoadBalancingTarget) {
LoadBalancingTarget<?> other = (LoadBalancingTarget) obj;
return type.equals(other.type)
&& name.equals(other.name);
}
return false;
}
@Override public int hashCode() {
return Objects.hashCode(type, name);
int result = 17;
result = 31 * result + type.hashCode();
result = 31 * result + name.hashCode();
return result;
}
@Override public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (LoadBalancingTarget.class != obj.getClass())
return false;
LoadBalancingTarget<?> that = LoadBalancingTarget.class.cast(obj);
return equal(this.type, that.type) && equal(this.name, that.name);
@Override public String toString() {
return "LoadBalancingTarget(type=" + type.getSimpleName() + ", name=" + name + ")";
}
}

Loading…
Cancel
Save