View Javadoc
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.HashedSequence}.
16   * <p>
17   * This comparator acts as a proxy for the real comparator, evaluating the
18   * cached hash code before testing the underlying comparator's equality.
19   * Comparators of this type must be used with a
20   * {@link org.eclipse.jgit.diff.HashedSequence}.
21   * <p>
22   * To construct an instance of this type use
23   * {@link org.eclipse.jgit.diff.HashedSequencePair}.
24   *
25   * @param <S>
26   *            the base sequence type.
27   */
28  public final class HashedSequenceComparator<S extends Sequence> extends
29  		SequenceComparator<HashedSequence<S>> {
30  	private final SequenceComparator<? super S> cmp;
31  
32  	HashedSequenceComparator(SequenceComparator<? super S> cmp) {
33  		this.cmp = cmp;
34  	}
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	public boolean equals(HashedSequence<S> a, int ai, //
39  			HashedSequence<S> b, int bi) {
40  		return a.hashes[ai] == b.hashes[bi]
41  				&& cmp.equals(a.base, ai, b.base, bi);
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public int hash(HashedSequence<S> seq, int ptr) {
47  		return seq.hashes[ptr];
48  	}
49  }