Skip to content

Instantly share code, notes, and snippets.

@zabetak
Created October 23, 2019 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabetak/ac2a8320c72779dd646230278662fdd4 to your computer and use it in GitHub Desktop.
Save zabetak/ac2a8320c72779dd646230278662fdd4 to your computer and use it in GitHub Desktop.
/**
* A Query that needs to be bound to a value before it can be executed.
*
* The query can take many forms depending on the value which is bound at each
* time. The same value should always lead to the same query. Normally, before
* executing the query a concrete value must be set via {@link #setValue(Object)}
* method although some implementations may decide to return a query even the
* value is not set.
*/
public abstract class ParameterizedQuery extends Query
{
private Object value;
protected ParameterizedQuery()
{
}
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
throws IOException
{
Query query = this.createQuery(this.value);
assert query != null;
return query.createWeight(searcher, scoreMode, boost);
}
@Override
public Query rewrite(IndexReader reader) throws IOException
{
Query query = this.createQuery(this.value);
assert query != null;
return query.rewrite(reader);
}
/**
* Creates a query based on the value provided as a parameter.
*
* It should never return null as long as {@link #setValue(Object)} is
* called before. The same value should always generate the same query.
* Implementors can decide to return a default query when null is passed as
* a value. The method should not be called directly but only via
* {@link #createWeight(IndexSearcher, ScoreMode, float)} and
* {@link #rewrite(IndexReader)} during the execution of the query.
*
* @param value
* the value that is used to generate the query
* @return
* a query based on the provided value
*
*/
protected abstract Query createQuery(Object value);
/**
* Sets the value that will be used to generate the query.
*
* The method must be called before executing the query.
*/
public final void setValue(Object value)
{
this.value = value;
}
/**
* The query itself is not going to be executed or added to the
* {@link QueryCache} so there is no need to provide other equality than the
* identity.
*/
@Override
public boolean equals(Object obj)
{
return this == obj;
}
@Override
public int hashCode()
{
return System.identityHashCode(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment