<R> R applyAndCreateRoot(UnaryOperator<Expression> fn, Combiner nextCombiner) {
    Expression newPartial = fn.apply(state.partial());
    Expression newExpression = state.combiner().combine(state.current(), newPartial);
    newPartial = newExpression;
    CriteriaContext context = this.previous;

    while (context != null) {
      Expression tmpExpression = context.state.combiner().combine(context.state.current(), newPartial);
      if (isPathExtention(tmpExpression, newExpression)) {
        newExpression = tmpExpression;
        newPartial = tmpExpression;
      }

      context = context.previous;
    }
    
    // use initial creator
    CriteriaCreator<?> creator = first().state.creator();

    ImmutableState newState = state.withCombiner(nextCombiner)
            .withCreator(creator)
            .withCurrent(newExpression)
            .withPartial(state.defaultPartial());

    return new CriteriaContext(null, newState).create();
  }
Used by Aggregator Matchers (sum / count / avg etc.)
criteria/common/src/org/immutables/criteria/matcher/CriteriaContext.java