FirehoseEvent.java

/*
 * Copyright 2021 Global Crop Diversity Trust
 *
 * 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 org.gringlobal.component.firehose;

import java.util.Date;
import java.util.Objects;

/**
 *
 * @author Artem Hrybeniuk
 */
public class FirehoseEvent {

	private Class<?> clazz;
	private Long id;

	private Date timestamp;

	private Object entity;

	private EventType eventType;

	public static enum EventType {
		CREATE, UPDATE, DELETE
	}

	public FirehoseEvent(Class<?> clazz, Long id, Date timestamp, EventType eventType) {
		this.clazz = clazz;
		this.id = id;
		this.timestamp = timestamp;
		this.eventType = eventType;
	}

	public FirehoseEvent(Class<?> clazz, Long id, Date timestamp, EventType eventType, Object entity) {
		this.clazz = clazz;
		this.id = id;
		this.timestamp = timestamp;
		this.eventType = eventType;
		this.entity = entity;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void updateTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

	public Object getEntity() {
		return entity;
	}

	public void setEntity(Object entity) {
		this.entity = entity;
	}

	public EventType getEventType() {
		return eventType;
	}

	@Override
	public String toString() {
		return "Firehose." + this.eventType + " for type " + this.clazz + " id=" + this.id + " @" + this.timestamp;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof FirehoseEvent)) return false;
		FirehoseEvent that = (FirehoseEvent) o;
		return Objects.equals(timestamp, that.timestamp) && Objects.equals(id, that.id) && Objects.equals(clazz, that.clazz);
	}

	@Override
	public int hashCode() {
		return Objects.hash(timestamp, clazz, id);
	}

	/**
	 * Compare by timestamp, then id, then clazz
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static int compareTo(FirehoseEvent a, FirehoseEvent b) {
		if (a.timestamp.equals(b.timestamp)) {
			if (b.id == null && a.id == null) {
				return 0;
			} else if (b.id == null) {
				return -1;
			} else if (a.id == null) {
				return 1;
			} else {
				return (int) (a.id - b.id);
			}
		} else {
			return a.timestamp.compareTo(b.timestamp);
		}
	}

	/**
	 * Match by id and clazz
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public boolean sameReference(FirehoseEvent b) {
		if (b == null) {
			return false;
		}
		return Objects.equals(id, b.id) && Objects.equals(clazz, b.clazz);
	}
}