Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/CsetParamKeeper.java @ 565:78a9e26e670d
Refactor common code to initialize changelog revision for a command into standalone class
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 09 Apr 2013 17:15:30 +0200 |
parents | |
children | 6526d8adbc0f |
comparison
equal
deleted
inserted
replaced
564:e6407313bab7 | 565:78a9e26e670d |
---|---|
1 /* | |
2 * Copyright (c) 2013 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.internal; | |
18 | |
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; | |
20 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
21 | |
22 import org.tmatesoft.hg.core.HgBadArgumentException; | |
23 import org.tmatesoft.hg.core.Nodeid; | |
24 import org.tmatesoft.hg.repo.HgInvalidRevisionException; | |
25 import org.tmatesoft.hg.repo.HgRepository; | |
26 | |
27 /** | |
28 * Common code to keep changelog revision and to perform boundary check. | |
29 * | |
30 * @author Artem Tikhomirov | |
31 * @author TMate Software Ltd. | |
32 */ | |
33 public class CsetParamKeeper { | |
34 private final HgRepository repo; | |
35 private int changelogRevisionIndex = HgRepository.BAD_REVISION; | |
36 | |
37 public CsetParamKeeper(HgRepository hgRepo) { | |
38 repo = hgRepo; | |
39 } | |
40 | |
41 public CsetParamKeeper set(Nodeid changeset) throws HgBadArgumentException { | |
42 try { | |
43 set(repo.getChangelog().getRevisionIndex(changeset)); | |
44 } catch (HgInvalidRevisionException ex) { | |
45 throw new HgBadArgumentException("Can't find revision", ex).setRevision(changeset); | |
46 } | |
47 return this; | |
48 } | |
49 | |
50 public CsetParamKeeper set(int changelogRevIndex) throws HgBadArgumentException { | |
51 int lastCsetIndex = repo.getChangelog().getLastRevision(); | |
52 if (changelogRevIndex == HgRepository.TIP) { | |
53 changelogRevIndex = lastCsetIndex; | |
54 } | |
55 if (changelogRevIndex < 0 || changelogRevIndex > lastCsetIndex) { | |
56 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changelogRevIndex, lastCsetIndex), null).setRevisionIndex(changelogRevIndex); | |
57 } | |
58 doSet(changelogRevIndex); | |
59 return this; | |
60 } | |
61 | |
62 public void doSet(int changelogRevIndex) { | |
63 changelogRevisionIndex = changelogRevIndex; | |
64 } | |
65 | |
66 /** | |
67 * @return the value set, or {@link HgRepository#BAD_REVISION} otherwise | |
68 */ | |
69 public int get() { | |
70 return changelogRevisionIndex; | |
71 } | |
72 | |
73 /** | |
74 * @param defaultRevisionIndex value to return when no revision was set, may be {@link HgRepository#TIP} which gets translated to real index if used | |
75 * @return changelog revision index if set, or defaultRevisionIndex value otherwise | |
76 */ | |
77 public int get(int defaultRevisionIndex) { | |
78 // XXX perhaps, shall translate other predefined constants (like WORKING COPY) here, too (e.g. for HgRevertCommand) | |
79 if (changelogRevisionIndex != BAD_REVISION || changelogRevisionIndex != TIP) { | |
80 return changelogRevisionIndex; | |
81 } | |
82 if (changelogRevisionIndex == TIP || defaultRevisionIndex == TIP) { | |
83 return repo.getChangelog().getLastRevision(); | |
84 } | |
85 return defaultRevisionIndex; | |
86 } | |
87 } |