annotate src/org/tmatesoft/hg/internal/CsetParamKeeper.java @ 606:5daa42067e7c

Avoid mmap files when only few bytes are to be read
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 07 May 2013 14:16:35 +0200
parents 78a9e26e670d
children 6526d8adbc0f
rev   line source
565
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import org.tmatesoft.hg.core.HgBadArgumentException;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import org.tmatesoft.hg.core.Nodeid;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.repo.HgRepository;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 /**
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 * Common code to keep changelog revision and to perform boundary check.
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 *
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 * @author Artem Tikhomirov
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @author TMate Software Ltd.
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 */
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 public class CsetParamKeeper {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 private final HgRepository repo;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 private int changelogRevisionIndex = HgRepository.BAD_REVISION;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 public CsetParamKeeper(HgRepository hgRepo) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 repo = hgRepo;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 public CsetParamKeeper set(Nodeid changeset) throws HgBadArgumentException {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 try {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 set(repo.getChangelog().getRevisionIndex(changeset));
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 } catch (HgInvalidRevisionException ex) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 throw new HgBadArgumentException("Can't find revision", ex).setRevision(changeset);
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 return this;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 public CsetParamKeeper set(int changelogRevIndex) throws HgBadArgumentException {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 int lastCsetIndex = repo.getChangelog().getLastRevision();
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 if (changelogRevIndex == HgRepository.TIP) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 changelogRevIndex = lastCsetIndex;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 if (changelogRevIndex < 0 || changelogRevIndex > lastCsetIndex) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changelogRevIndex, lastCsetIndex), null).setRevisionIndex(changelogRevIndex);
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 doSet(changelogRevIndex);
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 return this;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 public void doSet(int changelogRevIndex) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 changelogRevisionIndex = changelogRevIndex;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 /**
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 * @return the value set, or {@link HgRepository#BAD_REVISION} otherwise
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 */
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 public int get() {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 return changelogRevisionIndex;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 /**
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 * @param defaultRevisionIndex value to return when no revision was set, may be {@link HgRepository#TIP} which gets translated to real index if used
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 * @return changelog revision index if set, or defaultRevisionIndex value otherwise
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 */
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 public int get(int defaultRevisionIndex) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 // XXX perhaps, shall translate other predefined constants (like WORKING COPY) here, too (e.g. for HgRevertCommand)
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 if (changelogRevisionIndex != BAD_REVISION || changelogRevisionIndex != TIP) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 return changelogRevisionIndex;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 if (changelogRevisionIndex == TIP || defaultRevisionIndex == TIP) {
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 return repo.getChangelog().getLastRevision();
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 return defaultRevisionIndex;
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
78a9e26e670d Refactor common code to initialize changelog revision for a command into standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 }