ObjectIdRef.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc.
  3.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.lib;

  12. import org.eclipse.jgit.annotations.NonNull;
  13. import org.eclipse.jgit.annotations.Nullable;

  14. /**
  15.  * A {@link org.eclipse.jgit.lib.Ref} that points directly at an
  16.  * {@link org.eclipse.jgit.lib.ObjectId}.
  17.  */
  18. public abstract class ObjectIdRef implements Ref {
  19.     /** Any reference whose peeled value is not yet known. */
  20.     public static class Unpeeled extends ObjectIdRef {
  21.         /**
  22.          * Create a new ref pairing.
  23.          *
  24.          * @param st
  25.          *            method used to store this ref.
  26.          * @param name
  27.          *            name of this ref.
  28.          * @param id
  29.          *            current value of the ref. May be {@code null} to indicate
  30.          *            a ref that does not exist yet.
  31.          */
  32.         public Unpeeled(@NonNull Storage st, @NonNull String name,
  33.                 @Nullable ObjectId id) {
  34.             super(st, name, id, UNDEFINED_UPDATE_INDEX);
  35.         }

  36.         /**
  37.          * Create a new ref pairing with update index.
  38.          *
  39.          * @param st
  40.          *            method used to store this ref.
  41.          * @param name
  42.          *            name of this ref.
  43.          * @param id
  44.          *            current value of the ref. May be {@code null} to indicate
  45.          *            a ref that does not exist yet.
  46.          * @param updateIndex
  47.          *            number increasing with each update to the reference.
  48.          * @since 5.3
  49.          */
  50.         public Unpeeled(@NonNull Storage st, @NonNull String name,
  51.                 @Nullable ObjectId id, long updateIndex) {
  52.             super(st, name, id, updateIndex);
  53.         }

  54.         @Override
  55.         @Nullable
  56.         public ObjectId getPeeledObjectId() {
  57.             return null;
  58.         }

  59.         @Override
  60.         public boolean isPeeled() {
  61.             return false;
  62.         }
  63.     }

  64.     /** An annotated tag whose peeled object has been cached. */
  65.     public static class PeeledTag extends ObjectIdRef {
  66.         private final ObjectId peeledObjectId;

  67.         /**
  68.          * Create a new ref pairing.
  69.          *
  70.          * @param st
  71.          *            method used to store this ref.
  72.          * @param name
  73.          *            name of this ref.
  74.          * @param id
  75.          *            current value of the ref.
  76.          * @param p
  77.          *            the first non-tag object that tag {@code id} points to.
  78.          */
  79.         public PeeledTag(@NonNull Storage st, @NonNull String name,
  80.                 @Nullable ObjectId id, @NonNull ObjectId p) {
  81.             super(st, name, id, UNDEFINED_UPDATE_INDEX);
  82.             peeledObjectId = p;
  83.         }

  84.         /**
  85.          * Create a new ref pairing with update index.
  86.          *
  87.          * @param st
  88.          *            method used to store this ref.
  89.          * @param name
  90.          *            name of this ref.
  91.          * @param id
  92.          *            current value of the ref. May be {@code null} to indicate
  93.          *            a ref that does not exist yet.
  94.          * @param p
  95.          *            the first non-tag object that tag {@code id} points to.
  96.          * @param updateIndex
  97.          *            number increasing with each update to the reference.
  98.          * @since 5.3
  99.          */
  100.         public PeeledTag(@NonNull Storage st, @NonNull String name,
  101.                 @Nullable ObjectId id, @NonNull ObjectId p, long updateIndex) {
  102.             super(st, name, id, updateIndex);
  103.             peeledObjectId = p;
  104.         }

  105.         @Override
  106.         @NonNull
  107.         public ObjectId getPeeledObjectId() {
  108.             return peeledObjectId;
  109.         }

  110.         @Override
  111.         public boolean isPeeled() {
  112.             return true;
  113.         }
  114.     }

  115.     /** A reference to a non-tag object coming from a cached source. */
  116.     public static class PeeledNonTag extends ObjectIdRef {
  117.         /**
  118.          * Create a new ref pairing.
  119.          *
  120.          * @param st
  121.          *            method used to store this ref.
  122.          * @param name
  123.          *            name of this ref.
  124.          * @param id
  125.          *            current value of the ref. May be {@code null} to indicate
  126.          *            a ref that does not exist yet.
  127.          */
  128.         public PeeledNonTag(@NonNull Storage st, @NonNull String name,
  129.                 @Nullable ObjectId id) {
  130.             super(st, name, id, UNDEFINED_UPDATE_INDEX);
  131.         }

  132.         /**
  133.          * Create a new ref pairing with update index.
  134.          *
  135.          * @param st
  136.          *            method used to store this ref.
  137.          * @param name
  138.          *            name of this ref.
  139.          * @param id
  140.          *            current value of the ref. May be {@code null} to indicate
  141.          *            a ref that does not exist yet.
  142.          * @param updateIndex
  143.          *            number increasing with each update to the reference.
  144.          * @since 5.3
  145.          */
  146.         public PeeledNonTag(@NonNull Storage st, @NonNull String name,
  147.                 @Nullable ObjectId id, long updateIndex) {
  148.             super(st, name, id, updateIndex);
  149.         }

  150.         @Override
  151.         @Nullable
  152.         public ObjectId getPeeledObjectId() {
  153.             return null;
  154.         }

  155.         @Override
  156.         public boolean isPeeled() {
  157.             return true;
  158.         }
  159.     }

  160.     private final String name;

  161.     private final Storage storage;

  162.     private final ObjectId objectId;

  163.     private final long updateIndex;

  164.     /**
  165.      * Create a new ref pairing.
  166.      *
  167.      * @param st
  168.      *            method used to store this ref.
  169.      * @param name
  170.      *            name of this ref.
  171.      * @param id
  172.      *            current value of the ref. May be {@code null} to indicate a
  173.      *            ref that does not exist yet.
  174.      * @param updateIndex
  175.      *            number that increases with each ref update. Set to -1 if the
  176.      *            storage doesn't support versioning.
  177.      * @since 5.3
  178.      */
  179.     protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
  180.             @Nullable ObjectId id, long updateIndex) {
  181.         this.name = name;
  182.         this.storage = st;
  183.         this.objectId = id;
  184.         this.updateIndex = updateIndex;
  185.     }

  186.     /** {@inheritDoc} */
  187.     @Override
  188.     @NonNull
  189.     public String getName() {
  190.         return name;
  191.     }

  192.     /** {@inheritDoc} */
  193.     @Override
  194.     public boolean isSymbolic() {
  195.         return false;
  196.     }

  197.     /** {@inheritDoc} */
  198.     @Override
  199.     @NonNull
  200.     public Ref getLeaf() {
  201.         return this;
  202.     }

  203.     /** {@inheritDoc} */
  204.     @Override
  205.     @NonNull
  206.     public Ref getTarget() {
  207.         return this;
  208.     }

  209.     /** {@inheritDoc} */
  210.     @Override
  211.     @Nullable
  212.     public ObjectId getObjectId() {
  213.         return objectId;
  214.     }

  215.     /** {@inheritDoc} */
  216.     @Override
  217.     @NonNull
  218.     public Storage getStorage() {
  219.         return storage;
  220.     }

  221.     /**
  222.      * {@inheritDoc}
  223.      * @since 5.3
  224.      */
  225.     @Override
  226.     public long getUpdateIndex() {
  227.         if (updateIndex == UNDEFINED_UPDATE_INDEX) {
  228.             throw new UnsupportedOperationException();
  229.         }
  230.         return updateIndex;
  231.     }

  232.     /** {@inheritDoc} */
  233.     @NonNull
  234.     @Override
  235.     public String toString() {
  236.         StringBuilder r = new StringBuilder();
  237.         r.append("Ref["); //$NON-NLS-1$
  238.         r.append(getName());
  239.         r.append('=');
  240.         r.append(ObjectId.toString(getObjectId()));
  241.         r.append('(');
  242.         r.append(updateIndex); // Print value, even if -1
  243.         r.append(")]"); //$NON-NLS-1$
  244.         return r.toString();
  245.     }
  246. }