com.richtodd.android.quiltdesign.block.QuiltBlock.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.quiltdesign.block.QuiltBlock.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.quiltdesign.block;

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

import android.os.Parcel;
import android.os.Parcelable;

public class QuiltBlock implements Parcelable {

    //
    // Fields
    //

    private PaperPiecedBlock m_block;

    //
    // Constructors
    //

    public QuiltBlock() {
        m_block = null;
    }

    public QuiltBlock(Parcel in) {
        m_block = in.readParcelable(PaperPiecedBlock.class.getClassLoader());
    }

    //
    // Package-Private
    //

    PaperPiecedBlock getBlock() {
        return m_block;
    }

    void setBlock(PaperPiecedBlock block) {
        m_block = block;
    }

    //
    // JSON
    //

    JSONObject createJSONObject() throws JSONException {

        JSONObject jsonQuiltBlock = new JSONObject();

        jsonQuiltBlock.put("block", m_block.createJSONObject());

        return jsonQuiltBlock;
    }

    static final QuiltBlock createFromJSONObject(JSONObject jsonObject) throws JSONException {

        QuiltBlock quiltBlock = new QuiltBlock();

        quiltBlock.m_block = PaperPiecedBlock.createFromJSONObject(jsonObject.getJSONObject("block"));

        return quiltBlock;
    }

    //
    // Parcelable
    //

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(m_block, 0);
    }

    public static final Parcelable.Creator<QuiltBlock> CREATOR = new Creator<QuiltBlock>() {

        @Override
        public QuiltBlock[] newArray(int size) {
            return new QuiltBlock[size];
        }

        @Override
        public QuiltBlock createFromParcel(Parcel in) {
            return new QuiltBlock(in);
        }
    };
}