View Javadoc

1   /*
2    * (C)opyright 2010, Nikolaos Georgosopoulos
3    *
4    * This file is part of URLChecker.
5   
6       URLChecker is free software: you can redistribute it and/or modify
7       it under the terms of the Lesser General Public License as published by
8       the Free Software Foundation, either version 3 of the License, or
9       (at your option) any later version.
10  
11      URLChecker is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the Lesser General Public License
17      along with URLChecker.  If not, see <http://www.gnu.org/licenses/>.
18  
19   */
20  package net.sf.urlchecker.commands;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.text.MessageFormat;
25  
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  
29  /**
30   * The Result Class. It encapsulates all information about a target URL, the
31   * target URL, the returned result code, whether or not it is a valid result and
32   * an arbitrary object to store further information should they be needed while
33   * processing the URL. Should the target URL is unreachable due to an error, the
34   * userData object will contain the throwable object occurring due to the
35   * exception.
36   * 
37   * <p>
38   * <b> $Id: Result.java 181 2010-12-12 23:39:00Z georgosn $</b>
39   * </p>
40   * 
41   * @author $LastChangedBy: georgosn $
42   * @version $LastChangedRevision: 181 $
43   */
44  public class Result {
45  
46      /** The valid. */
47      private boolean valid;
48  
49      /** The target. */
50      private String  target;
51  
52      /** The URI. */
53      private URI     uri;
54  
55      /** The user data. */
56      private Object  userData;
57  
58      /** The result. */
59      // TODO how about a real object for the result, then we could return all
60      // sorts of things. it would change the interface though!
61      private int     result;
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see java.lang.Object#equals(java.lang.Object)
67       */
68      /** {@inheritDoc} */
69      @Override
70      public boolean equals(final Object obj) {
71          if (obj == null) {
72              return false;
73          }
74          if (obj == this) {
75              return true;
76          }
77          if (obj.getClass() != getClass()) {
78              return false;
79          }
80          final Result remote = (Result) obj;
81          return new EqualsBuilder().append(target, remote.getTarget())
82                  .append(uri, remote.getURI()).isEquals();
83      }
84  
85      /**
86       * Gets the result.
87       * 
88       * @return the result
89       */
90      public int getResult() {
91          return result;
92      }
93  
94      /**
95       * Gets the target.
96       * 
97       * @return the target
98       */
99      public String getTarget() {
100         return target;
101     }
102 
103     /**
104      * Gets the URI.
105      * 
106      * @return the URI
107      */
108     public URI getURI() {
109         return uri;
110     }
111 
112     /**
113      * Gets the user data.
114      * 
115      * @return the user data
116      */
117     public Object getUserData() {
118         return userData;
119     }
120 
121     /*
122      * (non-Javadoc)
123      * 
124      * @see java.lang.Object#hashCode()
125      */
126     /** {@inheritDoc} */
127     @Override
128     public int hashCode() {
129         return new HashCodeBuilder().append(target).append(uri).hashCode();
130     }
131 
132     /**
133      * Checks if is valid.
134      * 
135      * @return true, if is valid
136      */
137     public boolean isValid() {
138         return valid;
139     }
140 
141     /**
142      * Sets the result.
143      * 
144      * @param aResult
145      *            the result to set
146      */
147     public void setResult(final int aResult) {
148         this.result = aResult;
149     }
150 
151     /**
152      * Sets the target.
153      * 
154      * @param aTarget
155      *            the target to set
156      */
157     public void setTarget(final String aTarget) {
158         this.target = aTarget;
159         try {
160             uri = new URI(target);
161         } catch (final URISyntaxException e) {
162             uri = null;
163         } finally {
164             valid = uri != null;
165         }
166     }
167 
168     /**
169      * Sets the user data.
170      * 
171      * @param someUserData
172      *            the userData to set
173      */
174     public void setUserData(final Object someUserData) {
175         this.userData = someUserData;
176     }
177 
178     /**
179      * Sets the valid.
180      * 
181      * @param value
182      *            the valid to set
183      */
184     public void setValid(final boolean value) {
185         this.valid = value;
186     }
187 
188     /*
189      * (non-Javadoc)
190      * 
191      * @see java.lang.Object#toString()
192      */
193     /** {@inheritDoc} */
194     @Override
195     public String toString() {
196         return MessageFormat
197                 .format("Result [target={0}, url={1}, userData={2}, valid={3}, result={4}",
198                         new Object[] { target, uri, userData, valid, result });
199     }
200 }