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

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.msg.UnadvertiseMsg.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.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

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 UnadvertiseMsg extends Message implements Externalizable {

    private static final long serialVersionUID = 1L;

    private Advertise advertise;
    private Advertise context;
    private Set<Advertise> uncovered; // contains the uncovered advertises.

    public UnadvertiseMsg() {

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

    public UnadvertiseMsg(JSONObject payload) {

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

        if (payload.containsKey("uncovered")) {

            // array of arrays
            for (Object fil : (JSONArray) payload.get("uncovered")) {

                this.uncovered.add(Advertise.fromJSON((JSONArray) fil));
            }
        }
    }

    public UnadvertiseMsg(Advertise advertise, Advertise context) {

        super(MessageType.UNADVERTISE);
        this.advertise = Objects.requireNonNull(advertise, "Advertise is null");
        this.context = Objects.requireNonNull(context, "Context is null");
        uncovered = new HashSet<>();
    }

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

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

        JSONArray uncov = new JSONArray();
        for (Advertise fil : uncovered) {

            uncov.add(fil.toJSON());
        }
        json.put("uncovered", uncov);

        return json;
    }

    @Override
    public boolean equals(Object obj) {

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

        if (obj instanceof UnadvertiseMsg) {

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

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + advertise.hashCode();
        hash = 89 * hash + context.hashCode();
        hash = 89 * hash + uncovered.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);
    }

    public Set<Advertise> getUncovered() {
        return uncovered;
    }

    public void setUncovered(Set<Advertise> uncovered) {

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

    @Override
    public void writeExternal(ObjectOutput output) throws IOException {

        advertise.writeExternal(output);
        context.writeExternal(output);

        output.writeByte(uncovered.size());

        for (Advertise uncov : this.uncovered) {

            uncov.writeExternal(output);
        }
    }

    @Override
    public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {

        advertise = new Advertise();
        advertise.readExternal(input);
        context = new Advertise();
        context.readExternal(input);

        uncovered = new HashSet<>();
        int num = input.readByte();

        for (int i = 0; i < num; i++) {

            Advertise uncov = new Advertise();
            uncov.readExternal(input);
            this.uncovered.add(uncov);
        }
    }
}