com.conwet.silbops.msg.PublishMsg.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.msg.PublishMsg.java

Source

package com.conwet.silbops.msg;

/*
 * #%L
 * SilboPS API Broker
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.util.Objects;

import com.conwet.silbops.model.Context;
import com.conwet.silbops.model.MessageType;
import com.conwet.silbops.model.Notification;
import org.json.simple.JSONObject;

/**
 *
 * @author sortega
 */
public class PublishMsg extends Message {

    private Notification notification;
    private Context context;

    public PublishMsg() {

        this(new Notification(), new Context());
    }

    public PublishMsg(JSONObject payload) {

        this(Notification.fromJSON((JSONObject) payload.get("notification")),
                Context.fromJSON((JSONObject) payload.get("context")));
    }

    public PublishMsg(Notification notification, Context context) {

        super(MessageType.PUBLISH);
        this.notification = Objects.requireNonNull(notification, "Notification is null");
        this.context = Objects.requireNonNull(context, "Context is null");
    }

    @Override
    @SuppressWarnings("unchecked")
    public JSONObject getPayloadAsJSON() {

        JSONObject json = new JSONObject();
        json.put("notification", notification.toJSON());
        json.put("context", context.toJSON());

        return json;
    }

    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (obj instanceof PublishMsg) {

            PublishMsg other = (PublishMsg) obj;
            return notification.equals(other.notification) && context.equals(other.context);
        }

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + notification.hashCode();
        hash = 83 * hash + context.hashCode();
        return hash;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {

        this.context = Objects.requireNonNull(context);
    }

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {

        this.notification = Objects.requireNonNull(notification);
    }
}