KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > validation > ValidationResult


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.validation;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * Represents a result of a validation execution. Contains a set of
32  * {@link ValidationFailure ValidationFailures}that occured in a given context. All
33  * failures are kept in the same order they were added.
34  *
35  * @author Fabricio Voznika
36  * @since 1.1
37  */

38 public class ValidationResult implements Serializable JavaDoc {
39
40     private List JavaDoc failures;
41
42     public ValidationResult() {
43         failures = new ArrayList JavaDoc();
44     }
45
46     /**
47      * Add a failure to the validation result.
48      *
49      * @param failure failure to be added. It may not be null.
50      * @see ValidationFailure
51      */

52     public void addFailure(ValidationFailure failure) {
53         if (failure == null) {
54             throw new IllegalArgumentException JavaDoc("failure cannot be null.");
55         }
56
57         failures.add(failure);
58     }
59
60     /**
61      * Returns all failures added to this result, or empty list is result has no failures.
62      */

63     public List JavaDoc getFailures() {
64         return Collections.unmodifiableList(failures);
65     }
66
67     /**
68      * Returns all failures related to the <code>source</code> object, or an empty list
69      * if there are no such failures.
70      *
71      * @param source it may be null.
72      * @see ValidationFailure#getSource()
73      */

74     public List JavaDoc getFailures(Object JavaDoc source) {
75
76         ArrayList JavaDoc matchingFailures = new ArrayList JavaDoc(5);
77         Iterator JavaDoc it = failures.iterator();
78         while (it.hasNext()) {
79             ValidationFailure failure = (ValidationFailure) it.next();
80             if (Util.nullSafeEquals(source, failure.getSource())) {
81                 matchingFailures.add(failure);
82             }
83         }
84
85         return matchingFailures;
86     }
87
88     /**
89      * Returns true if at least one failure has been added to this result. False
90      * otherwise.
91      */

92     public boolean hasFailures() {
93         return !failures.isEmpty();
94     }
95
96     /**
97      * @param source it may be null.
98      * @return true if there is at least one failure for <code>source</code>. False
99      * otherwise.
100      */

101     public boolean hasFailures(Object JavaDoc source) {
102         Iterator JavaDoc it = failures.iterator();
103         while (it.hasNext()) {
104             ValidationFailure failure = (ValidationFailure) it.next();
105             if (Util.nullSafeEquals(source, failure.getSource())) {
106                 return true;
107             }
108         }
109
110         return false;
111     }
112
113     public String JavaDoc toString() {
114         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
115         String JavaDoc separator = System.getProperty("line.separator");
116
117         Iterator JavaDoc it = getFailures().iterator();
118         while (it.hasNext()) {
119             if (ret.length() > 0) {
120                 ret.append(separator);
121             }
122
123             ret.append(it.next());
124         }
125
126         return ret.toString();
127     }
128 }
129
Popular Tags