com.google.example.eightbitartist.messages.Message.java Source code

Java tutorial

Introduction

Here is the source code for com.google.example.eightbitartist.messages.Message.java

Source

/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.example.eightbitartist.messages;

import android.util.Log;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Base class for all messages sent between 8BitArtist devices.
 *      String type - a tag indicating the class of the message, used by the recipient as an
 *              instruction for how to deserialize.
 */
public class Message {

    private static final String TAG = Message.class.getSimpleName();
    private String type;

    /** Default constructor required for Jackson **/
    public Message() {
    }

    public Message(String type) {
        this.type = type;
    }

    public String persist(ObjectMapper objectMapper) {
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            Log.e(TAG, "Could not persist message of type " + type, e);
            return "";
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}