001package org.conqat.engine.index.shared;
002
003import java.util.Objects;
004
005import com.google.common.base.Preconditions;
006
007/**
008 * An already parsed commit, whose branch name may still be missing and whose
009 * {@link #parentIndex} is not yet resolved.
010 * 
011 * This intermediate version of {@link CommitDescriptor} can be built from a
012 * string without access to a project storage system. It can be resolved to a
013 * {@link CommitDescriptor} with UnresolvedCommitDescriptorUtils.
014 */
015public final class UnresolvedCommitDescriptor {
016
017        /** The name of the branch. */
018        private final String branchName;
019
020        /** The timestamp on the branch. */
021        private final long timestamp;
022
023        /**
024         * Describes the commit that is "parentIndex" before the commit referenced by
025         * the given timestamp. If no parentIndex is given this field is zero.
026         */
027        private final int parentIndex;
028
029        public UnresolvedCommitDescriptor(String branchName, long timestamp) {
030                this(branchName, timestamp, 0);
031        }
032
033        public UnresolvedCommitDescriptor(String branchName, long timestamp, int parentIndex) {
034                Preconditions.checkArgument(parentIndex >= 0, "Parent index %d must be positive!", parentIndex);
035                this.branchName = branchName;
036                this.timestamp = timestamp;
037                this.parentIndex = parentIndex;
038        }
039
040        /** @see #branchName */
041        public String getBranchName() {
042                return branchName;
043        }
044
045        /** @see #timestamp */
046        public long getTimestamp() {
047                return timestamp;
048        }
049
050        /** @see #parentIndex */
051        public int getParentIndex() {
052                return parentIndex;
053        }
054
055        @Override
056        public String toString() {
057                StringBuilder result = new StringBuilder();
058                if (branchName != null) {
059                        result.append(branchName);
060                        result.append(":");
061                }
062
063                if (timestamp == Long.MAX_VALUE) {
064                        result.append(CommitDescriptor.HEAD_TIMESTAMP);
065                } else {
066                        result.append(timestamp);
067                }
068
069                if (parentIndex > 0) {
070                        result.append("p").append(parentIndex);
071                }
072                return result.toString();
073        }
074
075        @Override
076        public boolean equals(Object o) {
077                if (this == o) {
078                        return true;
079                }
080                if (o == null || getClass() != o.getClass()) {
081                        return false;
082                }
083                UnresolvedCommitDescriptor that = (UnresolvedCommitDescriptor) o;
084                return timestamp == that.timestamp && parentIndex == that.parentIndex
085                                && Objects.equals(branchName, that.branchName);
086        }
087
088        @Override
089        public int hashCode() {
090                return Objects.hash(branchName, timestamp, parentIndex);
091        }
092}