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.time.Instant;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
*
* @author Artem Hrybeniuk
*/
@Getter
@Setter
@ToString
public class FirehoseEvent {
private EventType eventType;
private Instant timestamp;
private Class<?> clazz;
private Long id;
private Object entity;
/** SID that triggered the event */
private Long sid;
public static enum EventType {
CREATE, UPDATE, DELETE,
}
public FirehoseEvent(Class<?> clazz, Long id, Instant timestamp, EventType eventType) {
this.clazz = clazz;
this.id = id;
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.timestamp = timestamp;
this.eventType = eventType;
this.entity = entity;
}
@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);
}
}