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.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import net.sf.urlchecker.events.ChainListener;
27  
28  import org.apache.commons.collections.IteratorUtils;
29  import org.apache.commons.collections.PredicateUtils;
30  import org.apache.commons.lang.builder.EqualsBuilder;
31  import org.apache.commons.lang.builder.HashCodeBuilder;
32  
33  /**
34   * The Context to pass all information through the commands. The context
35   * contains the Listeners of events, the results of each stage of processing and
36   * the original source of target URLs.
37   * 
38   * <p>
39   * <b> $Id: Context.java 184 2010-12-17 21:23:28Z georgosn $</b>
40   * </p>
41   * 
42   * @author $LastChangedBy: georgosn $
43   * @version $LastChangedRevision: 184 $
44   */
45  public abstract class Context {
46  
47      /** The results. */
48      private final Set<Result>        results;
49  
50      /** The source. */
51      private Readable                 source;
52  
53      /** The listeners. */
54      private final Set<ChainListener> listeners;
55  
56      /**
57       * Instantiates a new context.
58       */
59      public Context() {
60          results = new HashSet<Result>();
61          listeners = new HashSet<ChainListener>();
62      }
63  
64      /**
65       * Adds the listener.
66       * 
67       * @param newListener
68       *            the new listener
69       */
70      public void addListener(final ChainListener newListener) {
71          listeners.add(newListener);
72      }
73  
74      /**
75       * Clear listeners.
76       */
77      public void clearListeners() {
78          listeners.clear();
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see java.lang.Object#equals(java.lang.Object)
85       */
86      /** {@inheritDoc} */
87      @Override
88      public boolean equals(final Object obj) {
89          if (this == obj) {
90              return true;
91          }
92          if (obj == null) {
93              return false;
94          }
95          if (getClass() != obj.getClass()) {
96              return false;
97          }
98          final Context other = (Context) obj;
99          return new EqualsBuilder().append(source, other.getSource())
100                 .append(results, other.getResults()).isEquals();
101     }
102 
103     /**
104      * Returns the listeners.
105      * 
106      * @return the listeners
107      */
108     public Set<ChainListener> getListeners() {
109         return new HashSet<ChainListener>(listeners);
110     }
111 
112     /**
113      * Gets the results.
114      * 
115      * @return the results
116      */
117     public Set<Result> getResults() {
118         return results;
119     }
120 
121     /**
122      * Gets the source.
123      * 
124      * @return the source
125      */
126     public Readable getSource() {
127         return source;
128     }
129 
130     /*
131      * (non-Javadoc)
132      * 
133      * @see java.lang.Object#hashCode()
134      */
135     /** {@inheritDoc} */
136     @Override
137     public int hashCode() {
138         return new HashCodeBuilder().append(results).hashCode();
139     }
140 
141     /**
142      * Removes the listener.
143      * 
144      * @param oldListener
145      *            the old listener
146      * @return true, if successful
147      */
148     public boolean removeListener(final ChainListener oldListener) {
149         return listeners.remove(oldListener);
150     }
151 
152     /**
153      * Sets the source.
154      * 
155      * @param aSource
156      *            the source to set
157      */
158     public void setSource(final Readable aSource) {
159         this.source = aSource;
160     }
161 
162     /**
163      * Valid results iterator.
164      * 
165      * @return the iterator
166      */
167     public Iterator<Result> validResultsIterator() {
168         // construct an iterator only for the valid results in order to
169         // avoid unnecessary if statements
170         return IteratorUtils.filteredIterator(results.iterator(),
171                 PredicateUtils.invokerPredicate("isValid"));
172     }
173 }