Record.java

package org.microspace.replicator.record;

import java.io.Serializable;

import org.microspace.thread.ContextId;

/**
 * This Record is used when data is replicated.
 * 
 * @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
 * @version 2016-06-26
 */
public class Record implements Serializable {
	
	public enum Type {
		/** contains field information about a new class to be written for the first time */
		HEADER, 
		/** indicates that a new transaction started. */
		BEGIN,
		/** new data was added */
		ADD, 
		/** old data was removed */
		REMOVE, 
		/** indicates transaction end. Objects wont be updated until this is received. */
		END,
		/** While DataModel in AddRecords */
		DATAMODEL,
	}

	private static final long serialVersionUID = 20120624100800001L;

	final Type type;
	final RecordData data;
	final ContextId contextId;
	
	public Record (ContextId contextId, Type type) {
		this.type = type;
		this.data = null;
		this.contextId = contextId;
	}

	public Record(ContextId contextId, AddRecord data) {
		this.type = Type.ADD;
		this.data = data;
		this.contextId = contextId;
	}
	
	public Record(ContextId contextId, RemoveRecord data) {
		this.type = Type.REMOVE;
		this.data = data;
		this.contextId = contextId;
	}

	public Record(HeaderRecord data) {
		this.type = Type.HEADER;
		this.data = data;
		this.contextId = null;
	}
	public Record(ContextId contextId, DataModelRecord data) {
		this.type = Type.DATAMODEL;
		this.data = data;
		this.contextId = contextId;
	}
	
	
	public Type getType () {
		return type;
	}

	public ContextId getContextId() {
		return contextId;
	}

	public HeaderRecord getHeaderRecord () {
		return type == Type.HEADER ? (HeaderRecord) data : null;
	}
	
	public AddRecord getAddRecord () {
		return type == Type.ADD ? (AddRecord) data : null;
	}
	
	public RemoveRecord getRemoveRecord () {
		return type == Type.REMOVE ? (RemoveRecord) data : null;
	}
	
	public DataModelRecord getDataModelRecord () {
		return type == Type.DATAMODEL ? (DataModelRecord) data : null;
	}
}