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

Java tutorial

Introduction

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

import com.conwet.silbops.model.Advertise;
import com.conwet.silbops.model.MessageType;

/**
 *
 * @author sergio
 */
public class AdvertiseMsg extends Message {

    private Advertise advertise;
    private Advertise context;

    public AdvertiseMsg() {

        this(new Advertise(), new Advertise());
    }

    public AdvertiseMsg(JSONObject payload) {

        this(Advertise.fromJSON((JSONArray) payload.get("advertise")),
                Advertise.fromJSON((JSONArray) payload.get("context")));
    }

    public AdvertiseMsg(Advertise advertise, Advertise context) {

        super(MessageType.ADVERTISE);
        this.advertise = Objects.requireNonNull(advertise, "Advertise is null");
        this.context = Objects.requireNonNull(context, "Context is null");
    }

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

        JSONObject json = new JSONObject();
        json.put("advertise", advertise.toJSON());
        json.put("context", context.toJSON());
        return json;
    }

    @Override
    public boolean equals(Object obj) {

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

        if (obj instanceof AdvertiseMsg) {

            AdvertiseMsg other = (AdvertiseMsg) obj;
            return advertise.equals(other.advertise) && context.equals(other.context);
        }

        return false;
    }

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

    public Advertise getAdvertise() {
        return advertise;
    }

    public void setAdvertise(Advertise advertise) {

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

    public Advertise getContext() {
        return context;
    }

    public void setContext(Advertise context) {

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