KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > util > ViolatedPrecondition


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/ViolatedPrecondition.java,v 1.11 2004/08/05 14:43:30 dflorey Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 14:43:30 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.webdav.util;
24
25 // import list
26

27 /**
28  * This class encapsulates the status code and name of the precondition
29  * that has been violated.
30  *
31  * @version $Revision: 1.11 $
32  *
33  **/

34 public class ViolatedPrecondition {
35
36     /**
37      * String constant for the message of the IllegalArgumentException
38      * that might be thrown in {@link #ViolatedPrecondition the constructor}
39      * if the parameter <code>precondition</code> is <code>null</code>.
40      */

41     public static final String JavaDoc PRECONDITION_MUST_NOT_BE_NULL = "Parameter 'precondition' must not be null";
42     
43     
44     /**
45      * The status of the violated precondition.
46      * Must be either <code>403 (Forbidden)</code> or
47      * <code>409 (Conflict)</code>.
48      */

49     protected int statusCode = WebdavStatus.SC_FORBIDDEN;
50     
51     /**
52      * The precondition that has been violated.
53      */

54     protected String JavaDoc precondition = null;
55     protected String JavaDoc explanation = null;
56
57     /**
58      * The String representation returned by {@link #toString toString()}.
59      */

60     protected String JavaDoc stringRepresentation = null;
61     
62     
63     
64     /**
65      * Creates a ViolatedPrecondition.
66      *
67      * @pre precondition != null
68      * @pre (statusCode == 403) || (statusCode == 409)
69      *
70      * @param precondition the name of the violated precondition. Must not
71      * be <code>null</code>.
72      * @param statusCode the status of the violated precondition.
73      * Must be either <code>403 (Forbidden)</code> or
74      * <code>409 (Conflict)</code>.
75      *
76      * @throws IllegalArgumentException if the <code>precondition</code> is
77      * <code>null</code>, or if the parameter
78      * <code>statusCode</code> is neither
79      * <code>403 (Forbidden)</code>
80      * nor <code>409 (Conflict)</code>.
81      */

82     public ViolatedPrecondition(String JavaDoc precondition, int statusCode) {
83         this(precondition, statusCode, null);
84     }
85     
86     public ViolatedPrecondition(String JavaDoc precondition, int statusCode, String JavaDoc explanation) {
87         
88         if (precondition == null) {
89             throw new IllegalArgumentException JavaDoc(PRECONDITION_MUST_NOT_BE_NULL);
90         }
91
92         this.precondition = precondition;
93         this.statusCode = statusCode;
94         this.explanation = explanation;
95     }
96     
97     /**
98      * Returns the name of the violated precondition.
99      *
100      * @return the name of the violated precondition.
101      */

102     public String JavaDoc getPrecondition() {
103         return precondition;
104     }
105     
106     /**
107      * Returns the status of the violated precondition.
108      *
109      * @return the status of the violated precondition.
110      */

111     public int getStatusCode() {
112         return statusCode;
113     }
114     
115     public void setExplanation(String JavaDoc explanation) {
116         this.explanation = explanation;
117     }
118     
119     public String JavaDoc getExplanation() {
120         return explanation;
121     }
122     
123     /**
124      * Returns <code>true</code> if the <code>other</code> Object is a
125      * ViolatedPrecondition and if both their precondition and status code
126      * are equal.
127      *
128      * @param other the Object to test for equality.
129      *
130      * @return <code>true</code> if the other Object is equal to this one.
131      */

132     public boolean equals(Object JavaDoc other) {
133         
134         boolean isEqual = false;
135         if (other instanceof ViolatedPrecondition) {
136             ViolatedPrecondition otherViolatedPrecondition = (ViolatedPrecondition)other;
137             isEqual = getPrecondition().equals(otherViolatedPrecondition.getPrecondition());
138             isEqual &= ( getStatusCode() == otherViolatedPrecondition.getStatusCode() );
139         }
140         
141         return isEqual;
142     }
143     
144     /**
145      * Returns the hash code. Due to definition equal objects must have
146      * the same hash code.
147      *
148      * @return the hash code.
149      */

150     public int hashCode() {
151         return 13*getPrecondition().hashCode() + getStatusCode();
152     }
153     
154     /**
155      * Returns the String representation of this Object.
156      *
157      * @return the String representation of this Object.
158      */

159     public String JavaDoc toString() {
160         
161         if (stringRepresentation == null) {
162             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("ViolatedPrecondition[");
163             buffer.append(getPrecondition());
164             buffer.append(", ");
165             buffer.append(getStatusCode());
166             buffer.append(" ");
167             buffer.append(WebdavStatus.getStatusText(getStatusCode()));
168             buffer.append("]");
169             stringRepresentation = buffer.toString();
170         }
171         return stringRepresentation;
172     }
173 }
174
175
Popular Tags