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.genesys.server.component.firehose;
import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.time.Instant;
import java.util.Objects;
/**
*
* @author Artem Hrybeniuk
*/
public class FirehoseEvent implements Comparable<FirehoseEvent>, Serializable {
private static final long serialVersionUID = 5481828485125320364L;
private Class<?> clazz; // TODO Maybe just className?
private Long id;
private String key;
private Instant timestamp;
transient private Object entity;
private EventType eventType;
public static enum EventType {
CREATE, UPDATE, DELETE
}
public FirehoseEvent(Class<?> clazz, Long id, Instant timestamp, EventType eventType) {
this.clazz = clazz;
this.id = id;
this.key = (this.id == null ? "null" : this.id) + ":" + (this.clazz == null ? "null" : this.clazz.getName());
this.timestamp = timestamp;
this.eventType = eventType;
}
public FirehoseEvent(Class<?> clazz, Long id, Instant timestamp, EventType eventType, Object entity) {
this.clazz = clazz;
this.id = id;
this.key = (this.id == null ? "null" : this.id) + ":" + (this.clazz == null ? "null" : this.clazz.getName());
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;
this.key = (this.id == null ? "null" : this.id) + ":" + (this.clazz == null ? "null" : this.clazz.getName());
}
public Instant getTimestamp() {
return timestamp;
}
public void updateTimestamp(Instant 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
*/
@Override
public int compareTo(@NotNull FirehoseEvent firehoseEvent) {
if (this.timestamp.equals(firehoseEvent.timestamp)) {
if (firehoseEvent.id == null && this.id == null) {
return 0;
} else if (firehoseEvent.id == null) {
return -1;
} else if (this.id == null) {
return 1;
} else {
return (int) (this.id - firehoseEvent.id);
}
} else {
return this.timestamp.compareTo(firehoseEvent.timestamp);
}
}
/**
* Match by id and clazz
*
* @param b
* @return
*/
public boolean sameReference(FirehoseEvent b) {
if (b == null) {
return false;
}
return Objects.equals(key, b.key);
}
/**
* Object key
*
* @return id+className
*/
public String getKey() {
return this.key;
}
}