KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.apache.cayenne.validation;
22
23 /**
24  * Represents a generic validation failure that contains
25  * failed object and a message describing the failure.
26  *
27  * @since 1.1
28  * @author Andrus Adamchik
29  */

30 public class SimpleValidationFailure implements ValidationFailure {
31     protected Object JavaDoc source;
32     protected Object JavaDoc error;
33
34     public SimpleValidationFailure(Object JavaDoc source, Object JavaDoc error) {
35         this.source = source;
36         this.error = error;
37     }
38
39     /**
40      * Returns the error converted to String.
41      */

42     public String JavaDoc getDescription() {
43         return String.valueOf(error);
44     }
45
46     /**
47      * Returns object that failed the validation.
48      */

49     public Object JavaDoc getSource() {
50         return source;
51     }
52
53     public Object JavaDoc getError() {
54         return error;
55     }
56
57     /**
58      * Returns a String representation of the failure.
59      */

60     public String JavaDoc toString() {
61         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
62
63         buffer.append("Validation failure for ");
64         Object JavaDoc source = getSource();
65
66         if (source == null) {
67             buffer.append("[General]");
68         }
69         else {
70             String JavaDoc sourceLabel = (source instanceof String JavaDoc) ? source.toString() : source
71                     .getClass()
72                     .getName();
73             buffer.append(sourceLabel);
74         }
75         buffer.append(": ");
76         buffer.append(getDescription());
77         return buffer.toString();
78     }
79 }
80
Popular Tags