Reduce duplication between FormulaError and ErrorConstants

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1658185 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-02-08 15:17:20 +00:00
parent 25385edc38
commit 557eade297
3 changed files with 25 additions and 28 deletions

View File

@ -18,38 +18,26 @@
package org.apache.poi.ss.formula.eval;
import org.apache.poi.ss.usermodel.ErrorConstants;
import org.apache.poi.ss.usermodel.FormulaError;
/**
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
*
* Evaluations for formula errors
*/
public final class ErrorEval implements ValueEval {
// convenient access to namespace
private static final ErrorConstants EC = null;
/** <b>#NULL!</b> - Intersection of two cell ranges is empty */
@SuppressWarnings("static-access")
public static final ErrorEval NULL_INTERSECTION = new ErrorEval(EC.ERROR_NULL);
public static final ErrorEval NULL_INTERSECTION = new ErrorEval(FormulaError.NULL);
/** <b>#DIV/0!</b> - Division by zero */
@SuppressWarnings("static-access")
public static final ErrorEval DIV_ZERO = new ErrorEval(EC.ERROR_DIV_0);
public static final ErrorEval DIV_ZERO = new ErrorEval(FormulaError.DIV0);
/** <b>#VALUE!</b> - Wrong type of operand */
@SuppressWarnings("static-access")
public static final ErrorEval VALUE_INVALID = new ErrorEval(EC.ERROR_VALUE);
public static final ErrorEval VALUE_INVALID = new ErrorEval(FormulaError.VALUE);
/** <b>#REF!</b> - Illegal or deleted cell reference */
@SuppressWarnings("static-access")
public static final ErrorEval REF_INVALID = new ErrorEval(EC.ERROR_REF);
public static final ErrorEval REF_INVALID = new ErrorEval(FormulaError.REF);
/** <b>#NAME?</b> - Wrong function or range name */
@SuppressWarnings("static-access")
public static final ErrorEval NAME_INVALID = new ErrorEval(EC.ERROR_NAME);
public static final ErrorEval NAME_INVALID = new ErrorEval(FormulaError.NAME);
/** <b>#NUM!</b> - Value range overflow */
@SuppressWarnings("static-access")
public static final ErrorEval NUM_ERROR = new ErrorEval(EC.ERROR_NUM);
public static final ErrorEval NUM_ERROR = new ErrorEval(FormulaError.NUM);
/** <b>#N/A</b> - Argument or function not available */
@SuppressWarnings("static-access")
public static final ErrorEval NA = new ErrorEval(EC.ERROR_NA);
public static final ErrorEval NA = new ErrorEval(FormulaError.NA);
// POI internal error codes
private static final int CIRCULAR_REF_ERROR_CODE = 0xFFFFFFC4;
@ -58,7 +46,6 @@ public final class ErrorEval implements ValueEval {
// Note - Excel does not seem to represent this condition with an error code
public static final ErrorEval CIRCULAR_REF_ERROR = new ErrorEval(CIRCULAR_REF_ERROR_CODE);
/**
* Translates an Excel internal error code into the corresponding POI ErrorEval instance
* @param errorCode
@ -72,7 +59,7 @@ public final class ErrorEval implements ValueEval {
case ErrorConstants.ERROR_NAME: return NAME_INVALID;
case ErrorConstants.ERROR_NUM: return NUM_ERROR;
case ErrorConstants.ERROR_NA: return NA;
// non-std errors (conditions modeled as errors by POI)
// non-std errors (conditions modelled as errors by POI)
case CIRCULAR_REF_ERROR_CODE: return CIRCULAR_REF_ERROR;
}
throw new RuntimeException("Unexpected error code (" + errorCode + ")");
@ -84,8 +71,8 @@ public final class ErrorEval implements ValueEval {
* @return the String representation of the specified Excel error code.
*/
public static String getText(int errorCode) {
if(ErrorConstants.isValidCode(errorCode)) {
return ErrorConstants.getText(errorCode);
if(FormulaError.isValidCode(errorCode)) {
return FormulaError.forInt((byte)errorCode).getString();
}
// It is desirable to make these (arbitrary) strings look clearly different from any other
// value expression that might appear in a formula. In addition these error strings should
@ -104,6 +91,9 @@ public final class ErrorEval implements ValueEval {
private ErrorEval(int errorCode) {
_errorCode = errorCode;
}
private ErrorEval(FormulaError error) {
_errorCode = error.getCode();
}
public int getErrorCode() {
return _errorCode;

View File

@ -20,7 +20,7 @@ package org.apache.poi.ss.usermodel;
/**
* Contains raw Excel error codes (as defined in OOO's excelfileformat.pdf (2.5.6)
*
* @author Michael Harhen
* @deprecated Use {@link FormulaError} instead where possible
*/
public class ErrorConstants {
protected ErrorConstants() {

View File

@ -21,8 +21,8 @@ import java.util.HashMap;
/**
* Enumerates error values in SpreadsheetML formula calculations.
*
* @author Yegor Kozlov
*
* See also OOO's excelfileformat.pdf (2.5.6)
*/
public enum FormulaError {
/**
@ -125,6 +125,13 @@ public enum FormulaError {
smap.put(error.getString(), error);
}
}
public static final boolean isValidCode(int errorCode) {
for (FormulaError error : values()) {
if (error.getCode() == errorCode) return true;
}
return false;
}
public static FormulaError forInt(byte type){
FormulaError err = imap.get(type);