Java tutorial
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 com.conwet.silbops.model.Subscription; import com.conwet.silbops.model.MessageType; 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; /** * * @author sortega */ public class UnsubscribeMsg extends Message implements Externalizable { private static final long serialVersionUID = 1L; private Subscription subscription; private Set<Subscription> uncovered; public UnsubscribeMsg() { this(new Subscription()); } public UnsubscribeMsg(JSONObject payload) { this(Subscription.fromJSON((JSONObject) payload.get("subscription"))); if (payload.containsKey("uncovered")) { for (Object fil : (JSONArray) payload.get("uncovered")) { uncovered.add(Subscription.fromJSON((JSONObject) fil)); } } } public UnsubscribeMsg(Subscription subscription) { super(MessageType.UNSUBSCRIBE); this.subscription = Objects.requireNonNull(subscription, "Subscription is null"); this.uncovered = new HashSet<>(); } public Subscription getSubscription() { return subscription; } public void setSubscription(Subscription subscription) { this.subscription = Objects.requireNonNull(subscription); } public Set<Subscription> getUncovered() { return uncovered; } public void setUncovered(Set<Subscription> uncovered) { this.uncovered = Objects.requireNonNull(uncovered); } @Override @SuppressWarnings("unchecked") public JSONObject getPayloadAsJSON() { JSONObject json = new JSONObject(); json.put("subscription", subscription.toJSON()); JSONArray uncov = new JSONArray(); for (Subscription 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 UnsubscribeMsg) { UnsubscribeMsg other = (UnsubscribeMsg) obj; return subscription.equals(other.subscription) && uncovered.equals(other.uncovered); } return false; } @Override public int hashCode() { int hash = 5; hash = 61 * hash + subscription.hashCode(); hash = 61 * hash + uncovered.hashCode(); return hash; } @Override public void writeExternal(ObjectOutput output) throws IOException { subscription.writeExternal(output); output.writeByte(uncovered.size()); for (Subscription uncov : uncovered) { uncov.writeExternal(output); } } @Override public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException { subscription = new Subscription(); subscription.readExternal(input); uncovered = new HashSet<>(); int num = input.readByte(); for (int i = 0; i < num; i++) { Subscription uncov = new Subscription(); uncov.readExternal(input); uncovered.add(uncov); } } }