Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ErrorConstant.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.constant;
  16. import org.apache.poi.ss.usermodel.FormulaError;
  17. import org.apache.poi.util.POILogFactory;
  18. import org.apache.poi.util.POILogger;
  19. /**
  20. * Represents a constant error code value as encoded in a constant values array. <p>
  21. *
  22. * This class is a type-safe wrapper for a 16-bit int value performing a similar job to
  23. * <tt>ErrorEval</tt>.
  24. */
  25. public final class ErrorConstant {
  26. private static final POILogger LOG = POILogFactory.getLogger(ErrorConstant.class);
  27. private static final ErrorConstant NULL = new ErrorConstant(FormulaError.NULL.getCode());
  28. private static final ErrorConstant DIV_0 = new ErrorConstant(FormulaError.DIV0.getCode());
  29. private static final ErrorConstant VALUE = new ErrorConstant(FormulaError.VALUE.getCode());
  30. private static final ErrorConstant REF = new ErrorConstant(FormulaError.REF.getCode());
  31. private static final ErrorConstant NAME = new ErrorConstant(FormulaError.NAME.getCode());
  32. private static final ErrorConstant NUM = new ErrorConstant(FormulaError.NUM.getCode());
  33. private static final ErrorConstant NA = new ErrorConstant(FormulaError.NA.getCode());
  34. private final int _errorCode;
  35. private ErrorConstant(int errorCode) {
  36. _errorCode = errorCode;
  37. }
  38. public int getErrorCode() {
  39. return _errorCode;
  40. }
  41. public String getText() {
  42. if(FormulaError.isValidCode(_errorCode)) {
  43. return FormulaError.forInt(_errorCode).getString();
  44. }
  45. return "unknown error code (" + _errorCode + ")";
  46. }
  47. public static ErrorConstant valueOf(int errorCode) {
  48. if (FormulaError.isValidCode(errorCode)) {
  49. switch (FormulaError.forInt(errorCode)) {
  50. case NULL: return NULL;
  51. case DIV0: return DIV_0;
  52. case VALUE: return VALUE;
  53. case REF: return REF;
  54. case NAME: return NAME;
  55. case NUM: return NUM;
  56. case NA: return NA;
  57. default: break;
  58. }
  59. }
  60. LOG.log( POILogger.WARN, "Warning - unexpected error code (", errorCode, ")");
  61. return new ErrorConstant(errorCode);
  62. }
  63. public String toString() {
  64. StringBuilder sb = new StringBuilder(64);
  65. sb.append(getClass().getName()).append(" [");
  66. sb.append(getText());
  67. sb.append("]");
  68. return sb.toString();
  69. }
  70. }