HashedSequenceComparator.java

  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. package org.eclipse.jgit.diff;

  11. /**
  12.  * Wrap another comparator for use with
  13.  * {@link org.eclipse.jgit.diff.HashedSequence}.
  14.  * <p>
  15.  * This comparator acts as a proxy for the real comparator, evaluating the
  16.  * cached hash code before testing the underlying comparator's equality.
  17.  * Comparators of this type must be used with a
  18.  * {@link org.eclipse.jgit.diff.HashedSequence}.
  19.  * <p>
  20.  * To construct an instance of this type use
  21.  * {@link org.eclipse.jgit.diff.HashedSequencePair}.
  22.  *
  23.  * @param <S>
  24.  *            the base sequence type.
  25.  */
  26. public final class HashedSequenceComparator<S extends Sequence> extends
  27.         SequenceComparator<HashedSequence<S>> {
  28.     private final SequenceComparator<? super S> cmp;

  29.     HashedSequenceComparator(SequenceComparator<? super S> cmp) {
  30.         this.cmp = cmp;
  31.     }

  32.     /** {@inheritDoc} */
  33.     @Override
  34.     public boolean equals(HashedSequence<S> a, int ai, //
  35.             HashedSequence<S> b, int bi) {
  36.         return a.hashes[ai] == b.hashes[bi]
  37.                 && cmp.equals(a.base, ai, b.base, bi);
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public int hash(HashedSequence<S> seq, int ptr) {
  42.         return seq.hashes[ptr];
  43.     }
  44. }