com.wellsandwhistles.android.redditsp.reddit.things.RedditComment.java Source code

Java tutorial

Introduction

Here is the source code for com.wellsandwhistles.android.redditsp.reddit.things.RedditComment.java

Source

package com.wellsandwhistles.android.redditsp.reddit.things;

/** This file was either copied or modified from https://github.com/QuantumBadger/RedReader
 * under the Free Software Foundation General Public License version 3*/

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import org.apache.commons.lang3.StringEscapeUtils;
import com.wellsandwhistles.android.redditsp.common.LinkHandler;
import com.wellsandwhistles.android.redditsp.jsonwrap.JsonValue;
import com.wellsandwhistles.android.redditsp.reddit.url.PostCommentListingURL;

import java.util.HashSet;

public final class RedditComment implements Parcelable, RedditThingWithIdAndType {

    public String body, body_html, author, subreddit;
    public String author_flair_text;
    public Boolean archived, likes, score_hidden;

    public JsonValue replies;

    public String id, subreddit_id, link_id, parent_id, name, context;

    public int ups, downs;
    public int gilded;

    public Object edited;

    public long created, created_utc;

    public Boolean saved;

    public String distinguished;

    public RedditComment() {
    }

    // one of the many reasons why the Android API is awful
    private RedditComment(final Parcel in) {

        body = in.readString();
        body_html = in.readString();
        author = in.readString();
        subreddit = in.readString();
        author_flair_text = in.readString();

        archived = in.readInt() == 1;
        switch (in.readInt()) {
        case -1:
            likes = false;
            break;
        case 0:
            likes = null;
            break;
        case 1:
            likes = true;
            break;
        }

        replies = null;

        id = in.readString();
        subreddit_id = in.readString();
        link_id = in.readString();
        parent_id = in.readString();
        name = in.readString();
        context = in.readString();

        ups = in.readInt();
        downs = in.readInt();

        final long in_edited = in.readLong();
        if (in_edited == -1) {
            edited = false;
        } else {
            edited = in_edited;
        }

        created = in.readLong();
        created_utc = in.readLong();

        saved = in.readInt() != 0;
        gilded = in.readInt();

        distinguished = in.readString();
    }

    public void writeToParcel(final Parcel parcel, final int flags) {

        parcel.writeString(body);
        parcel.writeString(body_html);
        parcel.writeString(author);
        parcel.writeString(subreddit);
        parcel.writeString(author_flair_text);
        parcel.writeInt(archived ? 1 : 0);

        if (likes == null) {
            parcel.writeInt(0);
        } else {
            parcel.writeInt(likes ? 1 : -1);
        }

        parcel.writeString(id);
        parcel.writeString(subreddit_id);
        parcel.writeString(link_id);
        parcel.writeString(parent_id);
        parcel.writeString(name);
        parcel.writeString(context);

        parcel.writeInt(ups);
        parcel.writeInt(downs);

        if (edited instanceof Long) {
            parcel.writeLong((Long) edited);
        } else {
            parcel.writeLong(-1);
        }

        parcel.writeLong(created);
        parcel.writeLong(created_utc);

        parcel.writeInt(saved ? 1 : 0);
        parcel.writeInt(gilded);

        parcel.writeString(distinguished);
    }

    @Override
    public String getIdAlone() {
        return id;
    }

    @Override
    public String getIdAndType() {
        return name;
    }

    public boolean isArchived() {
        return Boolean.TRUE.equals(archived);
    }

    @Nullable
    public PostCommentListingURL getContextUrl() {

        if (context != null) {

            String rawContextUrl = context;

            if (rawContextUrl.startsWith("r/")) {
                rawContextUrl = "/" + rawContextUrl;
            }

            if (rawContextUrl.startsWith("/")) {
                rawContextUrl = "https://reddit.com" + rawContextUrl;
            }

            return PostCommentListingURL.parse(Uri.parse(rawContextUrl));

        } else {
            return new PostCommentListingURL(null, link_id, getIdAlone(), 3, null, null);
        }
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<RedditComment> CREATOR = new Parcelable.Creator<RedditComment>() {
        public RedditComment createFromParcel(final Parcel in) {
            return new RedditComment(in);
        }

        public RedditComment[] newArray(final int size) {
            return new RedditComment[size];
        }
    };

    public HashSet<String> computeAllLinks() {
        return LinkHandler.computeAllLinks(StringEscapeUtils.unescapeHtml4(body_html));
    }
}