Mercurial > hg4j
comparison src/org/tmatesoft/hg/core/HgInvalidRevisionException.java @ 347:8da7ade36c57
Add specific IAE subclass to handle wrong (e.g. outdated after rollback) revisions
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Tue, 22 Nov 2011 05:25:57 +0100 |
| parents | |
| children | 8107b95f4280 |
comparison
equal
deleted
inserted
replaced
| 346:6d2c6b2469fc | 347:8da7ade36c57 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.core; | |
| 18 | |
| 19 import org.tmatesoft.hg.internal.Experimental; | |
| 20 import org.tmatesoft.hg.repo.HgRepository; | |
| 21 | |
| 22 /** | |
| 23 * Use of revision or local revision index that is not valid for a given revlog. | |
| 24 * | |
| 25 * @author Artem Tikhomirov | |
| 26 * @author TMate Software Ltd. | |
| 27 */ | |
| 28 @SuppressWarnings("serial") | |
| 29 @Experimental(reason="1) Whether to use checked or runtime exception is not yet decided. 2) Perhaps, its use not bound to wrong arguments") | |
| 30 public class HgInvalidRevisionException extends IllegalArgumentException { | |
| 31 private Nodeid rev; | |
| 32 private Integer revIdx; | |
| 33 // next two make sense only when revIdx is present | |
| 34 private int rangeLeftBoundary = -1, rangeRightBoundary = -1; | |
| 35 | |
| 36 /** | |
| 37 * | |
| 38 * this exception is not expected to be initialized with another exception, although those who need to, | |
| 39 * may still use {@link #initCause(Throwable)} | |
| 40 * @param message optional description of the issue | |
| 41 * @param revision invalid revision, may be <code>null</code> if revisionIndex is used | |
| 42 * @param revisionIndex invalid revision index, may be <code>null</code> if not known and revision is supplied | |
| 43 */ | |
| 44 public HgInvalidRevisionException(String message, Nodeid revision, Integer revisionIndex) { | |
| 45 super(message); | |
| 46 assert revision != null || revisionIndex != null; | |
| 47 rev = revision; | |
| 48 revIdx = revisionIndex; | |
| 49 } | |
| 50 | |
| 51 public HgInvalidRevisionException(Nodeid revision) { | |
| 52 this(null, revision, null); | |
| 53 } | |
| 54 | |
| 55 public HgInvalidRevisionException(int revisionIndex) { | |
| 56 this(null, null, revisionIndex); | |
| 57 } | |
| 58 | |
| 59 public Nodeid getRevision() { | |
| 60 return rev; | |
| 61 } | |
| 62 | |
| 63 public Integer getRevisionIndex() { | |
| 64 return revIdx; | |
| 65 } | |
| 66 | |
| 67 public HgInvalidRevisionException setRevision(Nodeid revision) { | |
| 68 assert revision != null; | |
| 69 rev = revision; | |
| 70 return this; | |
| 71 } | |
| 72 | |
| 73 // int, not Integer is on purpose, not to clear exception completely | |
| 74 public HgInvalidRevisionException setRevisionIndex(int revisionIndex) { | |
| 75 revIdx = revisionIndex; | |
| 76 return this; | |
| 77 } | |
| 78 | |
| 79 public HgInvalidRevisionException setRevisionIndex(int revisionIndex, int rangeLeft, int rangeRight) { | |
| 80 revIdx = revisionIndex; | |
| 81 rangeLeftBoundary = rangeLeft; | |
| 82 rangeRightBoundary = rangeRight; | |
| 83 return this; | |
| 84 } | |
| 85 | |
| 86 @Override | |
| 87 public String getMessage() { | |
| 88 String msg = super.getMessage(); | |
| 89 if (msg != null) { | |
| 90 return msg; | |
| 91 } | |
| 92 StringBuilder sb = new StringBuilder(); | |
| 93 sb.append('['); | |
| 94 if (rev != null) { | |
| 95 sb.append(rev.shortNotation()); | |
| 96 sb.append(' '); | |
| 97 } | |
| 98 if (revIdx != null) { | |
| 99 String sr; | |
| 100 switch (revIdx) { | |
| 101 case HgRepository.BAD_REVISION : sr = "UNKNOWN"; break; | |
| 102 case HgRepository.TIP : sr = "TIP"; break; | |
| 103 case HgRepository.WORKING_COPY: sr = "WORKING-COPY"; break; | |
| 104 default : sr = revIdx.toString(); | |
| 105 } | |
| 106 if (rangeLeftBoundary != -1 || rangeRightBoundary != -1) { | |
| 107 sb.append(String.format("%s is not from [%d..%d]", sr, rangeLeftBoundary, rangeRightBoundary)); | |
| 108 } else { | |
| 109 sb.append(sr); | |
| 110 } | |
| 111 } | |
| 112 sb.append(']'); | |
| 113 return sb.toString(); | |
| 114 } | |
| 115 } |
