View Javadoc

1   /**
2    * This file Copyright (c) 2008-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.importhandler;
35  
36  import com.sun.syndication.feed.synd.SyndFeed;
37  import info.magnolia.module.rssaggregator.util.Assert;
38  import org.apache.commons.lang.builder.ToStringBuilder;
39  
40  /**
41   * A {@link #name named} feed channel that typically belongs to an {@link AggregateFeed}. Knows the {@link #url url} on
42   * which a given RSS or ATOM feed resides and {@link #feed holds} the {@link SyndFeed feed} that was fetched after
43   * succesful retrieval by a {@link RSSFeedFetcher}.
44   *
45   * @author Rob van der Linden Vooren
46   * @see AggregateFeed
47   * @see RSSFeedFetcher
48   * @see SyndFeed
49   */
50  public class FeedChannel {
51  
52      private final String name;
53      private final String url;
54      private final String title;
55      private SyndFeed feed;
56  
57      /**
58       * Create a new feed channel from the given <code>name</code>, <code>url</code> and <code>title</code>.
59       *
60       * @param name the name of the feed channel (e.g.: 'channel-0')
61       * @param url the url of the feed channel (e.g.: 'http://robl.jteam.nl/?feed=rss2'
62       * @param title the title of the feed channel (e.g.: 'I'll take the red pill ...')
63       * @throws IllegalArgumentException if the given <code>name</code> or <code>url</code> provided is blank, or
64       * <code>title</code> is <code>null</code>
65       */
66      public FeedChannel(String name, String url, String title) {
67          Assert.notBlank(name, "'name' must not be blank");
68          Assert.notBlank(url, "'url' must not be blank");
69          Assert.notNull(title, "'title' must not be null");
70          this.name = name;
71          this.url = url;
72          this.title = title;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public boolean equals(Object o) {
78          if (this == o) return true;
79          if (o == null || getClass() != o.getClass()) return false;
80  
81          FeedChannel that = (FeedChannel) o;
82  
83          if (feed != null ? !feed.equals(that.feed) : that.feed != null) return false;
84          if (!name.equals(that.name)) return false;
85          if (!title.equals(that.title)) return false;
86          if (!url.equals(that.url)) return false;
87  
88          return true;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public int hashCode() {
94          int result = name.hashCode();
95          result = 31 * result + url.hashCode();
96          result = 31 * result + title.hashCode();
97          result = 31 * result + (feed != null ? feed.hashCode() : 0);
98          return result;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public String toString() {
104         return new ToStringBuilder(this)
105                 .append("name", name)
106                 .append("url", url)
107                 .append("title", title)
108                 .append("feed", feed)
109                 .toString();
110     }
111 
112     //  Helper methods
113 
114     /**
115      * Return <code>true</code> when this feed channel contains a {@link #feed}, <code>false</code> otherwise.
116      *
117      * @return true when this feed channel contains a feed, false otherwise
118      */
119     public boolean hasFeed() {
120         return feed != null;
121     }
122 
123     //  Getters & setters
124 
125     /**
126      * Return the name of the feed channel.
127      *
128      * @return the name of the feed channel
129      */
130     public String getName() {
131         return name;
132     }
133 
134     /**
135      * Return the url which points to the RSS or ATOM feed for this feed channel.
136      *
137      * @return the url which points to the RSS or ATOM feed for this feed channel
138      */
139     public String getUrl() {
140         return url;
141     }
142 
143     /**
144      * Return the title of the feed channel.
145      *
146      * @return the title of the feed channel
147      */
148     public String getTitle() {
149         return title;
150     }
151 
152     /**
153      * Set the feed (fetch result) for this feed channel.
154      *
155      * @param feed the feed (fetch result) for this feed channel (must not be null)
156      * @throws IllegalArgumentException if the given feed is <code>null</code>
157      */
158     public void setFeed(SyndFeed feed) {
159         Assert.notNull(feed, "'feed' must not be null");
160         this.feed = feed;
161     }
162 
163     /**
164      * Return the feed (fetch result) for this feed channel.
165      *
166      * @return the feed (fetch result) for this feed channel or <code>null</code> if no result was set
167      */
168     public SyndFeed getFeed() {
169         return feed;
170     }
171 
172 }