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

org.conqat.engine.index.shared.UnresolvedCommitDescriptor Maven / Gradle / Ivy

package org.conqat.engine.index.shared;

import java.util.Objects;

import com.google.common.base.Preconditions;

/**
 * An already parsed commit, whose branch name may still be missing, whose
 * timestamp can be Long.MAX_VALUE (corresponding to the "HEAD" commit
 * descriptor given in service calls), and whose {@link #parentIndex} is not yet
 * resolved.
 * 
 * This intermediate version of {@link CommitDescriptor} can be built from a
 * string without access to a project storage system. It can be resolved to a
 * {@link CommitDescriptor} with UnresolvedCommitDescriptorUtils.
 */
public final class UnresolvedCommitDescriptor {

	/** The name of the branch. */
	private final String branchName;

	/**
	 * The timestamp on the branch. This can be Long.MAX_VALUE, e.g., if the commit
	 * descriptor "branch:HEAD" was given in a service call.
	 */
	private final long timestamp;

	/**
	 * Describes the commit that is "parentIndex" before the commit referenced by
	 * the given timestamp. If no parentIndex is given this field is zero.
	 *
	 * A parent index can be used to refer to a parent of the current commit. For
	 * example, the commit descriptor "master:1234p1" refers to the immediate parent
	 * of commit "master:1234".
	 */
	private final int parentIndex;

	public UnresolvedCommitDescriptor(String branchName, long timestamp) {
		this(branchName, timestamp, 0);
	}

	public UnresolvedCommitDescriptor(String branchName, long timestamp, int parentIndex) {
		Preconditions.checkArgument(parentIndex >= 0, "Parent index %d must be positive!", parentIndex);
		this.branchName = branchName;
		this.timestamp = timestamp;
		this.parentIndex = parentIndex;
	}

	/** @see #branchName */
	public String getBranchName() {
		return branchName;
	}

	/** @see #timestamp */
	public long getTimestamp() {
		return timestamp;
	}

	/** @see #parentIndex */
	public int getParentIndex() {
		return parentIndex;
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		if (branchName != null) {
			result.append(branchName);
			result.append(":");
		}

		if (timestamp == Long.MAX_VALUE) {
			result.append(CommitDescriptor.HEAD_TIMESTAMP);
		} else {
			result.append(timestamp);
		}

		if (parentIndex > 0) {
			result.append("p").append(parentIndex);
		}
		return result.toString();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		UnresolvedCommitDescriptor that = (UnresolvedCommitDescriptor) o;
		return timestamp == that.timestamp && parentIndex == that.parentIndex
				&& Objects.equals(branchName, that.branchName);
	}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy