DateVersionEntityId.java

/*
 * Copyright 2022 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.model;

import java.time.Instant;
import java.util.Objects;

import javax.validation.constraints.NotNull;

/**
 * The Interface EntityVersionedId.
 */
public interface DateVersionEntityId {

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	Long getId();

	/**
	 * Gets the version.
	 *
	 * @return the version
	 */
	Instant getModifiedDate();

	/**
	 * API utility class for receiving only entity id and version
	 */
	public static class EntityIdAndModifiedDate implements DateVersionEntityId {
		@NotNull
		public Long id;
		@NotNull
		public Instant modifiedDate;

		public EntityIdAndModifiedDate() {
		}

		public EntityIdAndModifiedDate(final long id, final Instant modifiedDate) {
			this.id = id;
			this.modifiedDate = modifiedDate;
		}

		@Override
		public Long getId() {
			return id;
		}

		@Override
		public Instant getModifiedDate() {
			return modifiedDate;
		}

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

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			EntityIdAndModifiedDate other = (EntityIdAndModifiedDate) obj;
			return Objects.equals(id, other.id) && Objects.equals(modifiedDate, other.modifiedDate);
		}

		public static EntityIdAndModifiedDate of(DateVersionEntityId obj) {
			return new EntityIdAndModifiedDate(obj.getId(), obj.getModifiedDate());
		}

		@Override
		public String toString() {
			return "id=" + this.id + " modifiedDate=" + this.modifiedDate;
		}
	}
}