This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit 7ff8dee09e17732a30fbcc2bf52203f86d859006 Author: Andy Seaborne <[email protected]> AuthorDate: Thu Dec 19 16:50:37 2024 +0000 Reformat class Query --- .../src/main/java/org/apache/jena/query/Query.java | 800 ++++++++++----------- .../java/org/apache/jena/query/QueryVisitor.java | 42 +- 2 files changed, 388 insertions(+), 454 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/query/Query.java b/jena-arq/src/main/java/org/apache/jena/query/Query.java index ed1546e18e..faf6a8aa70 100644 --- a/jena-arq/src/main/java/org/apache/jena/query/Query.java +++ b/jena-arq/src/main/java/org/apache/jena/query/Query.java @@ -18,34 +18,34 @@ package org.apache.jena.query; -import java.io.OutputStream ; -import java.util.* ; - -import org.apache.jena.atlas.io.IndentedLineBuffer ; -import org.apache.jena.atlas.io.IndentedWriter ; -import org.apache.jena.atlas.io.Printable ; -import org.apache.jena.atlas.logging.Log ; -import org.apache.jena.graph.Node ; -import org.apache.jena.sparql.ARQConstants ; -import org.apache.jena.sparql.algebra.table.TableData ; -import org.apache.jena.sparql.core.* ; -import org.apache.jena.sparql.engine.binding.Binding ; -import org.apache.jena.sparql.expr.Expr ; -import org.apache.jena.sparql.expr.ExprAggregator ; -import org.apache.jena.sparql.expr.ExprTransform ; -import org.apache.jena.sparql.expr.ExprVar ; -import org.apache.jena.sparql.expr.aggregate.Aggregator ; -import org.apache.jena.sparql.serializer.QuerySerializerFactory ; -import org.apache.jena.sparql.serializer.SerializerRegistry ; -import org.apache.jena.sparql.syntax.Element ; -import org.apache.jena.sparql.syntax.PatternVars ; -import org.apache.jena.sparql.syntax.Template ; +import java.io.OutputStream; +import java.util.*; + +import org.apache.jena.atlas.io.IndentedLineBuffer; +import org.apache.jena.atlas.io.IndentedWriter; +import org.apache.jena.atlas.io.Printable; +import org.apache.jena.atlas.logging.Log; +import org.apache.jena.graph.Node; +import org.apache.jena.sparql.ARQConstants; +import org.apache.jena.sparql.algebra.table.TableData; +import org.apache.jena.sparql.core.*; +import org.apache.jena.sparql.engine.binding.Binding; +import org.apache.jena.sparql.expr.Expr; +import org.apache.jena.sparql.expr.ExprAggregator; +import org.apache.jena.sparql.expr.ExprTransform; +import org.apache.jena.sparql.expr.ExprVar; +import org.apache.jena.sparql.expr.aggregate.Aggregator; +import org.apache.jena.sparql.serializer.QuerySerializerFactory; +import org.apache.jena.sparql.serializer.SerializerRegistry; +import org.apache.jena.sparql.syntax.Element; +import org.apache.jena.sparql.syntax.PatternVars; +import org.apache.jena.sparql.syntax.Template; import org.apache.jena.sparql.syntax.syntaxtransform.ElementTransform; import org.apache.jena.sparql.syntax.syntaxtransform.ElementTransformCopyBase; import org.apache.jena.sparql.syntax.syntaxtransform.ExprTransformApplyElementTransform; import org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps; -import org.apache.jena.sparql.util.FmtUtils ; -import org.apache.jena.sys.JenaSystem ; +import org.apache.jena.sparql.util.FmtUtils; +import org.apache.jena.sys.JenaSystem; /** The data structure for a query as presented externally. * There are two ways of creating a query - use the parser to turn @@ -61,474 +61,434 @@ import org.apache.jena.sys.JenaSystem ; public class Query extends Prologue implements Cloneable, Printable { - static { JenaSystem.init() ; /* Ensure everything has started properly */ } + static { JenaSystem.init(); /* Ensure everything has started properly */ } // Old constants. Retain for compatibility. - public static final int QueryTypeUnknown = -123 ; - public static final int QueryTypeSelect = 111 ; - public static final int QueryTypeConstruct = 222 ; - public static final int QueryTypeDescribe = 333 ; - public static final int QueryTypeAsk = 444 ; - public static final int QueryTypeJson = 555 ; + public static final int QueryTypeUnknown = -123; + public static final int QueryTypeSelect = 111; + public static final int QueryTypeConstruct = 222; + public static final int QueryTypeDescribe = 333; + public static final int QueryTypeAsk = 444; + public static final int QueryTypeJson = 555; private QueryType queryType = QueryType.UNKNOWN; // If no model is provided explicitly, the query engine will load // a model from the URL. Never a list of zero items. - private List<String> graphURIs = new ArrayList<>() ; - private List<String> namedGraphURIs = new ArrayList<>() ; + private List<String> graphURIs = new ArrayList<>(); + private List<String> namedGraphURIs = new ArrayList<>(); // The WHERE clause - private Element queryPattern = null ; + private Element queryPattern = null; // Query syntax - private Syntax syntax = Syntax.syntaxSPARQL ; // Default + private Syntax syntax = Syntax.syntaxSPARQL; // Default // LIMIT/OFFSET - public static final long NOLIMIT = Long.MIN_VALUE ; - private long resultLimit = NOLIMIT ; - private long resultOffset = NOLIMIT ; + public static final long NOLIMIT = Long.MIN_VALUE; + private long resultLimit = NOLIMIT; + private long resultOffset = NOLIMIT; // ORDER BY - private List<SortCondition> orderBy = null ; - public static final int ORDER_ASCENDING = 1 ; - public static final int ORDER_DESCENDING = -1 ; - public static final int ORDER_DEFAULT = -2 ; // Not explicitly given. - public static final int ORDER_UNKNOW = -3 ; + private List<SortCondition> orderBy = null; + public static final int ORDER_ASCENDING = 1; + public static final int ORDER_DESCENDING = -1; + public static final int ORDER_DEFAULT = -2; // Not explicitly given. + public static final int ORDER_UNKNOW = -3; // VALUES trailing clause - protected TableData valuesDataBlock = null ; + protected TableData valuesDataBlock = null; - protected boolean strictQuery = true ; + protected boolean strictQuery = true; // SELECT * seen - protected boolean queryResultStar = false ; + protected boolean queryResultStar = false; - protected boolean distinct = false ; - protected boolean reduced = false ; + protected boolean distinct = false; + protected boolean reduced = false; // CONSTRUCT - protected Template constructTemplate = null ; + protected Template constructTemplate = null; // DESCRIBE // Any URIs/QNames in the DESCRIBE clause // Also uses resultVars - protected List<Node> resultNodes = new ArrayList<>() ; // Type in list: Node + protected List<Node> resultNodes = new ArrayList<>(); // Type in list: Node /** * Creates a new empty query */ - public Query() - { - syntax = Syntax.syntaxSPARQL ; + public Query() { + syntax = Syntax.syntaxSPARQL; } /** * Creates a new empty query with the given prologue */ - public Query(Prologue prologue) - { - this() ; + public Query(Prologue prologue) { + this(); Prologue p2 = prologue.copy(); prefixMap = p2.getPrefixMapping(); - seenBaseURI = false ; + seenBaseURI = false; resolver = p2.getResolver(); } // Allocate variables that are unique to this query. - private VarAlloc varAlloc = new VarAlloc(ARQConstants.allocQueryVariables) ; - private Var allocInternVar() { return varAlloc.allocVar() ; } + private VarAlloc varAlloc = new VarAlloc(ARQConstants.allocQueryVariables); + private Var allocInternVar() { return varAlloc.allocVar(); } - public void setQuerySelectType() { queryType = QueryType.SELECT ; } - public void setQueryConstructType() { queryType = QueryType.CONSTRUCT ; queryResultStar = true ; } + public void setQuerySelectType() { queryType = QueryType.SELECT; } + public void setQueryConstructType() { queryType = QueryType.CONSTRUCT; queryResultStar = true; } public void setQueryDescribeType() { queryType = QueryType.DESCRIBE; } public void setQueryAskType() { queryType = QueryType.ASK; } public void setQueryJsonType() { queryType = QueryType.CONSTRUCT_JSON; } /** Return the {@link QueryType} */ - public QueryType queryType() { return queryType ; } + public QueryType queryType() { return queryType; } public boolean isSelectType() { return queryType == QueryType.SELECT; } - public boolean isConstructType() { return queryType == QueryType.CONSTRUCT ; } + public boolean isConstructType() { return queryType == QueryType.CONSTRUCT; } - public boolean isDescribeType() { return queryType == QueryType.DESCRIBE ; } + public boolean isDescribeType() { return queryType == QueryType.DESCRIBE; } - public boolean isAskType() { return queryType == QueryType.ASK ; } + public boolean isAskType() { return queryType == QueryType.ASK; } - public boolean isJsonType() { return queryType == QueryType.CONSTRUCT_JSON ; } + public boolean isJsonType() { return queryType == QueryType.CONSTRUCT_JSON; } - public boolean isUnknownType() { return queryType == QueryType.UNKNOWN ; } + public boolean isUnknownType() { return queryType == QueryType.UNKNOWN; } public boolean isConstructQuad() { return (isConstructType() && constructTemplate.containsRealQuad()); } // It was a mistake to extend Prologue ... but what is done is done. - public Prologue getPrologue() { return this ; } - - public void setStrict(boolean isStrict) - { - strictQuery = isStrict ; + public Prologue getPrologue() { return this; } - if ( strictQuery ) - initStrict() ; - else - initLax() ; + public void setStrict(boolean isStrict) { + strictQuery = isStrict; } - public boolean isStrict() { return strictQuery ; } - - private void initStrict() - { -// if ( prefixMap.getGlobalPrefixMapping() == globalPrefixMap ) -// prefixMap.setGlobalPrefixMapping(null) ; - } - - private void initLax() - { -// if ( prefixMap.getGlobalPrefixMapping() == null ) -// prefixMap.setGlobalPrefixMapping(globalPrefixMap) ; - } + public boolean isStrict() { return strictQuery; } - public void setDistinct(boolean b) { distinct = b ; } - public boolean isDistinct() { return distinct ; } + public void setDistinct(boolean b) { distinct = b; } + public boolean isDistinct() { return distinct; } - public void setReduced(boolean b) { reduced = b ; } - public boolean isReduced() { return reduced ; } + public void setReduced(boolean b) { reduced = b; } + public boolean isReduced() { return reduced; } /** @return Returns the syntax. */ - public Syntax getSyntax() { return syntax ; } + public Syntax getSyntax() { return syntax; } /** @param syntax The syntax to set. */ - public void setSyntax(Syntax syntax) - { - this.syntax = syntax ; + public void setSyntax(Syntax syntax) { + this.syntax = syntax; if ( syntax != Syntax.syntaxSPARQL ) - strictQuery = false ; + strictQuery = false; } // ---- Limit/offset - public long getLimit() { return resultLimit ; } - public void setLimit(long limit) { resultLimit = limit ; } - public boolean hasLimit() { return resultLimit != NOLIMIT ; } + public long getLimit() { return resultLimit; } + public void setLimit(long limit) { resultLimit = limit; } + public boolean hasLimit() { return resultLimit != NOLIMIT; } - public long getOffset() { return resultOffset ; } - public void setOffset(long offset) { resultOffset = offset ; } - public boolean hasOffset() { return resultOffset != NOLIMIT ; } + public long getOffset() { return resultOffset; } + public void setOffset(long offset) { resultOffset = offset; } + public boolean hasOffset() { return resultOffset != NOLIMIT; } // ---- Order By - public boolean hasOrderBy() { return orderBy != null && orderBy.size() > 0 ; } + public boolean hasOrderBy() { return orderBy != null && orderBy.size() > 0; } - public boolean isOrdered() { return hasOrderBy() ; } + public boolean isOrdered() { return hasOrderBy(); } - public void addOrderBy(SortCondition condition) - { + public void addOrderBy(SortCondition condition) { if ( orderBy == null ) - orderBy = new ArrayList<>() ; + orderBy = new ArrayList<>(); - orderBy.add(condition) ; + orderBy.add(condition); } - public void addOrderBy(Expr expr, int direction) - { - SortCondition sc = new SortCondition(expr, direction) ; - addOrderBy(sc) ; + + public void addOrderBy(Expr expr, int direction) { + SortCondition sc = new SortCondition(expr, direction); + addOrderBy(sc); } - public void addOrderBy(Node var, int direction) - { - if ( ! var.isVariable() ) - throw new QueryException("Not a variable: "+var) ; - SortCondition sc = new SortCondition(var, direction) ; - addOrderBy(sc) ; + public void addOrderBy(Node var, int direction) { + if ( !var.isVariable() ) + throw new QueryException("Not a variable: " + var); + SortCondition sc = new SortCondition(var, direction); + addOrderBy(sc); } - public void addOrderBy(String varName, int direction) - { - varName = Var.canonical(varName) ; - SortCondition sc = new SortCondition(new ExprVar(varName), direction) ; - addOrderBy(sc) ; + public void addOrderBy(String varName, int direction) { + varName = Var.canonical(varName); + SortCondition sc = new SortCondition(new ExprVar(varName), direction); + addOrderBy(sc); } - public List<SortCondition> getOrderBy() { return orderBy ; } + public List<SortCondition> getOrderBy() { + return orderBy; + } // ---- - /** Answer whether the query had SELECT/DESCRIBE/CONSTRUCT * + /** + * Answer whether the query had SELECT/DESCRIBE/CONSTRUCT * + * * @return boolean as to whether a * result form was seen */ - public boolean isQueryResultStar() { return queryResultStar ; } + public boolean isQueryResultStar() { + return queryResultStar; + } - /** Set whether the query had SELECT/DESCRIBE * + /** + * Set whether the query had SELECT/DESCRIBE * * * @param isQueryStar */ - public void setQueryResultStar(boolean isQueryStar) - { - queryResultStar = isQueryStar ; + public void setQueryResultStar(boolean isQueryStar) { + queryResultStar = isQueryStar; if ( isQueryStar ) - resultVarsSet = false ; + resultVarsSet = false; } - public void setQueryPattern(Element elt) - { - queryPattern = elt ; + public void setQueryPattern(Element elt) { + queryPattern = elt; } - public Element getQueryPattern() { return queryPattern ; } + public Element getQueryPattern() { + return queryPattern; + } - /** Location of the source for the data. If the model is not set, - * then the QueryEngine will attempt to load the data from these URIs - * into the default (unnamed) graph. + /** + * Location of the source for the data. If the model is not set, then the + * QueryEngine will attempt to load the data from these URIs into the default + * (unnamed) graph. */ - public void addGraphURI(String s) - { + public void addGraphURI(String s) { if ( graphURIs == null ) - graphURIs = new ArrayList<>() ; - graphURIs.add(s) ; + graphURIs = new ArrayList<>(); + graphURIs.add(s); } - /** Location of the source for the data. If the model is not set, - * then the QueryEngine will attempt to load the data from these URIs - * as named graphs in the dataset. + /** + * Location of the source for the data. If the model is not set, then the + * QueryEngine will attempt to load the data from these URIs as named graphs in + * the dataset. */ - public void addNamedGraphURI(String uri) - { + public void addNamedGraphURI(String uri) { if ( namedGraphURIs == null ) - namedGraphURIs = new ArrayList<>() ; + namedGraphURIs = new ArrayList<>(); if ( namedGraphURIs.contains(uri) ) - throw new QueryException("URI already in named graph set: "+uri) ; + throw new QueryException("URI already in named graph set: " + uri); else - namedGraphURIs.add(uri) ; + namedGraphURIs.add(uri); } - /** Return the list of URIs (strings) for the unnamed graph + /** + * Return the list of URIs (strings) for the unnamed graph * * @return List of strings */ - - public List<String> getGraphURIs() { return graphURIs ; } + public List<String> getGraphURIs() { return graphURIs; } /** Test whether the query mentions a URI in forming the default graph (FROM clause) * * @param uri * @return boolean True if the URI used in a FROM clause */ - public boolean usesGraphURI(String uri) { return graphURIs.contains(uri) ; } + public boolean usesGraphURI(String uri) { return graphURIs.contains(uri); } - /** Return the list of URIs (strings) for the named graphs (FROM NAMED clause) + /** + * Return the list of URIs (strings) for the named graphs (FROM NAMED clause) * * @return List of strings */ + public List<String> getNamedGraphURIs() { return namedGraphURIs; } - public List<String> getNamedGraphURIs() { return namedGraphURIs ; } - - /** Test whether the query mentions a URI for a named graph. + /** + * Test whether the query mentions a URI for a named graph. * * @param uri * @return True if the URI used in a FROM NAMED clause */ - public boolean usesNamedGraphURI(String uri) { return namedGraphURIs.contains(uri) ; } + public boolean usesNamedGraphURI(String uri) { return namedGraphURIs.contains(uri); } - /** Return true if the query has either some graph - * URIs or some named graph URIs in its description. - * This does not mean these URIs will be used - just that + /** + * Return true if the query has either some graph URIs or some named graph URIs + * in its description. This does not mean these URIs will be used - just that * they are noted as part of the query. */ - public boolean hasDatasetDescription() - { + public boolean hasDatasetDescription() { if ( getGraphURIs() != null && getGraphURIs().size() > 0 ) - return true ; + return true; if ( getNamedGraphURIs() != null && getNamedGraphURIs().size() > 0 ) - return true ; - return false ; + return true; + return false; } /** Return a dataset description (FROM/FROM NAMED clauses) for the query. */ - public DatasetDescription getDatasetDescription() - { - if ( ! hasDatasetDescription() ) + public DatasetDescription getDatasetDescription() { + if ( !hasDatasetDescription() ) return null; - DatasetDescription description = new DatasetDescription() ; + DatasetDescription description = new DatasetDescription(); - description.addAllDefaultGraphURIs(getGraphURIs()) ; - description.addAllNamedGraphURIs(getNamedGraphURIs()) ; - return description ; + description.addAllDefaultGraphURIs(getGraphURIs()); + description.addAllNamedGraphURIs(getNamedGraphURIs()); + return description; } // ---- SELECT - protected VarExprList projectVars = new VarExprList() ; + protected VarExprList projectVars = new VarExprList(); /** Return a list of the variables requested (SELECT) */ - public List<String> getResultVars() - { + public List<String> getResultVars() { // Ensure "SELECT *" processed - setResultVars() ; - return Var.varNames(projectVars.getVars()) ; + setResultVars(); + return Var.varNames(projectVars.getVars()); } /** Return a list of the variables requested (SELECT) */ - public List<Var> getProjectVars() - { + public List<Var> getProjectVars() { // Ensure "SELECT *" processed - setResultVars() ; - return projectVars.getVars() ; + setResultVars(); + return projectVars.getVars(); } - public VarExprList getProject() - { - return projectVars ; + public VarExprList getProject() { + return projectVars; } /** Add a collection of projection variables to a SELECT query */ - public void addProjectVars(Collection<?> vars) - { - for ( Object obj : vars ) - { - if ( obj instanceof String ) - { - this.addResultVar( (String) obj ); + public void addProjectVars(Collection<? > vars) { + for ( Object obj : vars ) { + if ( obj instanceof String ) { + this.addResultVar((String)obj); continue; } - if ( obj instanceof Var ) - { - this.addResultVar( (Var) obj ); + if ( obj instanceof Var ) { + this.addResultVar((Var)obj); continue; } - throw new QueryException( "Not a variable or variable name: " + obj ); + throw new QueryException("Not a variable or variable name: " + obj); } - resultVarsSet = true ; + resultVarsSet = true; } - /** Add a projection variable to a SELECT query */ - public void addResultVar(String varName) - { - varName = Var.canonical(varName) ; - _addResultVar(varName) ; + public void addResultVar(String varName) { + varName = Var.canonical(varName); + _addResultVar(varName); } - public void addResultVar(Node v) - { + public void addResultVar(Node v) { if ( !v.isVariable() ) - throw new QueryException("Not a variable: "+v) ; - _addResultVar(v.getName()) ; + throw new QueryException("Not a variable: " + v); + _addResultVar(v.getName()); } - public void addResultVar(Node v, Expr expr) - { - Var var = null ; + public void addResultVar(Node v, Expr expr) { + Var var = null; if ( v == null ) - var = allocInternVar() ; - else - { + var = allocInternVar(); + else { if ( !v.isVariable() ) - throw new QueryException("Not a variable: "+v) ; - var = Var.alloc(v) ; + throw new QueryException("Not a variable: " + v); + var = Var.alloc(v); } - _addVarExpr(projectVars, var, expr) ; + _addVarExpr(projectVars, var, expr); } /** Add an to a SELECT query (a name will be created for it) */ - public void addResultVar(Expr expr) - { - _addVarExpr(projectVars, allocInternVar(), expr) ; + public void addResultVar(Expr expr) { + _addVarExpr(projectVars, allocInternVar(), expr); } /** Add a named expression to a SELECT query */ - public void addResultVar(String varName, Expr expr) - { - Var var = null ; + public void addResultVar(String varName, Expr expr) { + Var var = null; if ( varName == null ) - var = allocInternVar() ; - else - { - varName = Var.canonical(varName) ; - var = Var.alloc(varName) ; + var = allocInternVar(); + else { + varName = Var.canonical(varName); + var = Var.alloc(varName); } - _addVarExpr(projectVars, var, expr) ; + _addVarExpr(projectVars, var, expr); } // Add raw name. - private void _addResultVar(String varName) - { - Var v = Var.alloc(varName) ; - _addVar(projectVars, v) ; - resultVarsSet = true ; + private void _addResultVar(String varName) { + Var v = Var.alloc(varName); + _addVar(projectVars, v); + resultVarsSet = true; } - private static void _addVar(VarExprList varExprList, Var v) - { - if ( varExprList.contains(v) ) - { - Expr expr = varExprList.getExpr(v) ; + private static void _addVar(VarExprList varExprList, Var v) { + if ( varExprList.contains(v) ) { + Expr expr = varExprList.getExpr(v); if ( expr != null ) // SELECT (?a+?b AS ?x) ?x - throw new QueryBuildException("Duplicate variable (had an expression) in result projection '"+v+"'") ; + throw new QueryBuildException("Duplicate variable (had an expression) in result projection '" + v + "'"); // SELECT ?x ?x - if ( ! ARQ.allowDuplicateSelectColumns ) - return ; + if ( !ARQ.allowDuplicateSelectColumns ) + return; // else drop through and have two variables of the same name. } - varExprList.add(v) ; + varExprList.add(v); } - private static void _addVarExpr(VarExprList varExprList, Var v, Expr expr) - { + private static void _addVarExpr(VarExprList varExprList, Var v, Expr expr) { if ( varExprList.contains(v) ) // SELECT ?x (?a+?b AS ?x) // SELECT (2*?a AS ?x) (?a+?b AS ?x) - throw new QueryBuildException("Duplicate variable in result projection '"+v+"'") ; - varExprList.add(v, expr) ; + throw new QueryBuildException("Duplicate variable in result projection '" + v + "'"); + varExprList.add(v, expr); } - protected VarExprList groupVars = new VarExprList() ; - protected List<Expr> havingExprs = new ArrayList<>() ; // Expressions : Make an ExprList? + protected VarExprList groupVars = new VarExprList(); + protected List<Expr> havingExprs = new ArrayList<>(); // Expressions : Make an ExprList? - public boolean hasGroupBy() { return ! groupVars.isEmpty() || getAggregators().size() > 0 ; } - public boolean hasHaving() { return havingExprs != null && havingExprs.size() > 0 ; } + public boolean hasGroupBy() { return ! groupVars.isEmpty() || getAggregators().size() > 0; } + public boolean hasHaving() { return havingExprs != null && havingExprs.size() > 0; } - public VarExprList getGroupBy() { return groupVars ; } + public VarExprList getGroupBy() { return groupVars; } - public List<Expr> getHavingExprs() { return havingExprs ; } + public List<Expr> getHavingExprs() { return havingExprs; } public void addGroupBy(String varName) { - varName = Var.canonical(varName) ; - addGroupBy(Var.alloc(varName)) ; + varName = Var.canonical(varName); + addGroupBy(Var.alloc(varName)); } public void addGroupBy(Node v) { - _addVar(groupVars, Var.alloc(v)) ; + _addVar(groupVars, Var.alloc(v)); } - public void addGroupBy(Expr expr) { addGroupBy(null, expr) ; } + public void addGroupBy(Expr expr) { addGroupBy(null, expr); } - public void addGroupBy(Var v, Expr expr) - { + public void addGroupBy(Var v, Expr expr) { if ( v == null ) - v = allocInternVar() ; + v = allocInternVar(); - if ( expr.isVariable() && v.isAllocVar() ) - { + if ( expr.isVariable() && v.isAllocVar() ) { // It was (?x) with no AS - keep the name by adding by variable. - addGroupBy(expr.asVar()) ; - return ; + addGroupBy(expr.asVar()); + return; } - groupVars.add(v, expr) ; + groupVars.add(v, expr); } - public void addHavingCondition(Expr expr) - { - havingExprs.add(expr) ; + public void addHavingCondition(Expr expr) { + havingExprs.add(expr); } // SELECT JSON @@ -555,67 +515,62 @@ public class Query extends Prologue implements Cloneable, Printable // Unlike SELECT expressions, here the expression itself (E_Aggregator) knows its variable // Commonality? - private List<ExprAggregator> aggregators = new ArrayList<>() ; - private Map<Var, ExprAggregator> aggregatorsMap = new HashMap<>() ; + private List<ExprAggregator> aggregators = new ArrayList<>(); + private Map<Var, ExprAggregator> aggregatorsMap = new HashMap<>(); // Note any E_Aggregator created for reuse. - private Map<String, Var> aggregatorsAllocated = new HashMap<>() ; + private Map<String, Var> aggregatorsAllocated = new HashMap<>(); - public boolean hasAggregators() { return aggregators.size() != 0 ; } - public List<ExprAggregator> getAggregators() { return aggregators ; } + public boolean hasAggregators() { return aggregators.size() != 0 ; } + public List<ExprAggregator> getAggregators() { return aggregators; } - public Expr allocAggregate(Aggregator agg) - { - // We need to track the aggregators in case one aggregator is used twice, e.g. in HAVING and in SELECT expression - // (is that much harm to do twice? Yes, if distinct.) - String key = agg.key() ; + public Expr allocAggregate(Aggregator agg) { + // We need to track the aggregators in case one aggregator is used twice, + // e.g. in HAVING and in SELECT expression + // (is that much harm to do twice? Yes, if distinct.) + String key = agg.key(); Var v = aggregatorsAllocated.get(key); - if ( v != null ) - { - ExprAggregator eAgg = aggregatorsMap.get(v) ; - if ( ! agg.equals(eAgg.getAggregator()) ) - Log.warn(Query.class, "Internal inconsistency: Aggregator: "+agg) ; - return eAgg ; + if ( v != null ) { + ExprAggregator eAgg = aggregatorsMap.get(v); + if ( !agg.equals(eAgg.getAggregator()) ) + Log.warn(Query.class, "Internal inconsistency: Aggregator: " + agg); + return eAgg; } // Allocate. - v = allocInternVar() ; - ExprAggregator aggExpr = new ExprAggregator(v, agg) ; - aggregatorsAllocated.put(key, v) ; - aggregatorsMap.put(v, aggExpr) ; - aggregators.add(aggExpr) ; - return aggExpr ; + v = allocInternVar(); + ExprAggregator aggExpr = new ExprAggregator(v, agg); + aggregatorsAllocated.put(key, v); + aggregatorsMap.put(v, aggExpr); + aggregators.add(aggExpr); + return aggExpr; } // ---- VALUES /** Does the query have a VALUES trailing block? */ - public boolean hasValues() { return valuesDataBlock != null ; } + public boolean hasValues() { return valuesDataBlock != null; } /** Variables from a VALUES trailing block */ - public List<Var> getValuesVariables() { return valuesDataBlock==null ? null : valuesDataBlock.getVars() ; } + public List<Var> getValuesVariables() { return valuesDataBlock==null ? null : valuesDataBlock.getVars(); } /** Data from a VALUES trailing block. null for a Node means undef */ - public List<Binding> getValuesData() { return valuesDataBlock==null ? null : valuesDataBlock.getRows() ; } + public List<Binding> getValuesData() { return valuesDataBlock==null ? null : valuesDataBlock.getRows(); } - public void setValuesDataBlock(List<Var> variables, List<Binding> values) - { - checkDataBlock(variables, values) ; - valuesDataBlock = new TableData(variables, values) ; + public void setValuesDataBlock(List<Var> variables, List<Binding> values) { + checkDataBlock(variables, values); + valuesDataBlock = new TableData(variables, values); } - private static void checkDataBlock(List<Var> variables, List<Binding> values) - { + private static void checkDataBlock(List<Var> variables, List<Binding> values) { // Check. - int N = variables.size() ; - for ( Binding valueRow : values ) - { - Iterator<Var> iter= valueRow.vars() ; - for ( ; iter.hasNext() ; ) - { - Var v = iter.next() ; - if ( ! variables.contains(v) ) - throw new QueryBuildException("Variable "+v+" not found in "+variables) ; + int N = variables.size(); + for ( Binding valueRow : values ) { + Iterator<Var> iter = valueRow.vars(); + for ( ; iter.hasNext() ; ) { + Var v = iter.next(); + if ( !variables.contains(v) ) + throw new QueryBuildException("Variable " + v + " not found in " + variables); } } } @@ -623,52 +578,52 @@ public class Query extends Prologue implements Cloneable, Printable // ---- CONSTRUCT /** Get the template pattern for a construct query */ - public Template getConstructTemplate() - { - return constructTemplate ; + public Template getConstructTemplate() { + return constructTemplate; } /** Set triple patterns for a construct query */ - public void setConstructTemplate(Template templ) { constructTemplate = templ ; } + public void setConstructTemplate(Template templ) { + constructTemplate = templ; + } // ---- DESCRIBE - public void addDescribeNode(Node node) - { - if ( node.isVariable() ) { addResultVar(node) ; return ; } - if ( node.isURI() || node.isBlank() ) - { + public void addDescribeNode(Node node) { + if ( node.isVariable() ) { + addResultVar(node); + return; + } + if ( node.isURI() || node.isBlank() ) { if ( !resultNodes.contains(node) ) resultNodes.add(node); - return ; + return; } if ( node.isLiteral() ) - throw new QueryException("Result node is a literal: "+FmtUtils.stringForNode(node)) ; - throw new QueryException("Result node not recognized: "+node) ; + throw new QueryException("Result node is a literal: " + FmtUtils.stringForNode(node)); + throw new QueryException("Result node not recognized: " + node); } - /** Get the result list (things wanted - not the results themselves) * of a DESCRIBE query. */ - public List<Node> getResultURIs() { return resultNodes ; } + public List<Node> getResultURIs() { return resultNodes; } - private boolean resultVarsSet = false ; + private boolean resultVarsSet = false; /** * Set the results variables if necessary, when the query has "*" ({@code SELECT *} * or {@code DESCRIBE *}) and for a construct query. This operation is idempotent and can * be called to ensure the results variables have been set. */ - public void setResultVars() - { + public void setResultVars() { if ( resultVarsSet ) - return ; - synchronized(this) { + return; + synchronized (this) { if ( resultVarsSet ) return; // Synchronized in case this query is used in a multithreaded // situation calling setResultVars(). JENA-1861. resetResultVars(); - resultVarsSet = true ; + resultVarsSet = true; } } @@ -677,95 +632,85 @@ public class Query extends Prologue implements Cloneable, Printable * variables of the query for {@code SELECT *} and {@code DESCRIBE *} and {@code CONSTRUCT}. */ public void resetResultVars() { - if ( isQueryResultStar() ) + if ( isQueryResultStar() ) projectVars.clear(); - if ( getQueryPattern() == null ) - { - if ( ! this.isDescribeType() ) - Log.warn(this, "setResultVars(): no query pattern") ; - return ; + if ( getQueryPattern() == null ) { + if ( !this.isDescribeType() ) + Log.warn(this, "setResultVars(): no query pattern"); + return; } - if ( isSelectType() ) - { + if ( isSelectType() ) { if ( isQueryResultStar() ) - findAndAddNamedVars() ; - return ; + findAndAddNamedVars(); + return; } - if ( isConstructType() ) - { + if ( isConstructType() ) { // All named variables are in-scope - findAndAddNamedVars() ; - return ; + findAndAddNamedVars(); + return; } - if ( isDescribeType() ) - { + if ( isDescribeType() ) { if ( isQueryResultStar() ) - findAndAddNamedVars() ; - return ; + findAndAddNamedVars(); + return; } -// if ( isAskType() ) -// {} +// if ( isAskType() ) { +// } } - private void findAndAddNamedVars() - { - Iterator<Var> varIter = null ; + private void findAndAddNamedVars() { + Iterator<Var> varIter = null; if ( hasGroupBy() ) - varIter = groupVars.getVars().iterator() ; - else - { + varIter = groupVars.getVars().iterator(); + else { // Binding variables -- in patterns, not in filters and not in EXISTS - LinkedHashSet<Var> queryVars = new LinkedHashSet<>() ; - PatternVars.vars(queryVars, this.getQueryPattern()) ; + LinkedHashSet<Var> queryVars = new LinkedHashSet<>(); + PatternVars.vars(queryVars, this.getQueryPattern()); if ( this.hasValues() ) - queryVars.addAll(getValuesVariables()) ; -// if ( this.hasValues() ) -// queryVars.addAll(getValuesVariables()) ; - varIter = queryVars.iterator() ; + queryVars.addAll(getValuesVariables()); + varIter = queryVars.iterator(); } // All query variables, including ones from bNodes in the query. - for ( ; varIter.hasNext() ; ) - { - Var var = varIter.next() ; + for ( ; varIter.hasNext() ; ) { + Var var = varIter.next(); if ( var.isNamedVar() ) - addResultVar(var) ; + addResultVar(var); } } - public void visit(QueryVisitor visitor) - { - visitor.startVisit(this) ; - visitor.visitResultForm(this) ; - visitor.visitPrologue(this) ; + public void visit(QueryVisitor visitor) { + visitor.startVisit(this); + visitor.visitResultForm(this); + visitor.visitPrologue(this); if ( this.isSelectType() ) - visitor.visitSelectResultForm(this) ; + visitor.visitSelectResultForm(this); if ( this.isConstructType() ) - visitor.visitConstructResultForm(this) ; + visitor.visitConstructResultForm(this); if ( this.isDescribeType() ) - visitor.visitDescribeResultForm(this) ; + visitor.visitDescribeResultForm(this); if ( this.isAskType() ) - visitor.visitAskResultForm(this) ; + visitor.visitAskResultForm(this); if ( this.isJsonType() ) - visitor.visitJsonResultForm(this) ; - visitor.visitDatasetDecl(this) ; - visitor.visitQueryPattern(this) ; - visitor.visitGroupBy(this) ; - visitor.visitHaving(this) ; - visitor.visitOrderBy(this) ; - visitor.visitOffset(this) ; - visitor.visitLimit(this) ; - visitor.visitValues(this) ; - visitor.finishVisit(this) ; + visitor.visitJsonResultForm(this); + visitor.visitDatasetDecl(this); + visitor.visitQueryPattern(this); + visitor.visitGroupBy(this); + visitor.visitHaving(this); + visitor.visitOrderBy(this); + visitor.visitOffset(this); + visitor.visitLimit(this); + visitor.visitValues(this); + visitor.finishVisit(this); } @Override - public Object clone() { return cloneQuery() ; } + public Object clone() { return cloneQuery(); } /** * Makes a copy of this query using the syntax transform machinery. @@ -785,25 +730,22 @@ public class Query extends Prologue implements Cloneable, Printable // "Equivalent" => gives the same results on any model @Override public String toString() - { return serialize() ; } + { return serialize(); } public String toString(Syntax syntax) - { return serialize(syntax) ; } - + { return serialize(syntax); } /** Must align with .equals */ - private int hashcode = -1 ; + private int hashcode = -1; @Override - public int hashCode() - { - if ( hashcode == -1 ) - { - hashcode = QueryHashCode.calc(this) ; + public int hashCode() { + if ( hashcode == -1 ) { + hashcode = QueryHashCode.calc(this); if ( hashcode == -1 ) - hashcode = Integer.MIN_VALUE/2 ; + hashcode = Integer.MIN_VALUE / 2; } - return hashcode ; + return hashcode; } /** Are two queries equals - tests shape and details. @@ -816,42 +758,35 @@ public class Query extends Prologue implements Cloneable, Printable * * Two instances of a query parsed from the same string are equal. */ - @Override - public boolean equals(Object other) - { - if ( ! ( other instanceof Query ) ) - return false ; - if ( this == other ) return true ; - return QueryCompare.equals(this, (Query)other) ; + public boolean equals(Object other) { + if ( !(other instanceof Query) ) + return false; + if ( this == other ) + return true; + return QueryCompare.equals(this, (Query)other); } -// public static boolean sameAs(Query query1, Query query2) -// { return query1.sameAs(query2) ; } - @Override - public void output(IndentedWriter out) - { - serialize(out) ; + public void output(IndentedWriter out) { + serialize(out); } /** Convert the query to a string */ - - public String serialize() - { - IndentedLineBuffer buff = new IndentedLineBuffer() ; - serialize(buff) ; + public String serialize() { + IndentedLineBuffer buff = new IndentedLineBuffer(); + serialize(buff); return buff.toString(); } - /** Convert the query to a string in the given syntax + /** + * Convert the query to a string in the given syntax + * * @param syntax */ - - public String serialize(Syntax syntax) - { - IndentedLineBuffer buff = new IndentedLineBuffer() ; - serialize(buff, syntax) ; + public String serialize(Syntax syntax) { + IndentedLineBuffer buff = new IndentedLineBuffer(); + serialize(buff, syntax); return buff.toString(); } @@ -865,30 +800,29 @@ public class Query extends Prologue implements Cloneable, Printable * @param out OutputStream * @param syntax Syntax URI */ - public void serialize(OutputStream out, Syntax syntax) { - IndentedWriter writer = new IndentedWriter(out) ; - serialize(writer, syntax) ; - writer.flush() ; - try { out.flush() ; } catch (Exception ex) { } + IndentedWriter writer = new IndentedWriter(out); + serialize(writer, syntax); + writer.flush(); + try { out.flush(); } catch (Exception ex) { } } - /** Format the query + /** + * Format the query * - * @param writer IndentedWriter + * @param writer IndentedWriter */ public void serialize(IndentedWriter writer) { serialize(writer, syntax); } - /** Format the query + /** + * Format the query * - * @param writer IndentedWriter - * @param outSyntax Syntax URI + * @param writer IndentedWriter + * @param outSyntax Syntax URI */ - - public void serialize(IndentedWriter writer, Syntax outSyntax) - { + public void serialize(IndentedWriter writer, Syntax outSyntax) { // Try to use a serializer factory if available QuerySerializerFactory factory = SerializerRegistry.get().getQuerySerializerFactory(outSyntax); QueryVisitor serializer = factory.create(outSyntax, this, writer); diff --git a/jena-arq/src/main/java/org/apache/jena/query/QueryVisitor.java b/jena-arq/src/main/java/org/apache/jena/query/QueryVisitor.java index 3d0afac43c..2595d13e0b 100644 --- a/jena-arq/src/main/java/org/apache/jena/query/QueryVisitor.java +++ b/jena-arq/src/main/java/org/apache/jena/query/QueryVisitor.java @@ -24,25 +24,25 @@ import org.apache.jena.sparql.core.Prologue ; public interface QueryVisitor { - public void startVisit(Query query) ; - public void visitPrologue(Prologue prologue) ; - - public void visitResultForm(Query query) ; - public void visitSelectResultForm(Query query) ; - public void visitConstructResultForm(Query query) ; - public void visitDescribeResultForm(Query query) ; - public void visitAskResultForm(Query query) ; - public void visitJsonResultForm(Query query); - - public void visitDatasetDecl(Query query) ; - public void visitQueryPattern(Query query) ; - - public void visitGroupBy(Query query) ; - public void visitHaving(Query query) ; - public void visitOrderBy(Query query) ; - public void visitLimit(Query query) ; - public void visitOffset(Query query) ; - public void visitValues(Query query) ; - - public void finishVisit(Query query) ; + public default void startVisit(Query query) {} + public default void visitPrologue(Prologue prologue) {} + + public default void visitResultForm(Query query) {} + public default void visitSelectResultForm(Query query) {} + public default void visitConstructResultForm(Query query) {} + public default void visitDescribeResultForm(Query query) {} + public default void visitAskResultForm(Query query) {} + public default void visitJsonResultForm(Query query) {} + + public default void visitDatasetDecl(Query query) {} + public default void visitQueryPattern(Query query) {} + + public default void visitGroupBy(Query query) {} + public default void visitHaving(Query query) {} + public default void visitOrderBy(Query query) {} + public default void visitLimit(Query query) {} + public default void visitOffset(Query query) {} + public default void visitValues(Query query) {} + + public default void finishVisit(Query query) {} }
