org.akop.crosswords.model.PuzzleSource.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.model.PuzzleSource.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package org.akop.crosswords.model;

import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

import org.akop.crosswords.Crosswords;
import org.joda.time.DateTime;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PuzzleSource extends Model implements Parcelable {
    public static final Parcelable.Creator<PuzzleSource> CREATOR = new Parcelable.Creator<PuzzleSource>() {
        public PuzzleSource createFromParcel(Parcel in) {
            return new PuzzleSource(in);
        }

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

    public enum Compression {
        Zip,
    }

    private static final Pattern URL_TEMPLATE_FINDER = Pattern.compile("<([^>]+)>");

    private static final String PH_TWO_DIGIT_YEAR = "yy";
    private static final String PH_TWO_DIGIT_MONTH = "mm";
    private static final String PH_TWO_DIGIT_DAY = "dd";

    @SerializedName("id")
    private String mId;
    @SerializedName("name")
    private String mName;
    @SerializedName("description")
    private String mDescriptionResId;
    @SerializedName("template")
    private String mUrlTemplate;
    @SerializedName("parser")
    private String mParser;
    @SerializedName("abbreviation")
    private String mAbbreviation;
    @SerializedName("compression")
    private Compression mCompression;
    @SerializedName("colorIndex")
    private int mColorIndex;
    @SerializedName("backlogDays")
    private int mBacklogDays;
    private String mDescription;

    public PuzzleSource(PuzzleSource original) {
        if (original != null) {
            mId = original.mId;
            mName = original.mName;
            mDescriptionResId = original.mDescriptionResId;
            mUrlTemplate = original.mUrlTemplate;
            mParser = original.mParser;
            mAbbreviation = original.mAbbreviation;
            mCompression = original.mCompression;
            mColorIndex = original.mColorIndex;
            mDescription = original.mDescription;
        }
    }

    private PuzzleSource(Parcel in) {
        mId = in.readString();
        mName = in.readString();
        mUrlTemplate = in.readString();
        mParser = in.readString();
        mCompression = readEnum(in, Compression.class);
        mAbbreviation = in.readString();
        mColorIndex = in.readInt();
        mBacklogDays = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mId);
        dest.writeString(mName);
        dest.writeString(mUrlTemplate);
        dest.writeString(mParser);
        writeEnum(dest, mCompression);
        dest.writeString(mAbbreviation);
        dest.writeInt(mColorIndex);
        dest.writeInt(mBacklogDays);
    }

    public String getId() {
        return mId;
    }

    public String getName() {
        return mName;
    }

    public String getDescription() {
        if (mDescription == null) {
            Crosswords app = Crosswords.getInstance();
            Resources res = app.getResources();
            int resId = res.getIdentifier(mDescriptionResId, "string", app.getPackageName());

            mDescription = res.getString(resId);
        }

        return mDescription;
    }

    public int getColorIndex() {
        return mColorIndex;
    }

    public void setColorIndex(int index) {
        mColorIndex = index;
    }

    public String getAbbreviation() {
        return mAbbreviation;
    }

    public String getParser() {
        return mParser;
    }

    public Compression getCompressionMode() {
        return mCompression;
    }

    public int getBacklogInDays() {
        return mBacklogDays;
    }

    public String createDateUrl(DateTime date) {
        StringBuilder sb = new StringBuilder();
        int start = 0;

        Matcher matcher = URL_TEMPLATE_FINDER.matcher(mUrlTemplate);
        while (matcher.find()) {
            sb.append(mUrlTemplate, start, matcher.start());

            start = matcher.end();
            String match = matcher.group(1);

            if (PH_TWO_DIGIT_YEAR.equals(match)) {
                String year = String.format("%02d", date.getYear());
                sb.append(year.substring(year.length() - 2));
            } else if (PH_TWO_DIGIT_MONTH.equals(match)) {
                sb.append(String.format("%02d", date.getMonthOfYear()));
            } else if (PH_TWO_DIGIT_DAY.equals(match)) {
                sb.append(String.format("%02d", date.getDayOfMonth()));
            }
        }

        sb.append(mUrlTemplate, start, mUrlTemplate.length());

        return sb.toString();
    }

    public static boolean equals(PuzzleSource one, PuzzleSource two) {
        if (one == null || two == null) {
            return one == two;
        }

        return one.mId == two.mId;
    }

    @Override
    public boolean equals(Object o) {
        return (o instanceof PuzzleSource) && PuzzleSource.equals(this, (PuzzleSource) o);
    }
}