tikhomirov@347: /*
tikhomirov@347: * Copyright (c) 2011 TMate Software Ltd
tikhomirov@347: *
tikhomirov@347: * This program is free software; you can redistribute it and/or modify
tikhomirov@347: * it under the terms of the GNU General Public License as published by
tikhomirov@347: * the Free Software Foundation; version 2 of the License.
tikhomirov@347: *
tikhomirov@347: * This program is distributed in the hope that it will be useful,
tikhomirov@347: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@347: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@347: * GNU General Public License for more details.
tikhomirov@347: *
tikhomirov@347: * For information on how to redistribute this software under
tikhomirov@347: * the terms of a license other than GNU General Public License
tikhomirov@347: * contact TMate Software at support@hg4j.com
tikhomirov@347: */
tikhomirov@347: package org.tmatesoft.hg.core;
tikhomirov@347:
tikhomirov@347: import org.tmatesoft.hg.internal.Experimental;
tikhomirov@347: import org.tmatesoft.hg.repo.HgRepository;
tikhomirov@347:
tikhomirov@347: /**
tikhomirov@347: * Use of revision or local revision index that is not valid for a given revlog.
tikhomirov@347: *
tikhomirov@347: * @author Artem Tikhomirov
tikhomirov@347: * @author TMate Software Ltd.
tikhomirov@347: */
tikhomirov@347: @SuppressWarnings("serial")
tikhomirov@347: @Experimental(reason="1) Whether to use checked or runtime exception is not yet decided. 2) Perhaps, its use not bound to wrong arguments")
tikhomirov@347: public class HgInvalidRevisionException extends IllegalArgumentException {
tikhomirov@347: private Nodeid rev;
tikhomirov@347: private Integer revIdx;
tikhomirov@347: // next two make sense only when revIdx is present
tikhomirov@347: private int rangeLeftBoundary = -1, rangeRightBoundary = -1;
tikhomirov@347:
tikhomirov@347: /**
tikhomirov@347: *
tikhomirov@347: * this exception is not expected to be initialized with another exception, although those who need to,
tikhomirov@347: * may still use {@link #initCause(Throwable)}
tikhomirov@347: * @param message optional description of the issue
tikhomirov@347: * @param revision invalid revision, may be null
if revisionIndex is used
tikhomirov@347: * @param revisionIndex invalid revision index, may be null
if not known and revision is supplied
tikhomirov@347: */
tikhomirov@347: public HgInvalidRevisionException(String message, Nodeid revision, Integer revisionIndex) {
tikhomirov@347: super(message);
tikhomirov@347: assert revision != null || revisionIndex != null;
tikhomirov@347: rev = revision;
tikhomirov@347: revIdx = revisionIndex;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public HgInvalidRevisionException(Nodeid revision) {
tikhomirov@347: this(null, revision, null);
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public HgInvalidRevisionException(int revisionIndex) {
tikhomirov@347: this(null, null, revisionIndex);
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public Nodeid getRevision() {
tikhomirov@347: return rev;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public Integer getRevisionIndex() {
tikhomirov@347: return revIdx;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public HgInvalidRevisionException setRevision(Nodeid revision) {
tikhomirov@347: assert revision != null;
tikhomirov@347: rev = revision;
tikhomirov@347: return this;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: // int, not Integer is on purpose, not to clear exception completely
tikhomirov@347: public HgInvalidRevisionException setRevisionIndex(int revisionIndex) {
tikhomirov@347: revIdx = revisionIndex;
tikhomirov@347: return this;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: public HgInvalidRevisionException setRevisionIndex(int revisionIndex, int rangeLeft, int rangeRight) {
tikhomirov@347: revIdx = revisionIndex;
tikhomirov@347: rangeLeftBoundary = rangeLeft;
tikhomirov@347: rangeRightBoundary = rangeRight;
tikhomirov@347: return this;
tikhomirov@347: }
tikhomirov@347:
tikhomirov@347: @Override
tikhomirov@347: public String getMessage() {
tikhomirov@347: String msg = super.getMessage();
tikhomirov@347: if (msg != null) {
tikhomirov@347: return msg;
tikhomirov@347: }
tikhomirov@347: StringBuilder sb = new StringBuilder();
tikhomirov@347: sb.append('[');
tikhomirov@347: if (rev != null) {
tikhomirov@347: sb.append(rev.shortNotation());
tikhomirov@347: sb.append(' ');
tikhomirov@347: }
tikhomirov@347: if (revIdx != null) {
tikhomirov@347: String sr;
tikhomirov@347: switch (revIdx) {
tikhomirov@347: case HgRepository.BAD_REVISION : sr = "UNKNOWN"; break;
tikhomirov@347: case HgRepository.TIP : sr = "TIP"; break;
tikhomirov@347: case HgRepository.WORKING_COPY: sr = "WORKING-COPY"; break;
tikhomirov@347: default : sr = revIdx.toString();
tikhomirov@347: }
tikhomirov@347: if (rangeLeftBoundary != -1 || rangeRightBoundary != -1) {
tikhomirov@347: sb.append(String.format("%s is not from [%d..%d]", sr, rangeLeftBoundary, rangeRightBoundary));
tikhomirov@347: } else {
tikhomirov@347: sb.append(sr);
tikhomirov@347: }
tikhomirov@347: }
tikhomirov@347: sb.append(']');
tikhomirov@347: return sb.toString();
tikhomirov@347: }
tikhomirov@347: }