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; package org.apache.poi.ss.formula.eval;
import org.apache.poi.ss.usermodel.ErrorConstants; 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 { 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 */ /** <b>#NULL!</b> - Intersection of two cell ranges is empty */
@SuppressWarnings("static-access") public static final ErrorEval NULL_INTERSECTION = new ErrorEval(FormulaError.NULL);
public static final ErrorEval NULL_INTERSECTION = new ErrorEval(EC.ERROR_NULL);
/** <b>#DIV/0!</b> - Division by zero */ /** <b>#DIV/0!</b> - Division by zero */
@SuppressWarnings("static-access") public static final ErrorEval DIV_ZERO = new ErrorEval(FormulaError.DIV0);
public static final ErrorEval DIV_ZERO = new ErrorEval(EC.ERROR_DIV_0);
/** <b>#VALUE!</b> - Wrong type of operand */ /** <b>#VALUE!</b> - Wrong type of operand */
@SuppressWarnings("static-access") public static final ErrorEval VALUE_INVALID = new ErrorEval(FormulaError.VALUE);
public static final ErrorEval VALUE_INVALID = new ErrorEval(EC.ERROR_VALUE);
/** <b>#REF!</b> - Illegal or deleted cell reference */ /** <b>#REF!</b> - Illegal or deleted cell reference */
@SuppressWarnings("static-access") public static final ErrorEval REF_INVALID = new ErrorEval(FormulaError.REF);
public static final ErrorEval REF_INVALID = new ErrorEval(EC.ERROR_REF);
/** <b>#NAME?</b> - Wrong function or range name */ /** <b>#NAME?</b> - Wrong function or range name */
@SuppressWarnings("static-access") public static final ErrorEval NAME_INVALID = new ErrorEval(FormulaError.NAME);
public static final ErrorEval NAME_INVALID = new ErrorEval(EC.ERROR_NAME);
/** <b>#NUM!</b> - Value range overflow */ /** <b>#NUM!</b> - Value range overflow */
@SuppressWarnings("static-access") public static final ErrorEval NUM_ERROR = new ErrorEval(FormulaError.NUM);
public static final ErrorEval NUM_ERROR = new ErrorEval(EC.ERROR_NUM);
/** <b>#N/A</b> - Argument or function not available */ /** <b>#N/A</b> - Argument or function not available */
@SuppressWarnings("static-access") public static final ErrorEval NA = new ErrorEval(FormulaError.NA);
public static final ErrorEval NA = new ErrorEval(EC.ERROR_NA);
// POI internal error codes // POI internal error codes
private static final int CIRCULAR_REF_ERROR_CODE = 0xFFFFFFC4; 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 // 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); 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 * Translates an Excel internal error code into the corresponding POI ErrorEval instance
* @param errorCode * @param errorCode
@ -72,7 +59,7 @@ public final class ErrorEval implements ValueEval {
case ErrorConstants.ERROR_NAME: return NAME_INVALID; case ErrorConstants.ERROR_NAME: return NAME_INVALID;
case ErrorConstants.ERROR_NUM: return NUM_ERROR; case ErrorConstants.ERROR_NUM: return NUM_ERROR;
case ErrorConstants.ERROR_NA: return NA; 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; case CIRCULAR_REF_ERROR_CODE: return CIRCULAR_REF_ERROR;
} }
throw new RuntimeException("Unexpected error code (" + errorCode + ")"); 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. * @return the String representation of the specified Excel error code.
*/ */
public static String getText(int errorCode) { public static String getText(int errorCode) {
if(ErrorConstants.isValidCode(errorCode)) { if(FormulaError.isValidCode(errorCode)) {
return ErrorConstants.getText(errorCode); return FormulaError.forInt((byte)errorCode).getString();
} }
// It is desirable to make these (arbitrary) strings look clearly different from any other // 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 // 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) { private ErrorEval(int errorCode) {
_errorCode = errorCode; _errorCode = errorCode;
} }
private ErrorEval(FormulaError error) {
_errorCode = error.getCode();
}
public int getErrorCode() { public int getErrorCode() {
return _errorCode; 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) * 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 { public class ErrorConstants {
protected ErrorConstants() { protected ErrorConstants() {

View File

@ -21,8 +21,8 @@ import java.util.HashMap;
/** /**
* Enumerates error values in SpreadsheetML formula calculations. * Enumerates error values in SpreadsheetML formula calculations.
* *
* @author Yegor Kozlov * See also OOO's excelfileformat.pdf (2.5.6)
*/ */
public enum FormulaError { public enum FormulaError {
/** /**
@ -125,6 +125,13 @@ public enum FormulaError {
smap.put(error.getString(), error); 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){ public static FormulaError forInt(byte type){
FormulaError err = imap.get(type); FormulaError err = imap.get(type);