/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.hssf.usermodel.HSSFCell;
/**
* Instances of this class keep track of multiple dependent cell evaluations due
* to recursive calls to {@link WorkbookEvaluator#evaluate(HSSFCell)}
* The main purpose of this class is to detect an attempt to evaluate a cell
* that is already being evaluated. In other words, it detects circular
* references in spreadsheet formulas.
*
* @author Josh Micich
*/
final class EvaluationTracker {
private final List _evaluationFrames;
private final EvaluationCache _cache;
public EvaluationTracker(EvaluationCache cache) {
_cache = cache;
_evaluationFrames = new ArrayList();
}
/**
* Notifies this evaluation tracker that evaluation of the specified cell is
* about to start.
*
* In the case of a true
return code, the caller should
* continue evaluation of the specified cell, and also be sure to call
* endEvaluate() when complete.
*
* In the case of a false
return code, the caller should
* return an evaluation result of
* ErrorEval.CIRCULAR_REF_ERROR, and not call endEvaluate().
*
* @return true
if the specified cell has not been visited yet in the current
* evaluation. false
if the specified cell is already being evaluated.
*/
public ValueEval startEvaluate(int sheetIndex, int srcRowNum, int srcColNum) {
CellEvaluationFrame cef = new CellEvaluationFrame(sheetIndex, srcRowNum, srcColNum);
if (_evaluationFrames.contains(cef)) {
return ErrorEval.CIRCULAR_REF_ERROR;
}
ValueEval result = _cache.getValue(cef);
if (result == null) {
_evaluationFrames.add(cef);
}
return result;
}
/**
* Notifies this evaluation tracker that the evaluation of the specified
* cell is complete.