SubsequenceComparator.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.Subsequence}.
  14.  * <p>
  15.  * This comparator acts as a proxy for the real comparator, translating element
  16.  * indexes on the fly by adding the subsequence's begin offset to them.
  17.  * Comparators of this type must be used with a
  18.  * {@link org.eclipse.jgit.diff.Subsequence}.
  19.  *
  20.  * @param <S>
  21.  *            the base sequence type.
  22.  */
  23. public final class SubsequenceComparator<S extends Sequence> extends
  24.         SequenceComparator<Subsequence<S>> {
  25.     private final SequenceComparator<? super S> cmp;

  26.     /**
  27.      * Construct a comparator wrapping another comparator.
  28.      *
  29.      * @param cmp
  30.      *            the real comparator.
  31.      */
  32.     public SubsequenceComparator(SequenceComparator<? super S> cmp) {
  33.         this.cmp = cmp;
  34.     }

  35.     /** {@inheritDoc} */
  36.     @Override
  37.     public boolean equals(Subsequence<S> a, int ai, Subsequence<S> b, int bi) {
  38.         return cmp.equals(a.base, ai + a.begin, b.base, bi + b.begin);
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public int hash(Subsequence<S> seq, int ptr) {
  43.         return cmp.hash(seq.base, ptr + seq.begin);
  44.     }
  45. }