mirror of https://github.com/grpc/grpc-java.git
Use instance equality for Context.Key
This allows applications to limit the visibility of values simply by not exposing the Key instance being used.
This commit is contained in:
parent
7e3d0fe9cc
commit
4a427b7ac9
|
@ -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 <T> Key<T> key(String name) {
|
||||
return new Key<T>(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 <T> Key<T> keyWithDefault(String name, T defaultValue) {
|
||||
return new Key<T>(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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue