All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.common.DateVersionedEntityImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 serso aka se.solovyev
 *
 * 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.
 *
 * ---------------------------------------------------------------------
 * Contact details
 *
 * Email: [email protected]
 * Site:  http://se.solovyev.org
 */

package org.solovyev.common;

import org.joda.time.DateTime;

import javax.annotation.Nonnull;

/**
 * User: serso
 * Date: 4/29/12
 * Time: 9:53 PM
 */
final class DateVersionedEntityImpl implements DateVersionedEntity {

	@Nonnull
	private VersionedEntity versionedEntity;

	@Nonnull
	private DateTime creationDate;

	@Nonnull
	private DateTime modificationDate;

	@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = {"NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"})
	private DateVersionedEntityImpl() {
	}

	@Nonnull
	static  DateVersionedEntity newEntity(@Nonnull I id) {
		final DateVersionedEntityImpl result = new DateVersionedEntityImpl();

		result.versionedEntity = Entities.newEntity(id);
		result.creationDate = DateTime.now();
		result.modificationDate = result.creationDate;

		return result;
	}

	@Nonnull
	static  DateVersionedEntity newVersion(@Nonnull DateVersionedEntity dateVersionedEntity) {
		final DateVersionedEntityImpl result = new DateVersionedEntityImpl();

		// increase version
		result.versionedEntity = Entities.newEntityVersion(dateVersionedEntity);
		result.creationDate = dateVersionedEntity.getCreationDate();
		result.modificationDate = DateTime.now();

		return result;
	}

	@Nonnull
	static  DateVersionedEntity newInstance(@Nonnull I id, @Nonnull Integer version, @Nonnull DateTime creationDate, @Nonnull DateTime modificationDate) {
		return newInstance(Entities.newEntity(id, version), creationDate, modificationDate);
	}

		@Nonnull
	static  DateVersionedEntity newInstance(@Nonnull VersionedEntity versionedEntity, @Nonnull DateTime creationDate, @Nonnull DateTime modificationDate) {
		final DateVersionedEntityImpl result = new DateVersionedEntityImpl();

		result.versionedEntity = versionedEntity;
		result.creationDate = creationDate;
		result.modificationDate = modificationDate;

		return result;
	}

	@Nonnull
	@Override
	public DateTime getCreationDate() {
		return this.creationDate;
	}

	@Nonnull
	@Override
	public DateTime getModificationDate() {
		return this.modificationDate;
	}

	@Override
	@Nonnull
	public I getId() {
		return versionedEntity.getId();
	}

	@Override
	@Nonnull
	public Integer getVersion() {
		return versionedEntity.getVersion();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}

		if (!(o instanceof DateVersionedEntityImpl)) {
			return false;
		}

		DateVersionedEntityImpl that = (DateVersionedEntityImpl) o;

		if (!versionedEntity.equals(that.versionedEntity)) {
			return false;
		}

		return true;
	}

	@Override
	public boolean equalsVersion(Object that) {
		return this.equals(that) && this.versionedEntity.equalsVersion(((DateVersionedEntityImpl) that).versionedEntity);
	}

	@Override
	public int hashCode() {
		return versionedEntity.hashCode();
	}

	@Nonnull
	@Override
	public DateVersionedEntityImpl clone() {
		final DateVersionedEntityImpl clone;

		try {
			//noinspection unchecked
			clone = (DateVersionedEntityImpl) super.clone();

			clone.versionedEntity = this.versionedEntity.clone();

			// dates are immutable => can leave links as is

		} catch (CloneNotSupportedException e) {
			throw new AssertionError(e);
		}

		return clone;
	}
}