comparison src/org/tmatesoft/hg/internal/GeneratePatchInspector.java @ 544:7f5998a9619d

Refactor PatchGenerator to be generic and welcome sequence of any nature
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 15 Feb 2013 16:48:54 +0100
parents
children 4ea0351ca878
comparison
equal deleted inserted replaced
543:1e95f48d9886 544:7f5998a9619d
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.internal.PatchGenerator.DeltaInspector;
20 import org.tmatesoft.hg.internal.PatchGenerator.LineSequence;
21
22 class GeneratePatchInspector extends DeltaInspector<LineSequence> {
23 private final Patch deltaCollector;
24
25 GeneratePatchInspector(Patch p) {
26 assert p != null;
27 deltaCollector = p;
28 }
29
30 public static Patch delta(byte[] prev, byte[] content) {
31 Patch rv = new Patch();
32 PatchGenerator<LineSequence> pg = new PatchGenerator<LineSequence>();
33 pg.init(new LineSequence(prev).splitByNewlines(), new LineSequence(content).splitByNewlines());
34 pg.findMatchingBlocks(new GeneratePatchInspector(rv));
35 return rv;
36 }
37
38 @Override
39 protected void changed(int s1From, int s1To, int s2From, int s2To) {
40 int from = seq1.chunk(s1From).getOffset();
41 int to = seq1.chunk(s1To).getOffset();
42 byte[] data = seq2.data(s2From, s2To);
43 deltaCollector.add(from, to, data);
44 }
45
46 @Override
47 protected void deleted(int s2DeletionPoint, int s1From, int s1To) {
48 int from = seq1.chunk(s1From).getOffset();
49 int to = seq1.chunk(s1To).getOffset();
50 deltaCollector.add(from, to, new byte[0]);
51 }
52
53 @Override
54 protected void added(int s1InsertPoint, int s2From, int s2To) {
55 int insPoint = seq1.chunk(s1InsertPoint).getOffset();
56 byte[] data = seq2.data(s2From, s2To);
57 deltaCollector.add(insPoint, insPoint, data);
58 }
59 }