com.conwet.xjsp.features.ImmutableMessage.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.xjsp.features.ImmutableMessage.java

Source

package com.conwet.xjsp.features;

/*
 * #%L
 * eXtensible JSON Streaming Protocol
 * %%
 * 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 org.json.simple.JSONObject;

/**
 * A read-only message.
 * @author sortega
 */
public class ImmutableMessage implements Message {

    private final String id;
    private final String messageType;
    private final String feature;
    private final Object payload;

    public ImmutableMessage(String feature, String messageType, Object payload, String id) {
        this.id = id;
        this.messageType = messageType;
        this.payload = payload;
        this.feature = feature;
    }

    @Override
    public String getFeature() {
        return feature;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getMessageType() {
        return messageType;
    }

    @Override
    public Object getPayload() {
        return payload;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ImmutableMessage other = (ImmutableMessage) obj;
        if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
            return false;
        }
        if ((this.messageType == null) ? (other.messageType != null)
                : !this.messageType.equals(other.messageType)) {
            return false;
        }
        if ((this.feature == null) ? (other.feature != null) : !this.feature.equals(other.feature)) {
            return false;
        }
        if (this.payload != other.payload && (this.payload == null || !this.payload.equals(other.payload))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 41 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 41 * hash + (this.messageType != null ? this.messageType.hashCode() : 0);
        hash = 41 * hash + (this.feature != null ? this.feature.hashCode() : 0);
        hash = 41 * hash + (this.payload != null ? this.payload.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "InmutableMessage{" + "id=" + id + ", messageType=" + messageType + ", feature=" + feature
                + ", payload=" + payload + '}';
    }

    @Override
    @SuppressWarnings("unchecked")
    public JSONObject toJSONObject(FeatureMap fm) {
        JSONObject object = new JSONObject();
        object.put("id", id);
        object.put("type", fm.getPrefix(this.getFeature()) + ":" + messageType);
        object.put("message", payload);
        return object;
    }
}