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 java.util.Objects; import org.json.simple.JSONObject; import com.conwet.silbops.model.Context; import com.conwet.silbops.model.MessageType; /** * * @author sergio */ public class ContextMsg extends Message { private Context context; private String id; public ContextMsg() { this("", new Context()); } public ContextMsg(JSONObject payload) { this((String) payload.get("id"), Context.fromJSON((JSONObject) payload.get("context"))); } public ContextMsg(String id, Context context) { super(MessageType.CONTEXT); // TODO FIX how contextID is generated // this.id = Objects.requireNonNull(id, "ID is null"); this.id = id == null ? "" : id; this.context = Objects.requireNonNull(context, "Context is null"); } @Override @SuppressWarnings("unchecked") public JSONObject getPayloadAsJSON() { JSONObject json = new JSONObject(); json.put("context", context.toJSON()); json.put("id", id); return json; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof ContextMsg) { ContextMsg other = (ContextMsg) obj; return id.equals(other.id) && context.equals(other.context); } return false; } @Override public int hashCode() { int hash = 7; hash = 23 * hash + context.hashCode(); hash = 23 * hash + id.hashCode(); return hash; } public Context getContext() { return context; } public void setContext(Context context) { this.context = Objects.requireNonNull(context); } public String getId() { return id; } public void setId(String id) { this.id = Objects.requireNonNull(id); } }