From 4a427b7ac9570b989dd34ddab09fef33cf5b8f3c Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Sat, 23 Jan 2016 09:24:00 -0800 Subject: [PATCH] Use instance equality for Context.Key This allows applications to limit the visibility of values simply by not exposing the Key instance being used. --- core/src/main/java/io/grpc/Context.java | 25 ++++----------------- core/src/test/java/io/grpc/ContextTest.java | 4 ++-- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/io/grpc/Context.java b/core/src/main/java/io/grpc/Context.java index 1873795a59..1a770fac2f 100644 --- a/core/src/main/java/io/grpc/Context.java +++ b/core/src/main/java/io/grpc/Context.java @@ -160,14 +160,16 @@ public class Context { }; /** - * Create a {@link Key} with the given name. + * Create a {@link Key} with the given debug name. Multiple different keys may have the same name; + * the name is intended for debugging purposes and does not impact behavior. */ public static Key key(String name) { return new Key(name); } /** - * Create a {@link Key} with the given name and default value. + * Create a {@link Key} with the given debug name and default value. Multiple different keys may + * have the same name; the name is intended for debugging purposes and does not impact behavior. */ public static Key keyWithDefault(String name, T defaultValue) { return new Key(name, defaultValue); @@ -768,25 +770,6 @@ public class Context { return value == null ? defaultValue : value; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Key key = (Key) o; - - return key.name.equals(this.name); - } - - @Override - public int hashCode() { - return name.hashCode(); - } - @Override public String toString() { return name; diff --git a/core/src/test/java/io/grpc/ContextTest.java b/core/src/test/java/io/grpc/ContextTest.java index 65710d1646..19853bd076 100644 --- a/core/src/test/java/io/grpc/ContextTest.java +++ b/core/src/test/java/io/grpc/ContextTest.java @@ -711,12 +711,12 @@ public class ContextTest { @Test public void testKeyEqualsHashCode() { assertTrue(PET.equals(PET)); - assertTrue(PET.equals(Context.key("pet"))); + assertFalse(PET.equals(Context.key("pet"))); assertFalse(PET.equals(FOOD)); assertFalse(PET.equals("pet")); assertFalse(PET.equals(null)); - assertEquals(PET.hashCode(), Context.key("pet").hashCode()); + assertEquals(PET.hashCode(), PET.hashCode()); } private static class QueuedExecutor implements Executor {