1 /*
2 * Copyright (C) 2010, Google Inc. and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 package org.eclipse.jgit.diff;
12
13 /**
14 * Wrap another comparator for use with
15 * {@link org.eclipse.jgit.diff.Subsequence}.
16 * <p>
17 * This comparator acts as a proxy for the real comparator, translating element
18 * indexes on the fly by adding the subsequence's begin offset to them.
19 * Comparators of this type must be used with a
20 * {@link org.eclipse.jgit.diff.Subsequence}.
21 *
22 * @param <S>
23 * the base sequence type.
24 */
25 public final class SubsequenceComparator<S extends Sequence> extends
26 SequenceComparator<Subsequence<S>> {
27 private final SequenceComparator<? super S> cmp;
28
29 /**
30 * Construct a comparator wrapping another comparator.
31 *
32 * @param cmp
33 * the real comparator.
34 */
35 public SubsequenceComparator(SequenceComparator<? super S> cmp) {
36 this.cmp = cmp;
37 }
38
39 /** {@inheritDoc} */
40 @Override
41 public boolean equals(Subsequence<S> a, int ai, Subsequence<S> b, int bi) {
42 return cmp.equals(a.base, ai + a.begin, b.base, bi + b.begin);
43 }
44
45 /** {@inheritDoc} */
46 @Override
47 public int hash(Subsequence<S> seq, int ptr) {
48 return cmp.hash(seq.base, ptr + seq.begin);
49 }
50 }