1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResult.java,v 1.11 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.11 $
  4. * $Date: 2004/02/21 17:10:29 $
  5. *
  6. * ====================================================================
  7. * Copyright 2001-2004 The Apache Software Foundation
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package org.apache.commons.validator;
  22. import java.io.Serializable;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. /**
  27. * This contains the results of a set of validation rules processed
  28. * on a JavaBean.
  29. */
  30. public class ValidatorResult implements Serializable {
  31. /**
  32. * Map of results. The key is the name of the <code>ValidatorAction</code>
  33. * and the value is whether or not this field passed or not.
  34. */
  35. protected Map hAction = new HashMap();
  36. /**
  37. * <code>Field</code> being validated.
  38. * TODO This variable is not used. Need to investigate removing it.
  39. */
  40. protected Field field = null;
  41. /**
  42. * Constructs a <code>ValidatorResult</code> with the associated field being
  43. * validated.
  44. */
  45. public ValidatorResult(Field field) {
  46. this.field = field;
  47. }
  48. /**
  49. * Add the result of a validator action.
  50. */
  51. public void add(String validatorName, boolean result) {
  52. this.add(validatorName, result, null);
  53. }
  54. /**
  55. * Add the result of a validator action.
  56. */
  57. public void add(String validatorName, boolean result, Object value) {
  58. hAction.put(validatorName, new ResultStatus(result, value));
  59. }
  60. public boolean containsAction(String validatorName) {
  61. return hAction.containsKey(validatorName);
  62. }
  63. public boolean isValid(String validatorName) {
  64. ResultStatus status = (ResultStatus) hAction.get(validatorName);
  65. return (status == null) ? false : status.isValid();
  66. }
  67. public Map getActionMap() {
  68. return Collections.unmodifiableMap(hAction);
  69. }
  70. /**
  71. * Returns the Field that was validated.
  72. */
  73. public Field getField() {
  74. return this.field;
  75. }
  76. /**
  77. * Contains the status of the validation.
  78. */
  79. protected class ResultStatus implements Serializable {
  80. private boolean valid = false;
  81. private Object result = null;
  82. public ResultStatus(boolean valid, Object result) {
  83. this.valid = valid;
  84. this.result = result;
  85. }
  86. /**
  87. * Gets whether or not the validation passed.
  88. * @deprecated Use isValid() instead.
  89. */
  90. public boolean getValid() {
  91. return valid;
  92. }
  93. /**
  94. * Tests whether or not the validation passed.
  95. */
  96. public boolean isValid() {
  97. return valid;
  98. }
  99. /**
  100. * Sets whether or not the validation passed.
  101. */
  102. public void setValid(boolean valid) {
  103. this.valid = valid;
  104. }
  105. /**
  106. * Gets the result returned by a validation method.
  107. * This can be used to retrieve to the correctly
  108. * typed value of a date validation for example.
  109. */
  110. public Object getResult() {
  111. return result;
  112. }
  113. /**
  114. * Sets the result returned by a validation method.
  115. * This can be used to retrieve to the correctly
  116. * typed value of a date validation for example.
  117. */
  118. public void setResult(Object result) {
  119. this.result = result;
  120. }
  121. }
  122. }