BitmapCommit.java

  1. /*
  2.  * Copyright (c) 2020, Google LLC 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.  * http://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.internal.storage.pack;

  11. import org.eclipse.jgit.lib.AnyObjectId;
  12. import org.eclipse.jgit.lib.ObjectId;

  13. /**
  14.  * A commit object for which a bitmap index should be built.
  15.  */
  16. public final class BitmapCommit extends ObjectId {

  17.     private final boolean reuseWalker;

  18.     private final int flags;

  19.     private final boolean addToIndex;

  20.     BitmapCommit(AnyObjectId objectId, boolean reuseWalker, int flags) {
  21.         super(objectId);
  22.         this.reuseWalker = reuseWalker;
  23.         this.flags = flags;
  24.         this.addToIndex = false;
  25.     }

  26.     BitmapCommit(AnyObjectId objectId, boolean reuseWalker, int flags,
  27.                  boolean addToIndex) {
  28.         super(objectId);
  29.         this.reuseWalker = reuseWalker;
  30.         this.flags = flags;
  31.         this.addToIndex = addToIndex;
  32.     }

  33.     boolean isReuseWalker() {
  34.         return reuseWalker;
  35.     }

  36.     int getFlags() {
  37.         return flags;
  38.     }

  39.     /**
  40.      * Whether corresponding bitmap should be added to PackBitmapIndexBuilder.
  41.      *
  42.      * @return true if the corresponding bitmap should be added to
  43.      *         PackBitmapIndexBuilder.
  44.      */
  45.     public boolean isAddToIndex() {
  46.         return addToIndex;
  47.     }

  48.     /**
  49.      * Get a builder of BitmapCommit whose object id is {@code objId}.
  50.      *
  51.      * @param objId
  52.      *            the object id of the BitmapCommit
  53.      * @return a BitmapCommit builder with object id set.
  54.      */
  55.     public static Builder newBuilder(AnyObjectId objId) {
  56.         return new Builder().setId(objId);
  57.     }

  58.     /**
  59.      * Get a builder of BitmapCommit whose fields are copied from
  60.      * {@code commit}.
  61.      *
  62.      * @param commit
  63.      *            the bitmap commit the builder is copying from
  64.      * @return a BitmapCommit build with fields copied from an existing bitmap
  65.      *         commit.
  66.      */
  67.     public static Builder copyFrom(BitmapCommit commit) {
  68.         return new Builder().setId(commit)
  69.                 .setReuseWalker(commit.isReuseWalker())
  70.                 .setFlags(commit.getFlags())
  71.                 .setAddToIndex(commit.isAddToIndex());
  72.     }

  73.     /**
  74.      * Builder of BitmapCommit.
  75.      */
  76.     public static class Builder {
  77.         private AnyObjectId objectId;

  78.         private boolean reuseWalker;

  79.         private int flags;

  80.         private boolean addToIndex;

  81.         // Prevent default constructor.
  82.         private Builder() {
  83.         }

  84.         /**
  85.          * Set objectId of the builder.
  86.          *
  87.          * @param objectId
  88.          *            the object id of the BitmapCommit
  89.          * @return the builder itself
  90.          */
  91.         public Builder setId(AnyObjectId objectId) {
  92.             this.objectId = objectId;
  93.             return this;
  94.         }

  95.         /**
  96.          * Set reuseWalker of the builder.
  97.          *
  98.          * @param reuseWalker
  99.          *            whether the BitmapCommit should reuse bitmap walker when
  100.          *            walking objects
  101.          * @return the builder itself
  102.          */
  103.         public Builder setReuseWalker(boolean reuseWalker) {
  104.             this.reuseWalker = reuseWalker;
  105.             return this;
  106.         }

  107.         /**
  108.          * Set flags of the builder.
  109.          *
  110.          * @param flags
  111.          *            the flags of the BitmapCommit
  112.          * @return the builder itself
  113.          */
  114.         public Builder setFlags(int flags) {
  115.             this.flags = flags;
  116.             return this;
  117.         }

  118.         /**
  119.          * Set whether whether the bitmap of the BitmapCommit should be added to
  120.          * PackBitmapIndexBuilder when building bitmap index file.
  121.          *
  122.          * @param addToIndex
  123.          *            whether the bitmap of the BitmapCommit should be added to
  124.          *            PackBitmapIndexBuilder when building bitmap index file
  125.          * @return the builder itself
  126.          */
  127.         public Builder setAddToIndex(boolean addToIndex) {
  128.             this.addToIndex = addToIndex;
  129.             return this;
  130.         }

  131.         /**
  132.          * Builds BitmapCommit from the builder.
  133.          *
  134.          * @return the new BitmapCommit.
  135.          */
  136.         public BitmapCommit build() {
  137.             return new BitmapCommit(objectId, reuseWalker, flags,
  138.                     addToIndex);
  139.         }
  140.     }
  141. }