use of org.whole.lang.exceptions.WholeIllegalStateException in project whole by wholeplatform.
the class SelectIterator method lookahead.
public E lookahead() {
if (nextEntity != null)
return nextEntity;
IEntity selfEntityOld = getBindings().wGet("self");
lookaheadScope().setFilterEnabled(false);
clearLookaheadScope();
IEntity selfEntity = getFromIterator().lookahead();
if (selfEntity == null) {
lookaheadScope().setFilterEnabled(true);
return null;
}
getBindings().wEnterScope(lookaheadScope(), true);
getSelectIterator().reset(selfEntity);
lookaheadScope().wAddAll(getFromIterator().lookaheadScope());
Set<String> filterNames = lookaheadScope().getFilterNames();
for (String name : getFromIterator().lookaheadScope().wLocalNames()) filterNames.add(name);
lookaheadScope().wDef("self", selfEntity);
filterNames.add("self");
try {
nextEntity = getSelectIterator().next();
} catch (NoSuchElementException e) {
throw new WholeIllegalStateException("Select clause must return a value", e).withSourceEntity(getSourceEntity()).withBindings(getBindings());
}
lookaheadScope().wAddAll(getSelectIterator().lookaheadScope());
// enable select names hidden by from iterator
filterNames.removeAll(getSelectIterator().lookaheadScope().wLocalNames());
applyWhereClause(nextEntity, selfEntity, getBindings());
filterNames.addAll(getWhereIterator().lookaheadScope().wLocalNames());
lookaheadScope().setFilterEnabled(true);
getBindings().wExitScope();
if (!Matcher.removeVars(nextEntity, namesToBind(), withNamesComplement())) {
Set<String> unboundedNames = Matcher.vars(nextEntity, false);
if (withNamesComplement())
unboundedNames.removeAll(namesToBind());
else
unboundedNames.retainAll(namesToBind());
throw new MissingVariableException("Unbounded names in select clause: ", null, unboundedNames.toArray(new String[unboundedNames.size()])).withSourceEntity(getSourceEntity()).withBindings(getBindings());
}
if (selfEntityOld != null)
getBindings().wDef("self", selfEntityOld);
return nextEntity;
}
Aggregations