comparison src/org/tmatesoft/hg/internal/RevlogDelegate.java @ 695:053bb4397bf9

Refactoring: nice Revlog.indexWalk() implementation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 05 Aug 2013 18:45:16 +0200
parents
children
comparison
equal deleted inserted replaced
694:7efabe0cddcf 695:053bb4397bf9
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 org.tmatesoft.hg.core.Nodeid;
20 import org.tmatesoft.hg.internal.RevlogStream.Inspector;
21 import org.tmatesoft.hg.repo.HgRepository;
22 import org.tmatesoft.hg.repo.HgRuntimeException;
23
24 /**
25 * Does almost nothing, facilitates chains of inspectors and sharing certain information between them
26 *
27 * @author Artem Tikhomirov
28 * @author TMate Software Ltd.
29 */
30 public abstract class RevlogDelegate implements RevlogStream.Inspector {
31
32 private final Inspector next;
33 private Nodeid nid;
34 private final RevlogDelegate nextAsRD;
35
36 protected RevlogDelegate(RevlogStream.Inspector nextInChain) {
37 next = nextInChain;
38 if (nextInChain instanceof RevlogDelegate) {
39 // additional benefits
40 nextAsRD = (RevlogDelegate) nextInChain;
41 } else {
42 nextAsRD = null;
43 }
44 }
45
46 /**
47 * iterates index only and ensures pre/post processing is invoked for the chain
48 */
49 public void walk(HgRepository hgRepo, RevlogStream stream, int from, int to) {
50 // hgRepo is handy for present uses, but is generally not appropriate,
51 // it's ok to refactor and let subclasses get what they need through e.g. a cons
52 stream.iterate(from, to, false, this);
53 postWalk(hgRepo);
54 }
55
56 // does nothing but gives a chance to delegate to handle the same
57 protected void postWalk(HgRepository hgRepo) {
58 if (nextAsRD != null) {
59 nextAsRD.postWalk(hgRepo);
60 }
61 }
62
63 /**
64 * @return Nodeid of current revision if already known, or a new instance otherwise.
65 * The value will propagate to subsequent {@link RevlogDelegate RevlogDelegates}, if any
66 */
67 protected Nodeid getRevision(byte[] nodeid) {
68 if (nid == null) {
69 nid = Nodeid.fromBinary(nodeid, 0);
70 }
71 return nid;
72 }
73
74 protected void setRevision(Nodeid nodeid) {
75 nid = nodeid;
76 }
77
78 public void next(int revisionIndex, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) throws HgRuntimeException {
79 if (next != null) {
80 if (nextAsRD != null) {
81 nextAsRD.setRevision(nid); // null is fine
82 }
83 next.next(revisionIndex, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, nodeid, data);
84 }
85 nid = null; // forget it, get ready for the next iteration
86 }
87 }