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

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.msg.NotifyMsg.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.MessageType;
import com.conwet.silbops.model.Notification;
import org.json.simple.JSONObject;

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

    private Notification notification;

    public NotifyMsg() {

        this(new Notification());
    }

    public NotifyMsg(JSONObject payload) {

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

    public NotifyMsg(Notification notification) {

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

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {

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

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

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

        return json;
    }

    @Override
    public boolean equals(Object obj) {

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

        if (obj instanceof NotifyMsg) {

            NotifyMsg other = (NotifyMsg) obj;
            return notification.equals(other.notification);
        }

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + notification.hashCode();
        return hash;
    }
}