DfsStreamKey.java

  1. /*
  2.  * Copyright (C) 2011, 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.internal.storage.dfs;

  11. import static java.nio.charset.StandardCharsets.UTF_8;

  12. import java.util.Arrays;

  13. import org.eclipse.jgit.annotations.Nullable;
  14. import org.eclipse.jgit.internal.storage.pack.PackExt;

  15. /**
  16.  * Key used by {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache} to disambiguate streams.
  17.  */
  18. public abstract class DfsStreamKey {
  19.     /**
  20.      * Create a {@code DfsStreamKey}
  21.      *
  22.      * @param repo
  23.      *            description of the containing repository.
  24.      * @param name
  25.      *            compute the key from a string name.
  26.      * @param ext
  27.      *            pack file extension, or {@code null}.
  28.      * @return key for {@code name}
  29.      */
  30.     public static DfsStreamKey of(DfsRepositoryDescription repo, String name,
  31.             @Nullable PackExt ext) {
  32.         return new ByteArrayDfsStreamKey(repo, name.getBytes(UTF_8), ext);
  33.     }

  34.     final int hash;

  35.     final int packExtPos;

  36.     /**
  37.      * Constructor for DfsStreamKey.
  38.      *
  39.      * @param hash
  40.      *            hash of the other identifying components of the key.
  41.      * @param ext
  42.      *            pack file extension, or {@code null}.
  43.      */
  44.     protected DfsStreamKey(int hash, @Nullable PackExt ext) {
  45.         // Multiply by 31 here so we can more directly combine with another
  46.         // value without doing the multiply there.
  47.         this.hash = hash * 31;
  48.         this.packExtPos = ext == null ? 0 : ext.getPosition();
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public int hashCode() {
  53.         return hash;
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public abstract boolean equals(Object o);

  58.     /** {@inheritDoc} */
  59.     @SuppressWarnings("boxing")
  60.     @Override
  61.     public String toString() {
  62.         return String.format("DfsStreamKey[hash=%08x]", hash); //$NON-NLS-1$
  63.     }

  64.     private static final class ByteArrayDfsStreamKey extends DfsStreamKey {
  65.         private final DfsRepositoryDescription repo;

  66.         private final byte[] name;

  67.         ByteArrayDfsStreamKey(DfsRepositoryDescription repo, byte[] name,
  68.                 @Nullable PackExt ext) {
  69.             super(repo.hashCode() * 31 + Arrays.hashCode(name), ext);
  70.             this.repo = repo;
  71.             this.name = name;
  72.         }

  73.         @Override
  74.         public boolean equals(Object o) {
  75.             if (o instanceof ByteArrayDfsStreamKey) {
  76.                 ByteArrayDfsStreamKey k = (ByteArrayDfsStreamKey) o;
  77.                 return hash == k.hash && repo.equals(k.repo)
  78.                         && Arrays.equals(name, k.name);
  79.             }
  80.             return false;
  81.         }
  82.     }

  83.     static final class ForReverseIndex extends DfsStreamKey {
  84.         private final DfsStreamKey idxKey;

  85.         ForReverseIndex(DfsStreamKey idxKey) {
  86.             super(idxKey.hash + 1, null);
  87.             this.idxKey = idxKey;
  88.         }

  89.         @Override
  90.         public boolean equals(Object o) {
  91.             return o instanceof ForReverseIndex
  92.                     && idxKey.equals(((ForReverseIndex) o).idxKey);
  93.         }
  94.     }
  95. }