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

Java tutorial

Introduction

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

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

    private Subscription subscription;

    public SubscribeMsg() {

        this(new Subscription());
    }

    public SubscribeMsg(JSONObject payload) {

        this(Subscription.fromJSON((JSONObject) payload.get("subscription")));
    }

    public SubscribeMsg(Subscription subscription) {

        super(MessageType.SUBSCRIBE);
        this.subscription = Objects.requireNonNull(subscription, "Subscription is null");
    }

    public Subscription getSubscription() {
        return subscription;
    }

    public void setSubscription(Subscription subscription) {

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

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

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

        return json;
    }

    @Override
    public boolean equals(Object obj) {

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

        if (obj instanceof SubscribeMsg) {

            SubscribeMsg other = (SubscribeMsg) obj;
            return subscription.equals(other.subscription);
        }

        return false;
    }

    @Override
    public int hashCode() {

        return 71 * subscription.hashCode();
    }
}