com.lillicoder.newsblurry.feeds.Feed.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.feeds.Feed.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lillicoder.newsblurry.feeds;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Model for a single feed followed by the user.
 * @author lillicoder
 */
public class Feed implements IFeed {

    /**
     * Serialization ID.
     */
    private static final long serialVersionUID = 2247849789977903921L;

    private static final String EXCEPTION_CHILDREN_UNSUPPORTED = "The Feed class never contains any children, this method is unsupported.";

    private static final String JSON_KEY_ID = "id";

    private static final String JSON_KEY_NAME = "name";

    private int _id;
    private String _name;
    private Favicon _favicon;
    private UnreadCounts _unreadCounts;

    //private String _feedLink; // <-- main site url
    //private String _feedUrl; // <-- rss feed url

    public Feed(int id, String name, Favicon favicon, UnreadCounts unreadCounts) {
        this.setId(id);
        this.setName(name);
        this.setFavicon(favicon);
        this.setUnreadCounts(unreadCounts);
    }

    @Override
    public int compareTo(IFeed another) {
        if (another == null)
            return 1;

        // Feeds should come before any feed with children.
        if (another.hasChildren())
            return 1;

        // Sort by name (alphabetical).
        String name = this.getName();
        String anotherName = another.getName();
        return name.compareTo(anotherName);
    }

    @Override
    public List<IFeed> getChildren() {
        // By convention, this model represents
        // a child-less feed.
        throw new UnsupportedOperationException(EXCEPTION_CHILDREN_UNSUPPORTED);
    }

    @Override
    public boolean hasChildren() {
        // By convention this model represents 
        // a child-less feed.
        return false;
    }

    /**
     * Gets a {@link JSONObject} representation of this feed.
     * Note that this representation is not compatible with
     * NewsBlur feeds JSON data, this is for internal app usage.
     * @return {@link JSONObject} representation of this feed.
     * @throws JSONException Thrown if there is an error writing feed values to JSON.
     */
    public JSONObject toJson() throws JSONException {
        JSONObject feedJson = new JSONObject();

        feedJson.put(JSON_KEY_ID, this.getId());
        feedJson.put(JSON_KEY_NAME, this.getName());

        return feedJson;
    }

    /**
     * Gets the ID for this feed.
     * @return Feed ID.
     */
    public int getId() {
        return this._id;
    }

    @Override
    public Favicon getFavicon() {
        return this._favicon;
    }

    @Override
    public String getName() {
        return this._name;
    }

    @Override
    public UnreadCounts getUnreadCounts() {
        return this._unreadCounts;
    }

    /**
     * Sets the {@link Favicon} for this feed.
     * @param favicon Feed {@link Favicon}.
     */
    private void setFavicon(Favicon favicon) {
        this._favicon = favicon;
    }

    /**
     * Sets the ID for this feed.
     * @param id Feed ID.
     */
    private void setId(int id) {
        this._id = id;
    }

    /**
     * Sets the name for this feed.
     * @param name Feed name.
     */
    private void setName(String name) {
        this._name = name;
    }

    /**
     * Sets the {@link UnreadCounts} for this feed.
     * @param unreadCounts Feed {@link UnreadCounts}.
     */
    private void setUnreadCounts(UnreadCounts unreadCounts) {
        this._unreadCounts = unreadCounts;
    }

}