make Context generic so we don't have to cast

This commit is contained in:
Britta Weber 2015-07-29 16:00:37 +02:00
parent 24c4b27978
commit 6295738d4c
5 changed files with 11 additions and 13 deletions

View File

@ -121,7 +121,7 @@ public interface FetchSubPhase {
* Fetch phases that use the plugin mechanism must provide a ContextFactory to the SearchContext that creates the fetch phase context and also associates them with a name. * Fetch phases that use the plugin mechanism must provide a ContextFactory to the SearchContext that creates the fetch phase context and also associates them with a name.
* See {@link SearchContext#getFetchSubPhaseContext(FetchSubPhase.ContextFactory)} * See {@link SearchContext#getFetchSubPhaseContext(FetchSubPhase.ContextFactory)}
*/ */
public interface ContextFactory { public interface ContextFactory<SubPhaseContext extends FetchSubPhaseContext> {
/** /**
* The name of the context. * The name of the context.
@ -131,6 +131,6 @@ public interface FetchSubPhase {
/** /**
* Creates a new instance of a FetchSubPhaseContext that holds all information a FetchSubPhase needs to execute on hits. * Creates a new instance of a FetchSubPhaseContext that holds all information a FetchSubPhase needs to execute on hits.
*/ */
public FetchSubPhaseContext newContextInstance(); public SubPhaseContext newContextInstance();
} }
} }

View File

@ -26,11 +26,11 @@ import org.elasticsearch.search.internal.SearchContext;
/** /**
* A parse element for a {@link org.elasticsearch.search.fetch.FetchSubPhase} that is used when parsing a search request. * A parse element for a {@link org.elasticsearch.search.fetch.FetchSubPhase} that is used when parsing a search request.
*/ */
public abstract class FetchSubPhaseParseElement implements SearchParseElement { public abstract class FetchSubPhaseParseElement<SubPhaseContext extends FetchSubPhaseContext> implements SearchParseElement {
@Override @Override
final public void parse(XContentParser parser, SearchContext context) throws Exception { final public void parse(XContentParser parser, SearchContext context) throws Exception {
FetchSubPhaseContext fetchSubPhaseContext = context.getFetchSubPhaseContext(getContextFactory()); SubPhaseContext fetchSubPhaseContext = context.getFetchSubPhaseContext(getContextFactory());
// this is to make sure that the SubFetchPhase knows it should execute // this is to make sure that the SubFetchPhase knows it should execute
fetchSubPhaseContext.setHitExecutionNeeded(true); fetchSubPhaseContext.setHitExecutionNeeded(true);
innerParse(parser, fetchSubPhaseContext); innerParse(parser, fetchSubPhaseContext);
@ -39,10 +39,10 @@ public abstract class FetchSubPhaseParseElement implements SearchParseElement {
/** /**
* Implement the actual parsing here. * Implement the actual parsing here.
*/ */
protected abstract void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception; protected abstract void innerParse(XContentParser parser, SubPhaseContext fetchSubPhaseContext) throws Exception;
/** /**
* Return the ContextFactory for this FetchSubPhase. * Return the ContextFactory for this FetchSubPhase.
*/ */
protected abstract FetchSubPhase.ContextFactory getContextFactory(); protected abstract FetchSubPhase.ContextFactory<SubPhaseContext> getContextFactory();
} }

View File

@ -36,11 +36,10 @@ import org.elasticsearch.search.internal.SearchContext;
* } * }
* </pre> * </pre>
*/ */
public class FieldDataFieldsParseElement extends FetchSubPhaseParseElement { public class FieldDataFieldsParseElement extends FetchSubPhaseParseElement<FieldDataFieldsContext> {
@Override @Override
protected void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception { protected void innerParse(XContentParser parser, FieldDataFieldsContext fieldDataFieldsContext) throws Exception {
FieldDataFieldsContext fieldDataFieldsContext = (FieldDataFieldsContext) fetchSubPhaseContext;
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
if (token == XContentParser.Token.START_ARRAY) { if (token == XContentParser.Token.START_ARRAY) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) {

View File

@ -164,7 +164,7 @@ public abstract class SearchContext implements Releasable, HasContextAndHeaders
public abstract SearchContext aggregations(SearchContextAggregations aggregations); public abstract SearchContext aggregations(SearchContextAggregations aggregations);
public abstract FetchSubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory); public abstract <SubPhaseContext extends FetchSubPhaseContext> SubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory<SubPhaseContext> contextFactory);
public abstract SearchContextHighlight highlight(); public abstract SearchContextHighlight highlight();

View File

@ -183,12 +183,11 @@ public class FetchSubPhasePluginTests extends ElasticsearchIntegrationTest {
} }
} }
public static class TermVectorsFetchParseElement extends FetchSubPhaseParseElement { public static class TermVectorsFetchParseElement extends FetchSubPhaseParseElement<TermVectorsFetchContext> {
@Override @Override
protected void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception { protected void innerParse(XContentParser parser, TermVectorsFetchContext termVectorsFetchContext) throws Exception {
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
TermVectorsFetchContext termVectorsFetchContext = (TermVectorsFetchContext) fetchSubPhaseContext;
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
String fieldName = parser.text(); String fieldName = parser.text();
termVectorsFetchContext.setField(fieldName); termVectorsFetchContext.setField(fieldName);