From bc5bf9784d6497bb6b4d78bea34d7d49423bb2f7 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Wed, 20 May 2015 10:49:45 +0200 Subject: [PATCH] Cleanup: Remove generics need in ContextAndHeaderHolder Generics were only needed for setting a header, that returned the object being set (most likely the request), but none of the other methods did this. --- .../common/ContextAndHeaderHolder.java | 5 ++--- .../org/elasticsearch/common/HasHeaders.java | 2 +- .../percolator/PercolateContext.java | 14 ++++++-------- .../search/internal/DefaultSearchContext.java | 4 ++-- .../search/internal/FilteredSearchContext.java | 4 ++-- .../transport/TransportMessage.java | 11 ++--------- .../elasticsearch/test/TestSearchContext.java | 4 +--- .../transport/TransportMessageTests.java | 16 ++++++++++++++++ 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/ContextAndHeaderHolder.java b/src/main/java/org/elasticsearch/common/ContextAndHeaderHolder.java index 338ea6b683d..9a3140dba1a 100644 --- a/src/main/java/org/elasticsearch/common/ContextAndHeaderHolder.java +++ b/src/main/java/org/elasticsearch/common/ContextAndHeaderHolder.java @@ -31,7 +31,7 @@ import java.util.Set; /** * */ -public class ContextAndHeaderHolder implements HasContextAndHeaders { +public class ContextAndHeaderHolder implements HasContextAndHeaders { private ObjectObjectHashMap context; protected Map headers; @@ -113,12 +113,11 @@ public class ContextAndHeaderHolder implements HasContextAndHeaders { @SuppressWarnings("unchecked") @Override - public final T putHeader(String key, Object value) { + public final void putHeader(String key, Object value) { if (headers == null) { headers = new HashMap<>(); } headers.put(key, value); - return (T) this; } @SuppressWarnings("unchecked") diff --git a/src/main/java/org/elasticsearch/common/HasHeaders.java b/src/main/java/org/elasticsearch/common/HasHeaders.java index b296362c81c..ab3a7da643e 100644 --- a/src/main/java/org/elasticsearch/common/HasHeaders.java +++ b/src/main/java/org/elasticsearch/common/HasHeaders.java @@ -26,7 +26,7 @@ import java.util.Set; */ public interface HasHeaders { - V putHeader(String key, V value); + void putHeader(String key, V value); V getHeader(String key); diff --git a/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/src/main/java/org/elasticsearch/percolator/PercolateContext.java index 6b3d3312211..4eb4b99ba2b 100644 --- a/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -45,7 +45,6 @@ import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.query.IndexQueryParserService; @@ -683,7 +682,7 @@ public class PercolateContext extends SearchContext { @Override public V putInContext(Object key, Object value) { - assert false : "percolatocontext does not support contexts & headers"; + assert false : "percolatecontext does not support contexts & headers"; return null; } @@ -724,13 +723,12 @@ public class PercolateContext extends SearchContext { @Override public void copyContextFrom(HasContext other) { - assert false : "percolatocontext does not support contexts & headers"; + assert false : "percolatecontext does not support contexts & headers"; } @Override - public V putHeader(String key, V value) { - assert false : "percolatocontext does not support contexts & headers"; - return value; + public void putHeader(String key, V value) { + assert false : "percolatecontext does not support contexts & headers"; } @Override @@ -750,11 +748,11 @@ public class PercolateContext extends SearchContext { @Override public void copyHeadersFrom(HasHeaders from) { - assert false : "percolatocontext does not support contexts & headers"; + assert false : "percolatecontext does not support contexts & headers"; } @Override public void copyContextAndHeadersFrom(HasContextAndHeaders other) { - assert false : "percolatocontext does not support contexts & headers"; + assert false : "percolatecontext does not support contexts & headers"; } } diff --git a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 8211d60bc13..962d37a9908 100644 --- a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -788,8 +788,8 @@ public class DefaultSearchContext extends SearchContext { } @Override - public V putHeader(String key, V value) { - return request.putHeader(key, value); + public void putHeader(String key, V value) { + request.putHeader(key, value); } @Override diff --git a/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index 0c73f1d048f..ffc6004d334 100644 --- a/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -597,8 +597,8 @@ public abstract class FilteredSearchContext extends SearchContext { } @Override - public V putHeader(String key, V value) { - return in.putHeader(key, value); + public void putHeader(String key, V value) { + in.putHeader(key, value); } @Override diff --git a/src/main/java/org/elasticsearch/transport/TransportMessage.java b/src/main/java/org/elasticsearch/transport/TransportMessage.java index 5adb02d3f89..f52f9179dd6 100644 --- a/src/main/java/org/elasticsearch/transport/TransportMessage.java +++ b/src/main/java/org/elasticsearch/transport/TransportMessage.java @@ -26,13 +26,12 @@ import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.transport.TransportAddress; import java.io.IOException; -import java.util.HashMap; /** * The transport message is also a {@link ContextAndHeaderHolder context holder} that holds transient context, that is, * the context is not serialized with message. */ -public abstract class TransportMessage> extends ContextAndHeaderHolder implements Streamable { +public abstract class TransportMessage> extends ContextAndHeaderHolder implements Streamable { private TransportAddress remoteAddress; @@ -40,13 +39,7 @@ public abstract class TransportMessage> extends } protected TransportMessage(TM message) { - // create a new copy of the headers/context, since we are creating a new request - // which might have its headers/context changed in the context of that specific request - - if (message.headers != null) { - this.headers = new HashMap<>(message.headers); - } - copyContextFrom(message); + copyContextAndHeadersFrom(message); } public void remoteAddress(TransportAddress remoteAddress) { diff --git a/src/test/java/org/elasticsearch/test/TestSearchContext.java b/src/test/java/org/elasticsearch/test/TestSearchContext.java index 321f01a3c25..54fabbd03da 100644 --- a/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -638,9 +638,7 @@ public class TestSearchContext extends SearchContext { } @Override - public V putHeader(String key, V value) { - return value; - } + public void putHeader(String key, V value) {} @Override public V getHeader(String key) { diff --git a/src/test/java/org/elasticsearch/transport/TransportMessageTests.java b/src/test/java/org/elasticsearch/transport/TransportMessageTests.java index 5d06ee6f69f..4fdd076f7c1 100644 --- a/src/test/java/org/elasticsearch/transport/TransportMessageTests.java +++ b/src/test/java/org/elasticsearch/transport/TransportMessageTests.java @@ -51,6 +51,10 @@ public class TransportMessageTests extends ElasticsearchTestCase { assertThat((String) message.getHeader("key1"), equalTo("value1")); assertThat((String) message.getHeader("key2"), equalTo("value2")); assertThat(message.isContextEmpty(), is(true)); + + // ensure that casting is not needed + String key1 = message.getHeader("key1"); + assertThat(key1, is("value1")); } @Test @@ -66,6 +70,18 @@ public class TransportMessageTests extends ElasticsearchTestCase { assertThat((String) m2.getHeader("key1"), equalTo("value1")); assertThat((String) m2.getHeader("key2"), equalTo("value2")); assertThat((String) m2.getFromContext("key3"), equalTo("value3")); + + // ensure that casting is not needed + String key3 = m2.getFromContext("key3"); + assertThat(key3, is("value3")); + testContext(m2, "key3", "value3"); + } + + // ensure that generic arg like this is not needed: TransportMessage transportMessage + private void testContext(TransportMessage transportMessage, String key, String expectedValue) { + String result = transportMessage.getFromContext(key); + assertThat(result, is(expectedValue)); + } private static class Message extends TransportMessage {